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