Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / frontend / t3lib_cache_frontend_variablefrontendtestcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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 /**
27 * Testcase for the variable cache frontend
28 *
29 * This file is a backport from FLOW3
30 *
31 * @author Ingo Renner <ingo@typo3.org>
32 * @package TYPO3
33 * @subpackage tests
34 * @version $Id$
35 */
36 class t3lib_cache_frontend_VariableFrontendTestCase extends tx_phpunit_testcase {
37
38 /**
39 * @expectedException InvalidArgumentException
40 * @test
41 * @author Robert Lemke <robert@typo3.org>
42 * @author Ingo Renner <ingo@typo3.org>
43 */
44 public function setChecksIfTheIdentifierIsValid() {
45 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('isValidEntryIdentifier'), array(), '', FALSE);
46 $cache->expects($this->once())->method('isValidEntryIdentifier')->with('foo')->will($this->returnValue(FALSE));
47 $cache->set('foo', 'bar');
48 }
49
50 /**
51 * @test
52 * @author Robert Lemke <robert@typo3.org>
53 * @author Ingo Renner <ingo@typo3.org>
54 */
55 public function setPassesSerializedStringToBackend() {
56 $theString = 'Just some value';
57 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
58 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)));
59
60 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
61 $cache->set('VariableCacheTest', $theString);
62 }
63
64 /**
65 * @test
66 * @author Robert Lemke <robert@typo3.org>
67 * @author Ingo Renner <ingo@typo3.org>
68 */
69 public function setPassesSerializedArrayToBackend() {
70 $theArray = array('Just some value', 'and another one.');
71 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
72 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theArray)));
73
74 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
75 $cache->set('VariableCacheTest', $theArray);
76 }
77
78 /**
79 * @test
80 * @author Karsten Dambekalns <karsten@typo3.org>
81 * @author Ingo Renner <ingo@typo3.org>
82 */
83 public function setPassesLifetimeToBackend() {
84 $theString = 'Just some value';
85 $theLifetime = 1234;
86 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
87 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)), $this->equalTo(array()), $this->equalTo($theLifetime));
88
89 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
90 $cache->set('VariableCacheTest', $theString, array(), $theLifetime);
91 }
92
93 /**
94 * @test
95 * @author Robert Lemke <robert@typo3.org>
96 * @author Ingo Renner <ingo@typo3.org>
97 */
98 public function getFetchesStringValueFromBackend() {
99 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
100 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize('Just some value')));
101
102 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
103 $this->assertEquals('Just some value', $cache->get('VariableCacheTest'), 'The returned value was not the expected string.');
104 }
105
106 /**
107 * @test
108 * @author Robert Lemke <robert@typo3.org>
109 * @author Ingo Renner <ingo@typo3.org>
110 */
111 public function getFetchesArrayValueFromBackend() {
112 $theArray = array('Just some value', 'and another one.');
113 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
114 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize($theArray)));
115
116 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
117 $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
118 }
119
120 /**
121 * @test
122 * @author Robert Lemke <robert@typo3.org>
123 * @author Ingo Renner <ingo@typo3.org>
124 */
125 public function getFetchesFalseBooleanValueFromBackend() {
126 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
127 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize(FALSE)));
128
129 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
130 $this->assertFalse($cache->get('VariableCacheTest'), 'The returned value was not the FALSE.');
131 }
132
133 /**
134 * @test
135 * @author Robert Lemke <robert@typo3.org>
136 * @author Ingo Renner <ingo@typo3.org>
137 */
138 public function hasReturnsResultFromBackend() {
139 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
140 $backend->expects($this->once())->method('has')->with($this->equalTo('VariableCacheTest'))->will($this->returnValue(TRUE));
141
142 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
143 $this->assertTrue($cache->has('VariableCacheTest'), 'has() did not return TRUE.');
144 }
145
146 /**
147 * @test
148 * @author Sebastian Kurfuerst <sebastian@typo3.org>
149 * @author Ingo Renner <ingo@typo3.org>
150 */
151 public function removeCallsBackend() {
152 $cacheIdentifier = 'someCacheIdentifier';
153 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
154
155 $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(TRUE));
156
157 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
158 $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
159 }
160
161 /**
162 * @test
163 * @expectedException InvalidArgumentException
164 * @author Karsten Dambekalns <karsten@typo3.org>
165 * @author Ingo Renner <ingo@typo3.org>
166 */
167 public function getByTagRejectsInvalidTags() {
168 $backend = $this->getMock('t3lib_cache_backend_Backend', array(), array(), '', FALSE);
169 $backend->expects($this->never())->method('getByTag');
170
171 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
172 $cache->getByTag('SomeInvalid\Tag');
173 }
174
175 /**
176 * @test
177 * @author Karsten Dambekalns <karsten@typo3.org>
178 * @author Ingo Renner <ingo@typo3.org>
179 */
180 public function getByTagCallsBackend() {
181 $tag = 'sometag';
182 $identifiers = array('one', 'two');
183 $entries = array('one value', 'two value');
184 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
185
186 $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
187 $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(serialize('one value'), serialize('two value')));
188
189 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
190 $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
191 }
192
193 }
194
195 ?>