[TASK] Replace @expectedException with code wise test in ext:core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / TransientMemoryBackendTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Testcase for the TransientMemory cache backend
19 *
20 * This file is a backport from FLOW3
21 */
22 class TransientMemoryBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
23 {
24 /**
25 * @test
26 */
27 public function setThrowsExceptionIfNoFrontEndHasBeenSet()
28 {
29 $this->expectException(\TYPO3\CMS\Core\Cache\Exception::class);
30 $this->expectExceptionCode(1238244992);
31
32 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
33 $data = 'Some data';
34 $identifier = 'MyIdentifier';
35 $backend->set($identifier, $data);
36 }
37
38 /**
39 * @test
40 */
41 public function itIsPossibleToSetAndCheckExistenceInCache()
42 {
43 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', false);
44 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
45 $backend->setCache($cache);
46 $data = 'Some data';
47 $identifier = 'MyIdentifier';
48 $backend->set($identifier, $data);
49 $inCache = $backend->has($identifier);
50 $this->assertTrue($inCache);
51 }
52
53 /**
54 * @test
55 */
56 public function itIsPossibleToSetAndGetEntry()
57 {
58 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', false);
59 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
60 $backend->setCache($cache);
61 $data = 'Some data';
62 $identifier = 'MyIdentifier';
63 $backend->set($identifier, $data);
64 $fetchedData = $backend->get($identifier);
65 $this->assertEquals($data, $fetchedData);
66 }
67
68 /**
69 * @test
70 */
71 public function itIsPossibleToRemoveEntryFromCache()
72 {
73 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', false);
74 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
75 $backend->setCache($cache);
76 $data = 'Some data';
77 $identifier = 'MyIdentifier';
78 $backend->set($identifier, $data);
79 $backend->remove($identifier);
80 $inCache = $backend->has($identifier);
81 $this->assertFalse($inCache);
82 }
83
84 /**
85 * @test
86 */
87 public function itIsPossibleToOverwriteAnEntryInTheCache()
88 {
89 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', false);
90 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
91 $backend->setCache($cache);
92 $data = 'Some data';
93 $identifier = 'MyIdentifier';
94 $backend->set($identifier, $data);
95 $otherData = 'some other data';
96 $backend->set($identifier, $otherData);
97 $fetchedData = $backend->get($identifier);
98 $this->assertEquals($otherData, $fetchedData);
99 }
100
101 /**
102 * @test
103 */
104 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag()
105 {
106 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', false);
107 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
108 $backend->setCache($cache);
109 $data = 'Some data';
110 $entryIdentifier = 'MyIdentifier';
111 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
112 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
113 $this->assertEquals($entryIdentifier, $retrieved[0]);
114 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
115 $this->assertEquals($entryIdentifier, $retrieved[0]);
116 }
117
118 /**
119 * @test
120 */
121 public function hasReturnsFalseIfTheEntryDoesntExist()
122 {
123 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', false);
124 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
125 $backend->setCache($cache);
126 $identifier = 'NonExistingIdentifier';
127 $inCache = $backend->has($identifier);
128 $this->assertFalse($inCache);
129 }
130
131 /**
132 * @test
133 */
134 public function removeReturnsFalseIfTheEntryDoesntExist()
135 {
136 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', false);
137 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
138 $backend->setCache($cache);
139 $identifier = 'NonExistingIdentifier';
140 $inCache = $backend->remove($identifier);
141 $this->assertFalse($inCache);
142 }
143
144 /**
145 * @test
146 */
147 public function flushByTagRemovesCacheEntriesWithSpecifiedTag()
148 {
149 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', false);
150 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
151 $backend->setCache($cache);
152 $data = 'some data' . microtime();
153 $backend->set('TransientMemoryBackendTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
154 $backend->set('TransientMemoryBackendTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
155 $backend->set('TransientMemoryBackendTest3', $data, array('UnitTestTag%test'));
156 $backend->flushByTag('UnitTestTag%special');
157 $this->assertTrue($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
158 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
159 $this->assertTrue($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
160 }
161
162 /**
163 * @test
164 */
165 public function flushRemovesAllCacheEntries()
166 {
167 $cache = $this->getMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class, array(), array(), '', false);
168 $backend = new \TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend('Testing');
169 $backend->setCache($cache);
170 $data = 'some data' . microtime();
171 $backend->set('TransientMemoryBackendTest1', $data);
172 $backend->set('TransientMemoryBackendTest2', $data);
173 $backend->set('TransientMemoryBackendTest3', $data);
174 $backend->flush();
175 $this->assertFalse($backend->has('TransientMemoryBackendTest1'), 'TransientMemoryBackendTest1');
176 $this->assertFalse($backend->has('TransientMemoryBackendTest2'), 'TransientMemoryBackendTest2');
177 $this->assertFalse($backend->has('TransientMemoryBackendTest3'), 'TransientMemoryBackendTest3');
178 }
179 }