[TASK] Update copyright year to 2013
[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 * Copyright notice
6 *
7 * (c) 2009-2013 Tobias Burger <tobias_burger@hotmail.com>
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 WinCache cache backend
29 *
30 * @author Tobias Burger <tobias_burger@hotmail.com>
31 */
32 class WincacheBackendTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * Sets up this testcase
36 *
37 * @return void
38 */
39 public function setUp() {
40 if (!extension_loaded('wincache')) {
41 $this->markTestSkipped('WinCache extension was not available');
42 }
43 }
44
45 /**
46 * @test
47 * @expectedException \TYPO3\CMS\Core\Cache\Exception
48 */
49 public function setThrowsExceptionIfNoFrontEndHasBeenSet() {
50 $backend = new \TYPO3\CMS\Core\Cache\Backend\WincacheBackend();
51 $data = 'Some data';
52 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
53 $backend->set($identifier, $data);
54 }
55
56 /**
57 * @test
58 */
59 public function itIsPossibleToSetAndCheckExistenceInCache() {
60 $backend = $this->setUpBackend();
61 $data = 'Some data';
62 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
63 $backend->set($identifier, $data);
64 $inCache = $backend->has($identifier);
65 $this->assertTrue($inCache, 'WinCache backend failed to set and check entry');
66 }
67
68 /**
69 * @test
70 */
71 public function itIsPossibleToSetAndGetEntry() {
72 $backend = $this->setUpBackend();
73 $data = 'Some data';
74 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
75 $backend->set($identifier, $data);
76 $fetchedData = $backend->get($identifier);
77 $this->assertEquals($data, $fetchedData, 'Winache backend failed to set and retrieve data');
78 }
79
80 /**
81 * @test
82 */
83 public function itIsPossibleToRemoveEntryFromCache() {
84 $backend = $this->setUpBackend();
85 $data = 'Some data';
86 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
87 $backend->set($identifier, $data);
88 $backend->remove($identifier);
89 $inCache = $backend->has($identifier);
90 $this->assertFalse($inCache, 'Failed to set and remove data from WinCache backend');
91 }
92
93 /**
94 * @test
95 */
96 public function itIsPossibleToOverwriteAnEntryInTheCache() {
97 $backend = $this->setUpBackend();
98 $data = 'Some data';
99 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
100 $backend->set($identifier, $data);
101 $otherData = 'some other data';
102 $backend->set($identifier, $otherData);
103 $fetchedData = $backend->get($identifier);
104 $this->assertEquals($otherData, $fetchedData, 'WinCache backend failed to overwrite and retrieve data');
105 }
106
107 /**
108 * @test
109 */
110 public function findIdentifiersByTagFindsSetEntries() {
111 $backend = $this->setUpBackend();
112 $data = 'Some data';
113 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
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 $backend = $this->setUpBackend();
126 $data = 'Some data';
127 $identifier = 'MyIdentifier' . md5(uniqid(mt_rand(), TRUE));
128 $backend->set($identifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tagX'));
129 $backend->set($identifier, $data, array('UnitTestTag%tag3'));
130 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tagX');
131 $this->assertEquals(array(), $retrieved, 'Found entry which should no longer exist.');
132 }
133
134 /**
135 * @test
136 */
137 public function hasReturnsFalseIfTheEntryDoesntExist() {
138 $backend = $this->setUpBackend();
139 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
140 $inCache = $backend->has($identifier);
141 $this->assertFalse($inCache, '"has" did not return FALSE when checking on non existing identifier');
142 }
143
144 /**
145 * @test
146 */
147 public function removeReturnsFalseIfTheEntryDoesntExist() {
148 $backend = $this->setUpBackend();
149 $identifier = 'NonExistingIdentifier' . md5(uniqid(mt_rand(), TRUE));
150 $inCache = $backend->remove($identifier);
151 $this->assertFalse($inCache, '"remove" did not return FALSE when checking on non existing identifier');
152 }
153
154 /**
155 * @test
156 */
157 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
158 $backend = $this->setUpBackend();
159 $data = 'some data' . microtime();
160 $backend->set('BackendWincacheTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
161 $backend->set('BackendWincacheTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
162 $backend->set('BackendWincacheTest3', $data, array('UnitTestTag%test'));
163 $backend->flushByTag('UnitTestTag%special');
164 $this->assertTrue($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
165 $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
166 $this->assertTrue($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
167 }
168
169 /**
170 * @test
171 */
172 public function flushRemovesAllCacheEntries() {
173 $backend = $this->setUpBackend();
174 $data = 'some data' . microtime();
175 $backend->set('BackendWincacheTest1', $data);
176 $backend->set('BackendWincacheTest2', $data);
177 $backend->set('BackendWincacheTest3', $data);
178 $backend->flush();
179 $this->assertFalse($backend->has('BackendWincacheTest1'), 'BackendWincacheTest1');
180 $this->assertFalse($backend->has('BackendWincacheTest2'), 'BackendWincacheTest2');
181 $this->assertFalse($backend->has('BackendWincacheTest3'), 'BackendWincacheTest3');
182 }
183
184 /**
185 * @test
186 */
187 public function flushRemovesOnlyOwnEntries() {
188 $thisCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
189 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
190 $thisBackend = new \TYPO3\CMS\Core\Cache\Backend\WincacheBackend();
191 $thisBackend->setCache($thisCache);
192 $thatCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
193 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
194 $thatBackend = new \TYPO3\CMS\Core\Cache\Backend\WincacheBackend();
195 $thatBackend->setCache($thatCache);
196 $thisBackend->set('thisEntry', 'Hello');
197 $thatBackend->set('thatEntry', 'World!');
198 $thatBackend->flush();
199 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
200 $this->assertFalse($thatBackend->has('thatEntry'));
201 }
202
203 /**
204 * Check if we can store ~5 MB of data
205 *
206 * @test
207 */
208 public function largeDataIsStored() {
209 $backend = $this->setUpBackend();
210 $data = str_repeat('abcde', 1024 * 1024);
211 $identifier = 'tooLargeData' . md5(uniqid(mt_rand(), TRUE));
212 $backend->set($identifier, $data);
213 $this->assertTrue($backend->has($identifier));
214 $this->assertEquals($backend->get($identifier), $data);
215 }
216
217 /**
218 * Sets up the WinCache backend used for testing
219 *
220 * @return \TYPO3\CMS\Core\Cache\Backend\WincacheBackend
221 */
222 protected function setUpBackend() {
223 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\FrontendInterface', array(), array(), '', FALSE);
224 $backend = new \TYPO3\CMS\Core\Cache\Backend\WincacheBackend();
225 $backend->setCache($cache);
226 return $backend;
227 }
228
229 }
230
231 ?>