[BUGFIX] Fix typo3 unit tests
[Packages/TYPO3.CMS.git] / tests / Unit / t3lib / cache / frontend / class.t3lib_cache_frontend_variablefrontendTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for the variable cache frontend
27 *
28 * This file is a backport from FLOW3
29 *
30 * @author Ingo Renner <ingo@typo3.org>
31 * @package TYPO3
32 * @subpackage tests
33 */
34 class t3lib_cache_frontend_VariableFrontendTest extends tx_phpunit_testcase {
35
36 /**
37 * @expectedException \InvalidArgumentException
38 * @test
39 */
40 public function setChecksIfTheIdentifierIsValid() {
41 $cache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\StringFrontend', array('isValidEntryIdentifier'), array(), '', FALSE);
42 $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(FALSE));
43 $cache->set('foo', 'bar');
44 }
45
46 /**
47 * @test
48 */
49 public function setPassesSerializedStringToBackend() {
50 $theString = 'Just some value';
51 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
52 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)));
53
54 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
55 $cache->set('VariableCacheTest', $theString);
56 }
57
58 /**
59 * @test
60 */
61 public function setPassesSerializedArrayToBackend() {
62 $theArray = array('Just some value', 'and another one.');
63 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
64 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theArray)));
65
66 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
67 $cache->set('VariableCacheTest', $theArray);
68 }
69
70 /**
71 * @test
72 */
73 public function setPassesLifetimeToBackend() {
74 $theString = 'Just some value';
75 $theLifetime = 1234;
76 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
77 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)), $this->equalTo(array()), $this->equalTo($theLifetime));
78
79 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
80 $cache->set('VariableCacheTest', $theString, array(), $theLifetime);
81 }
82
83 /**
84 * @test
85 */
86 public function setUsesIgBinarySerializeIfAvailable() {
87 if (!extension_loaded('igbinary')) {
88 $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.');
89 }
90
91 $theString = 'Just some value';
92 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
93 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(igbinary_serialize($theString)));
94
95 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
96 $cache->initializeObject();
97 $cache->set('VariableCacheTest', $theString);
98 }
99
100 /**
101 * @test
102 */
103 public function getFetchesStringValueFromBackend() {
104 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
105 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize('Just some value')));
106
107 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
108 $this->assertEquals('Just some value', $cache->get('VariableCacheTest'), 'The returned value was not the expected string.');
109 }
110
111 /**
112 * @test
113 */
114 public function getFetchesArrayValueFromBackend() {
115 $theArray = array('Just some value', 'and another one.');
116 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
117 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize($theArray)));
118
119 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
120 $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
121 }
122
123 /**
124 * @test
125 */
126 public function getFetchesFalseBooleanValueFromBackend() {
127 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
128 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize(FALSE)));
129
130 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
131 $this->assertFalse($cache->get('VariableCacheTest'), 'The returned value was not the FALSE.');
132 }
133
134 /**
135 * @test
136 */
137 public function getUsesIgBinaryIfAvailable() {
138 if (!extension_loaded('igbinary')) {
139 $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.');
140 }
141
142 $theArray = array('Just some value', 'and another one.');
143 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
144 $backend->expects($this->once())->method('get')->will($this->returnValue(igbinary_serialize($theArray)));
145
146 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
147 $cache->initializeObject();
148
149 $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
150 }
151
152 /**
153 * @test
154 */
155 public function hasReturnsResultFromBackend() {
156 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
157 $backend->expects($this->once())->method('has')->with($this->equalTo('VariableCacheTest'))->will($this->returnValue(TRUE));
158
159 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
160 $this->assertTrue($cache->has('VariableCacheTest'), 'has() did not return TRUE.');
161 }
162
163 /**
164 * @test
165 */
166 public function removeCallsBackend() {
167 $cacheIdentifier = 'someCacheIdentifier';
168 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
169
170 $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(TRUE));
171
172 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
173 $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
174 }
175
176 /**
177 * @test
178 * @expectedException \InvalidArgumentException
179 */
180 public function getByTagRejectsInvalidTags() {
181 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\BackendInterface', array(), array(), '', FALSE);
182 $backend->expects($this->never())->method('getByTag');
183
184 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
185 $cache->getByTag('SomeInvalid\Tag');
186 }
187
188 /**
189 * @test
190 */
191 public function getByTagCallsBackend() {
192 $tag = 'sometag';
193 $identifiers = array('one', 'two');
194 $entries = array('one value', 'two value');
195 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
196
197 $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
198 $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(serialize('one value'), serialize('two value')));
199
200 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
201 $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
202 }
203
204 /**
205 * @test
206 */
207 public function getByTagUsesIgBinaryIfAvailable() {
208 if (!extension_loaded('igbinary')) {
209 $this->markTestSkipped('Cannot test igbinary support, because igbinary is not installed.');
210 }
211
212 $tag = 'sometag';
213 $identifiers = array('one', 'two');
214 $entries = array('one value', 'two value');
215 $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'flush', 'flushByTag', 'collectGarbage'), array(), '', FALSE);
216
217 $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
218 $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(igbinary_serialize('one value'), igbinary_serialize('two value')));
219
220 $cache = new TYPO3\CMS\Core\Cache\Frontend\VariableFrontend('VariableFrontend', $backend);
221 $cache->initializeObject();
222 $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
223 }
224 }
225
226 ?>