[TASK] Re-work/simplify copyright header in PHP files - Part 4
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / PdoBackendTest.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 PDO cache backend
19 *
20 * @author Christian Kuhn <lolli@schwarzbu.ch>
21 */
22 class PdoBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
23
24 /**
25 * Sets up this testcase
26 */
27 public function setUp() {
28 if (!extension_loaded('pdo_sqlite')) {
29 $this->markTestSkipped('pdo_sqlite extension was not available');
30 }
31 }
32
33 /**
34 * @test
35 * @expectedException \TYPO3\CMS\Core\Cache\Exception
36 */
37 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
38 $backend = new \TYPO3\CMS\Core\Cache\Backend\PdoBackend('Testing');
39 $data = 'Some data';
40 $identifier = 'MyIdentifier';
41 $backend->set($identifier, $data);
42 }
43
44 /**
45 * @test
46 */
47 public function itIsPossibleToSetAndCheckExistenceInCache() {
48 $backend = $this->setUpBackend();
49 $data = 'Some data';
50 $identifier = 'MyIdentifier';
51 $backend->set($identifier, $data);
52 $this->assertTrue($backend->has($identifier));
53 }
54
55 /**
56 * @test
57 */
58 public function itIsPossibleToSetAndGetEntry() {
59 $backend = $this->setUpBackend();
60 $data = 'Some data';
61 $identifier = 'MyIdentifier';
62 $backend->set($identifier, $data);
63 $fetchedData = $backend->get($identifier);
64 $this->assertEquals($data, $fetchedData);
65 }
66
67 /**
68 * @test
69 */
70 public function itIsPossibleToRemoveEntryFromCache() {
71 $backend = $this->setUpBackend();
72 $data = 'Some data';
73 $identifier = 'MyIdentifier';
74 $backend->set($identifier, $data);
75 $backend->remove($identifier);
76 $this->assertFalse($backend->has($identifier));
77 }
78
79 /**
80 * @test
81 */
82 public function itIsPossibleToOverwriteAnEntryInTheCache() {
83 $backend = $this->setUpBackend();
84 $data = 'Some data';
85 $identifier = 'MyIdentifier';
86 $backend->set($identifier, $data);
87 $otherData = 'some other data';
88 $backend->set($identifier, $otherData);
89 $fetchedData = $backend->get($identifier);
90 $this->assertEquals($otherData, $fetchedData);
91 }
92
93 /**
94 * @test
95 */
96 public function findIdentifiersByTagFindsSetEntries() {
97 $backend = $this->setUpBackend();
98 $data = 'Some data';
99 $entryIdentifier = 'MyIdentifier';
100 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
101 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
102 $this->assertEquals($entryIdentifier, $retrieved[0]);
103 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
104 $this->assertEquals($entryIdentifier, $retrieved[0]);
105 }
106
107 /**
108 * @test
109 */
110 public function setRemovesTagsFromPreviousSet() {
111 $backend = $this->setUpBackend();
112 $data = 'Some data';
113 $entryIdentifier = 'MyIdentifier';
114 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
115 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag3'));
116 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
117 $this->assertEquals(array(), $retrieved);
118 }
119
120 /**
121 * @test
122 */
123 public function setOverwritesExistingEntryThatExceededItsLifetimeWithNewData() {
124 $backend = $this->setUpBackend();
125 $data1 = 'data1';
126 $entryIdentifier = uniqid('test');
127 $backend->set($entryIdentifier, $data1, array(), 1);
128 $data2 = 'data2';
129 $GLOBALS['EXEC_TIME'] += 2;
130 $backend->set($entryIdentifier, $data2, array(), 10);
131 $this->assertEquals($data2, $backend->get($entryIdentifier));
132 }
133
134 /**
135 * @test
136 */
137 public function hasReturnsFalseIfTheEntryDoesntExist() {
138 $backend = $this->setUpBackend();
139 $identifier = 'NonExistingIdentifier';
140 $this->assertFalse($backend->has($identifier));
141 }
142
143 /**
144 * @test
145 */
146 public function removeReturnsFalseIfTheEntryDoesntExist() {
147 $backend = $this->setUpBackend();
148 $identifier = 'NonExistingIdentifier';
149 $this->assertFalse($backend->remove($identifier));
150 }
151
152 /**
153 * @test
154 */
155 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
156 $backend = $this->setUpBackend();
157 $data = 'some data' . microtime();
158 $backend->set('PdoBackendTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
159 $backend->set('PdoBackendTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
160 $backend->set('PdoBackendTest3', $data, array('UnitTestTag%test'));
161 $backend->flushByTag('UnitTestTag%special');
162 $this->assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
163 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
164 $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
165 }
166
167 /**
168 * @test
169 */
170 public function flushRemovesAllCacheEntries() {
171 $backend = $this->setUpBackend();
172 $data = 'some data' . microtime();
173 $backend->set('PdoBackendTest1', $data);
174 $backend->set('PdoBackendTest2', $data);
175 $backend->set('PdoBackendTest3', $data);
176 $backend->flush();
177 $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
178 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
179 $this->assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
180 }
181
182 /**
183 * @test
184 */
185 public function flushRemovesOnlyOwnEntries() {
186 $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
187 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
188 $thisBackend = $this->setUpBackend();
189 $thisBackend->setCache($thisCache);
190 $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
191 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
192 $thatBackend = $this->setUpBackend();
193 $thatBackend->setCache($thatCache);
194 $thisBackend->set('thisEntry', 'Hello');
195 $thatBackend->set('thatEntry', 'World!');
196 $thatBackend->flush();
197 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
198 $this->assertFalse($thatBackend->has('thatEntry'));
199 }
200
201 /**
202 * @test
203 */
204 public function collectGarbageReallyRemovesAnExpiredCacheEntry() {
205 $backend = $this->setUpBackend();
206 $data = 'some data' . microtime();
207 $entryIdentifier = 'BackendPDORemovalTest';
208 $backend->set($entryIdentifier, $data, array(), 1);
209 $this->assertTrue($backend->has($entryIdentifier));
210 $GLOBALS['EXEC_TIME'] += 2;
211 $backend->collectGarbage();
212 $this->assertFalse($backend->has($entryIdentifier));
213 }
214
215 /**
216 * @test
217 */
218 public function collectGarbageReallyRemovesAllExpiredCacheEntries() {
219 $backend = $this->setUpBackend();
220 $data = 'some data' . microtime();
221 $entryIdentifier = 'BackendPDORemovalTest';
222 $backend->set($entryIdentifier . 'A', $data, array(), NULL);
223 $backend->set($entryIdentifier . 'B', $data, array(), 10);
224 $backend->set($entryIdentifier . 'C', $data, array(), 1);
225 $backend->set($entryIdentifier . 'D', $data, array(), 1);
226 $this->assertTrue($backend->has($entryIdentifier . 'A'));
227 $this->assertTrue($backend->has($entryIdentifier . 'B'));
228 $this->assertTrue($backend->has($entryIdentifier . 'C'));
229 $this->assertTrue($backend->has($entryIdentifier . 'D'));
230 $GLOBALS['EXEC_TIME'] += 2;
231 $backend->collectGarbage();
232 $this->assertTrue($backend->has($entryIdentifier . 'A'));
233 $this->assertTrue($backend->has($entryIdentifier . 'B'));
234 $this->assertFalse($backend->has($entryIdentifier . 'C'));
235 $this->assertFalse($backend->has($entryIdentifier . 'D'));
236 }
237
238 /**
239 * Sets up the PDO backend used for testing
240 *
241 * @return \TYPO3\CMS\Core\Cache\Backend\PdoBackend
242 */
243 protected function setUpBackend() {
244 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
245 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('TestCache'));
246 $backend = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\Backend\\PdoBackend', 'Testing');
247 $backend->setCache($mockCache);
248 $backend->setDataSourceName('sqlite::memory:');
249 $backend->initializeObject();
250 return $backend;
251 }
252
253 }