[BUGFIX] Add missing namespace parts
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / WincacheBackendTest.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 use TYPO3\CMS\Core\Cache\Backend\WincacheBackend;
18
19 /**
20 * Testcase for the WinCache cache backend
21 */
22 class WincacheBackendTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * Sets up this testcase
26 *
27 * @return void
28 */
29 protected function setUp()
30 {
31 if (!extension_loaded('wincache')) {
32 $this->markTestSkipped('WinCache extension was not available');
33 }
34 }
35
36 /**
37 * @test
38 */
39 public function setThrowsExceptionIfNoFrontEndHasBeenSet()
40 {
41 $this->expectException(\TYPO3\CMS\Core\Cache\Exception::class);
42 //@todo Add exception code with wincache extension
43
44 $backend = new WincacheBackend('Testing');
45 $data = 'Some data';
46 $identifier = $this->getUniqueId('MyIdentifier');
47 $backend->set($identifier, $data);
48 }
49
50 /**
51 * @test
52 */
53 public function itIsPossibleToSetAndCheckExistenceInCache()
54 {
55 $backend = $this->setUpBackend();
56 $data = 'Some data';
57 $identifier = $this->getUniqueId('MyIdentifier');
58 $backend->set($identifier, $data);
59 $inCache = $backend->has($identifier);
60 $this->assertTrue($inCache, 'WinCache backend failed to set and check entry');
61 }
62
63 /**
64 * @test
65 */
66 public function itIsPossibleToSetAndGetEntry()
67 {
68 $backend = $this->setUpBackend();
69 $data = 'Some data';
70 $identifier = $this->getUniqueId('MyIdentifier');
71 $backend->set($identifier, $data);
72 $fetchedData = $backend->get($identifier);
73 $this->assertEquals($data, $fetchedData, 'Winache backend failed to set and retrieve data');
74 }
75
76 /**
77 * @test
78 */
79 public function itIsPossibleToRemoveEntryFromCache()
80 {
81 $backend = $this->setUpBackend();
82 $data = 'Some data';
83 $identifier = $this->getUniqueId('MyIdentifier');
84 $backend->set($identifier, $data);
85 $backend->remove($identifier);
86 $inCache = $backend->has($identifier);
87 $this->assertFalse($inCache, 'Failed to set and remove data from WinCache backend');
88 }
89
90 /**
91 * @test
92 */
93 public function itIsPossibleToOverwriteAnEntryInTheCache()
94 {
95 $backend = $this->setUpBackend();
96 $data = 'Some data';
97 $identifier = $this->getUniqueId('MyIdentifier');
98 $backend->set($identifier, $data);
99 $otherData = 'some other data';
100 $backend->set($identifier, $otherData);
101 $fetchedData = $backend->get($identifier);
102 $this->assertEquals($otherData, $fetchedData, 'WinCache backend failed to overwrite and retrieve data');
103 }
104
105 /**
106 * @test
107 */
108 public function findIdentifiersByTagFindsSetEntries()
109 {
110 $backend = $this->setUpBackend();
111 $data = 'Some data';
112 $identifier = $this->getUniqueId('MyIdentifier');
113 $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
114 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
115 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
116 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
117 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
118 }
119
120 /**
121 * @test
122 */
123 public function setRemovesTagsFromPreviousSet()
124 {
125 $backend = $this->setUpBackend();
126 $data = 'Some data';
127 $identifier = $this->getUniqueId('MyIdentifier');
128 $backend->set($identifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tagX']);
129 $backend->set($identifier, $data, ['UnitTestTag%tag3']);
130 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
131 $this->assertEquals([], $retrieved, 'Found entry which should no longer exist.');
132 }
133
134 /**
135 * @test
136 */
137 public function hasReturnsFalseIfTheEntryDoesntExist()
138 {
139 $backend = $this->setUpBackend();
140 $identifier = $this->getUniqueId('NonExistingIdentifier');
141 $inCache = $backend->has($identifier);
142 $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
143 }
144
145 /**
146 * @test
147 */
148 public function removeReturnsFalseIfTheEntryDoesntExist()
149 {
150 $backend = $this->setUpBackend();
151 $identifier = $this->getUniqueId('NonExistingIdentifier');
152 $inCache = $backend->remove($identifier);
153 $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
154 }
155
156 /**
157 * @test
158 */
159 public function flushByTagRemovesCacheEntriesWithSpecifiedTag()
160 {
161 $backend = $this->setUpBackend();
162 $data = 'some data' . microtime();
163 $backend->set('BackendWincacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
164 $backend->set('BackendWincacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
165 $backend->set('BackendWincacheTest3', $data, ['UnitTestTag%test']);
166 $backend->flushByTag('UnitTestTag%special');
167 $this->assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
168 $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
169 $this->assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
170 }
171
172 /**
173 * @test
174 */
175 public function flushByTagsRemovesCacheEntriesWithSpecifiedTags()
176 {
177 $backend = $this->setUpBackend();
178 $data = 'some data' . microtime();
179 $backend->set('BackendWincacheTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
180 $backend->set('BackendWincacheTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
181 $backend->set('BackendWincacheTest3', $data, ['UnitTestTag%test']);
182 $backend->flushByTag('UnitTestTag%special', 'UnitTestTag%boring');
183 $this->assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
184 $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
185 $this->assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
186 }
187
188 /**
189 * @test
190 */
191 public function flushRemovesAllCacheEntries()
192 {
193 $backend = $this->setUpBackend();
194 $data = 'some data' . microtime();
195 $backend->set('BackendWincacheTest1', $data);
196 $backend->set('BackendWincacheTest2', $data);
197 $backend->set('BackendWincacheTest3', $data);
198 $backend->flush();
199 $this->assertFalse($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
200 $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
201 $this->assertFalse($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
202 }
203
204 /**
205 * @test
206 */
207 public function flushRemovesOnlyOwnEntries()
208 {
209 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $thisCache */
210 $thisCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
211 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
212 $thisBackend = new WincacheBackend('Testing');
213 $thisBackend->setCache($thisCache);
214
215 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $thatCache */
216 $thatCache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
217 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
218 $thatBackend = new WincacheBackend('Testing');
219 $thatBackend->setCache($thatCache);
220 $thisBackend->set('thisEntry', 'Hello');
221 $thatBackend->set('thatEntry', 'World!');
222 $thatBackend->flush();
223 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
224 $this->assertFalse($thatBackend->has('thatEntry'));
225 }
226
227 /**
228 * Check if we can store ~5 MB of data
229 *
230 * @test
231 */
232 public function largeDataIsStored()
233 {
234 $backend = $this->setUpBackend();
235 $data = str_repeat('abcde', 1024 * 1024);
236 $identifier = $this->getUniqueId('tooLargeData');
237 $backend->set($identifier, $data);
238 $this->assertTrue($backend->has($identifier));
239 $this->assertEquals($backend->get($identifier), $data);
240 }
241
242 /**
243 * @test
244 */
245 public function setTagsOnlyOnceToIdentifier()
246 {
247 $identifier = $this->getUniqueId('MyIdentifier');
248 $tags = ['UnitTestTag%test', 'UnitTestTag%boring'];
249
250 $backend = $this->setUpBackend(true);
251 $backend->_call('addIdentifierToTags', $identifier, $tags);
252 $this->assertSame(
253 $tags,
254 $backend->_call('findTagsByIdentifier', $identifier)
255 );
256
257 $backend->_call('addIdentifierToTags', $identifier, $tags);
258 $this->assertSame(
259 $tags,
260 $backend->_call('findTagsByIdentifier', $identifier)
261 );
262 }
263
264 /**
265 * Sets up the WinCache backend used for testing
266 *
267 * @param bool $accessible TRUE if backend should be encapsulated in accessible proxy otherwise FALSE.
268 * @return \TYPO3\Components\TestingFramework\Core\AccessibleObjectInterface|WincacheBackend
269 */
270 protected function setUpBackend($accessible = false)
271 {
272 /** @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache */
273 $cache = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
274 if ($accessible) {
275 $accessibleClassName = $this->buildAccessibleProxy(WincacheBackend::class);
276 $backend = new $accessibleClassName('Testing');
277 } else {
278 $backend = new WincacheBackend('Testing');
279 }
280 $backend->setCache($cache);
281 return $backend;
282 }
283 }