[TASK] Re-work/simplify copyright header in PHP files - Part 4
[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 /**
18 * Test case
19 *
20 * @author Philipp Gampe <philipp.gampe@typo3.org>
21 */
22 class XcacheBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * Sets up this testcase
26 *
27 * @return void
28 */
29 public function setUp() {
30 if (!extension_loaded('xcache')) {
31 $this->markTestSkipped('xcache extension was not available');
32 }
33 if (php_sapi_name() === 'cli') {
34 $this->markTestSkipped('XCache is not supported in CLI mode.');
35 }
36 }
37
38 /**
39 * @test
40 * @expectedException \TYPO3\CMS\Core\Cache\Exception
41 */
42 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
43 $backend = new \TYPO3\CMS\Core\Cache\Backend\XcacheBackend('Testing');
44 $data = 'Some data';
45 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
46 $backend->set($identifier, $data);
47 }
48
49 /**
50 * @test
51 */
52 public function itIsPossibleToSetAndCheckExistenceInCache() {
53 $backend = $this->setUpBackend();
54 $data = 'Some data';
55 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
56 $backend->set($identifier, $data);
57 $inCache = $backend->has($identifier);
58 $this->assertTrue($inCache, 'xcache backend failed to set and check entry');
59 }
60
61 /**
62 * @test
63 */
64 public function itIsPossibleToSetAndGetEntry() {
65 $backend = $this->setUpBackend();
66 $data = 'Some data';
67 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
68 $backend->set($identifier, $data);
69 $fetchedData = $backend->get($identifier);
70 $this->assertEquals($data, $fetchedData, 'xcache backend failed to set and retrieve data');
71 }
72
73 /**
74 * @test
75 */
76 public function itIsPossibleToRemoveEntryFromCache() {
77 $backend = $this->setUpBackend();
78 $data = 'Some data';
79 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
80 $backend->set($identifier, $data);
81 $backend->remove($identifier);
82 $inCache = $backend->has($identifier);
83 $this->assertFalse($inCache, 'Failed to set and remove data from xcache backend');
84 }
85
86 /**
87 * @test
88 */
89 public function itIsPossibleToOverwriteAnEntryInTheCache() {
90 $backend = $this->setUpBackend();
91 $data = 'Some data';
92 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
93 $backend->set($identifier, $data);
94 $otherData = 'some other data';
95 $backend->set($identifier, $otherData);
96 $fetchedData = $backend->get($identifier);
97 $this->assertEquals($otherData, $fetchedData, 'xcache backend failed to overwrite and retrieve data');
98 }
99
100 /**
101 * @test
102 */
103 public function findIdentifiersByTagFindsSetEntries() {
104 $backend = $this->setUpBackend();
105 $data = 'Some data';
106 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
107 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
108 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
109 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
110 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
111 $this->assertEquals($identifier, $retrieved[0], 'Could not retrieve expected entry by tag.');
112 }
113
114 /**
115 * @test
116 */
117 public function setRemovesTagsFromPreviousSet() {
118 $backend = $this->setUpBackend();
119 $data = 'Some data';
120 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
121 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
122 $backend->set($identifier, $data, array('UnitTestTag%tag3'));
123 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
124 $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
125 }
126
127 /**
128 * @test
129 */
130 public function hasReturnsFalseIfTheEntryDoesntExist() {
131 $backend = $this->setUpBackend();
132 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
133 $inCache = $backend->has($identifier);
134 $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
135 }
136
137 /**
138 * @test
139 */
140 public function removeReturnsFalseIfTheEntryDoesntExist() {
141 $backend = $this->setUpBackend();
142 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
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 $backend = $this->setUpBackend();
152 $data = 'some data' . microtime();
153 $backend->set('BackendXcacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
154 $backend->set('BackendXcacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
155 $backend->set('BackendXcacheTest3', $data, array('UnitTestTag%test'));
156 $backend->flushByTag('UnitTestTag%special');
157 $this->assertTrue($backend->has('BackendXcacheTest1'), 'BackendXcacheTest1');
158 $this->assertFalse($backend->has('BackendXcacheTest2'), 'BackendXcacheTest2');
159 $this->assertTrue($backend->has('BackendXcacheTest3'), 'BackendXcacheTest3');
160 }
161
162 /**
163 * @test
164 */
165 public function flushRemovesAllCacheEntries() {
166 $backend = $this->setUpBackend();
167 $data = 'some data' . microtime();
168 $backend->set('BackendXcacheTest1', $data);
169 $backend->set('BackendXcacheTest2', $data);
170 $backend->set('BackendXcacheTest3', $data);
171 $backend->flush();
172 $this->assertFalse($backend->has('BackendXcacheTest1'), 'BackendXcacheTest1');
173 $this->assertFalse($backend->has('BackendXcacheTest2'), 'BackendXcacheTest2');
174 $this->assertFalse($backend->has('BackendXcacheTest3'), 'BackendXcacheTest3');
175 }
176
177 /**
178 * @test
179 */
180 public function flushRemovesOnlyOwnEntries() {
181 $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
182 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
183 $thisBackend = new \TYPO3\CMS\Core\Cache\Backend\XcacheBackend('Testing');
184 $thisBackend->setCache($thisCache);
185 $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
186 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
187 $thatBackend = new \TYPO3\CMS\Core\Cache\Backend\XcacheBackend('Testing');
188 $thatBackend->setCache($thatCache);
189 $thisBackend->set('thisEntry', 'Hello');
190 $thatBackend->set('thatEntry', 'World!');
191 $thatBackend->flush();
192 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
193 $this->assertFalse($thatBackend->has('thatEntry'));
194 }
195
196 /**
197 * Check if we can store ~5 MB of data
198 *
199 * @test
200 */
201 public function largeDataIsStored() {
202 $backend = $this->setUpBackend();
203 $data = str_repeat('abcde', 1024 * 1024);
204 $identifier = 'tooLargeData' . md5(uniqid(mt_rand(), TRUE));
205 $backend->set($identifier, $data);
206 $this->assertTrue($backend->has($identifier));
207 $this->assertEquals($backend->get($identifier), $data);
208 }
209
210 /**
211 * Sets up the xcache backend used for testing
212 *
213 * @return \TYPO3\CMS\Core\Cache\Backend\XcacheBackend
214 */
215 protected function setUpBackend() {
216 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
217 $backend = new \TYPO3\CMS\Core\Cache\Backend\XcacheBackend('Testing');
218 $backend->setCache($cache);
219 return $backend;
220 }
221
222 }