da8609e021fe921fb1b0e9056bf683f3ae0af970
[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 * Copyright notice
6 *
7 * (c) 2010-2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for the PDO cache backend
29 *
30 * @author Christian Kuhn <lolli@schwarzbu.ch>
31 */
32 class PdoBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * Sets up this testcase
36 */
37 public function setUp() {
38 if (!extension_loaded('pdo_sqlite')) {
39 $this->markTestSkipped('pdo_sqlite extension was not available');
40 }
41 }
42
43 /**
44 * @test
45 * @expectedException \TYPO3\CMS\Core\Cache\Exception
46 */
47 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
48 $backend = new \TYPO3\CMS\Core\Cache\Backend\PdoBackend('Testing');
49 $data = 'Some data';
50 $identifier = 'MyIdentifier';
51 $backend->set($identifier, $data);
52 }
53
54 /**
55 * @test
56 */
57 public function itIsPossibleToSetAndCheckExistenceInCache() {
58 $backend = $this->setUpBackend();
59 $data = 'Some data';
60 $identifier = 'MyIdentifier';
61 $backend->set($identifier, $data);
62 $this->assertTrue($backend->has($identifier));
63 }
64
65 /**
66 * @test
67 */
68 public function itIsPossibleToSetAndGetEntry() {
69 $backend = $this->setUpBackend();
70 $data = 'Some data';
71 $identifier = 'MyIdentifier';
72 $backend->set($identifier, $data);
73 $fetchedData = $backend->get($identifier);
74 $this->assertEquals($data, $fetchedData);
75 }
76
77 /**
78 * @test
79 */
80 public function itIsPossibleToRemoveEntryFromCache() {
81 $backend = $this->setUpBackend();
82 $data = 'Some data';
83 $identifier = 'MyIdentifier';
84 $backend->set($identifier, $data);
85 $backend->remove($identifier);
86 $this->assertFalse($backend->has($identifier));
87 }
88
89 /**
90 * @test
91 */
92 public function itIsPossibleToOverwriteAnEntryInTheCache() {
93 $backend = $this->setUpBackend();
94 $data = 'Some data';
95 $identifier = 'MyIdentifier';
96 $backend->set($identifier, $data);
97 $otherData = 'some other data';
98 $backend->set($identifier, $otherData);
99 $fetchedData = $backend->get($identifier);
100 $this->assertEquals($otherData, $fetchedData);
101 }
102
103 /**
104 * @test
105 */
106 public function findIdentifiersByTagFindsSetEntries() {
107 $backend = $this->setUpBackend();
108 $data = 'Some data';
109 $entryIdentifier = 'MyIdentifier';
110 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
111 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
112 $this->assertEquals($entryIdentifier, $retrieved[0]);
113 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
114 $this->assertEquals($entryIdentifier, $retrieved[0]);
115 }
116
117 /**
118 * @test
119 */
120 public function setRemovesTagsFromPreviousSet() {
121 $backend = $this->setUpBackend();
122 $data = 'Some data';
123 $entryIdentifier = 'MyIdentifier';
124 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
125 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag3'));
126 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
127 $this->assertEquals(array(), $retrieved);
128 }
129
130 /**
131 * @test
132 */
133 public function setOverwritesExistingEntryThatExceededItsLifetimeWithNewData() {
134 $backend = $this->setUpBackend();
135 $data1 = 'data1';
136 $entryIdentifier = uniqid('test');
137 $backend->set($entryIdentifier, $data1, array(), 1);
138 $data2 = 'data2';
139 $GLOBALS['EXEC_TIME'] += 2;
140 $backend->set($entryIdentifier, $data2, array(), 10);
141 $this->assertEquals($data2, $backend->get($entryIdentifier));
142 }
143
144 /**
145 * @test
146 */
147 public function hasReturnsFalseIfTheEntryDoesntExist() {
148 $backend = $this->setUpBackend();
149 $identifier = 'NonExistingIdentifier';
150 $this->assertFalse($backend->has($identifier));
151 }
152
153 /**
154 * @test
155 */
156 public function removeReturnsFalseIfTheEntryDoesntExist() {
157 $backend = $this->setUpBackend();
158 $identifier = 'NonExistingIdentifier';
159 $this->assertFalse($backend->remove($identifier));
160 }
161
162 /**
163 * @test
164 */
165 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
166 $backend = $this->setUpBackend();
167 $data = 'some data' . microtime();
168 $backend->set('PdoBackendTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
169 $backend->set('PdoBackendTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
170 $backend->set('PdoBackendTest3', $data, array('UnitTestTag%test'));
171 $backend->flushByTag('UnitTestTag%special');
172 $this->assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
173 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
174 $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
175 }
176
177 /**
178 * @test
179 */
180 public function flushRemovesAllCacheEntries() {
181 $backend = $this->setUpBackend();
182 $data = 'some data' . microtime();
183 $backend->set('PdoBackendTest1', $data);
184 $backend->set('PdoBackendTest2', $data);
185 $backend->set('PdoBackendTest3', $data);
186 $backend->flush();
187 $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
188 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
189 $this->assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
190 }
191
192 /**
193 * @test
194 */
195 public function flushRemovesOnlyOwnEntries() {
196 $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
197 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
198 $thisBackend = $this->setUpBackend();
199 $thisBackend->setCache($thisCache);
200 $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
201 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
202 $thatBackend = $this->setUpBackend();
203 $thatBackend->setCache($thatCache);
204 $thisBackend->set('thisEntry', 'Hello');
205 $thatBackend->set('thatEntry', 'World!');
206 $thatBackend->flush();
207 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
208 $this->assertFalse($thatBackend->has('thatEntry'));
209 }
210
211 /**
212 * @test
213 */
214 public function collectGarbageReallyRemovesAnExpiredCacheEntry() {
215 $backend = $this->setUpBackend();
216 $data = 'some data' . microtime();
217 $entryIdentifier = 'BackendPDORemovalTest';
218 $backend->set($entryIdentifier, $data, array(), 1);
219 $this->assertTrue($backend->has($entryIdentifier));
220 $GLOBALS['EXEC_TIME'] += 2;
221 $backend->collectGarbage();
222 $this->assertFalse($backend->has($entryIdentifier));
223 }
224
225 /**
226 * @test
227 */
228 public function collectGarbageReallyRemovesAllExpiredCacheEntries() {
229 $backend = $this->setUpBackend();
230 $data = 'some data' . microtime();
231 $entryIdentifier = 'BackendPDORemovalTest';
232 $backend->set($entryIdentifier . 'A', $data, array(), NULL);
233 $backend->set($entryIdentifier . 'B', $data, array(), 10);
234 $backend->set($entryIdentifier . 'C', $data, array(), 1);
235 $backend->set($entryIdentifier . 'D', $data, array(), 1);
236 $this->assertTrue($backend->has($entryIdentifier . 'A'));
237 $this->assertTrue($backend->has($entryIdentifier . 'B'));
238 $this->assertTrue($backend->has($entryIdentifier . 'C'));
239 $this->assertTrue($backend->has($entryIdentifier . 'D'));
240 $GLOBALS['EXEC_TIME'] += 2;
241 $backend->collectGarbage();
242 $this->assertTrue($backend->has($entryIdentifier . 'A'));
243 $this->assertTrue($backend->has($entryIdentifier . 'B'));
244 $this->assertFalse($backend->has($entryIdentifier . 'C'));
245 $this->assertFalse($backend->has($entryIdentifier . 'D'));
246 }
247
248 /**
249 * Sets up the PDO backend used for testing
250 *
251 * @return \TYPO3\CMS\Core\Cache\Backend\PdoBackend
252 */
253 protected function setUpBackend() {
254 $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
255 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('TestCache'));
256 $backend = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\Backend\\PdoBackend', 'Testing');
257 $backend->setCache($mockCache);
258 $backend->setDataSourceName('sqlite::memory:');
259 $backend->initializeObject();
260 return $backend;
261 }
262
263 }