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