fixed a typo in the class name
[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 * @test
40 * @author Robert Lemke <robert@typo3.org>
41 * @author Karsten Dambekalns <karsten@typo3.org>
42 * @author Ingo Renner <ingo@typo3.org>
43 */
44 public function setRejectsInvalidIdentifiers() {
45 $theString = 'Just some value';
46 $backend = $this->getMock('t3lib_cache_backend_Backend', array(), array(), '', FALSE);
47 $backend->expects($this->never())->method('set');
48
49 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
50
51 foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#', 'some&') as $entryIdentifier) {
52 try {
53 $cache->set($entryIdentifier, $theString);
54 $this->fail('set() did no reject the entry identifier "' . $entryIdentifier . '".');
55 } catch (InvalidArgumentException $exception) {
56 }
57 }
58 }
59
60 /**
61 * @test
62 * @author Robert Lemke <robert@typo3.org>
63 * @author Ingo Renner <ingo@typo3.org>
64 */
65 public function setPassesSerializedStringToBackend() {
66 $theString = 'Just some value';
67 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
68 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theString)));
69
70 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
71 $cache->set('VariableCacheTest', $theString);
72 }
73
74 /**
75 * @test
76 * @author Robert Lemke <robert@typo3.org>
77 * @author Ingo Renner <ingo@typo3.org>
78 */
79 public function setPassesSerializedArrayToBackend() {
80 $theArray = array('Just some value', 'and another one.');
81 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
82 $backend->expects($this->once())->method('set')->with($this->equalTo('VariableCacheTest'), $this->equalTo(serialize($theArray)));
83
84 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
85 $cache->set('VariableCacheTest', $theArray);
86 }
87
88 /**
89 * @test
90 * @author Robert Lemke <robert@typo3.org>
91 * @author Ingo Renner <ingo@typo3.org>
92 */
93 public function getFetchesStringValueFromBackend() {
94 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
95 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize('Just some value')));
96
97 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
98 $this->assertEquals('Just some value', $cache->get('VariableCacheTest'), 'The returned value was not the expected string.');
99 }
100
101 /**
102 * @test
103 * @author Robert Lemke <robert@typo3.org>
104 * @author Ingo Renner <ingo@typo3.org>
105 */
106 public function getFetchesArrayValueFromBackend() {
107 $theArray = array('Just some value', 'and another one.');
108 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
109 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize($theArray)));
110
111 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
112 $this->assertEquals($theArray, $cache->get('VariableCacheTest'), 'The returned value was not the expected unserialized array.');
113 }
114
115 /**
116 * @test
117 * @author Robert Lemke <robert@typo3.org>
118 * @author Ingo Renner <ingo@typo3.org>
119 */
120 public function getFetchesFalseBooleanValueFromBackend() {
121 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
122 $backend->expects($this->once())->method('get')->will($this->returnValue(serialize(FALSE)));
123
124 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
125 $this->assertFalse($cache->get('VariableCacheTest'), 'The returned value was not the FALSE.');
126 }
127
128 /**
129 * @test
130 * @author Robert Lemke <robert@typo3.org>
131 * @author Ingo Renner <ingo@typo3.org>
132 */
133 public function hasReturnsResultFromBackend() {
134 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
135 $backend->expects($this->once())->method('has')->with($this->equalTo('VariableCacheTest'))->will($this->returnValue(TRUE));
136
137 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
138 $this->assertTrue($cache->has('VariableCacheTest'), 'has() did not return TRUE.');
139 }
140
141 /**
142 * @test
143 * @author Sebastian Kurfuerst <sebastian@typo3.org>
144 * @author Ingo Renner <ingo@typo3.org>
145 */
146 public function removeCallsBackend() {
147 $cacheIdentifier = 'someCacheIdentifier';
148 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
149
150 $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(TRUE));
151
152 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
153 $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
154 }
155
156 /**
157 * @test
158 * @expectedException InvalidArgumentException
159 * @author Karsten Dambekalns <karsten@typo3.org>
160 * @author Ingo Renner <ingo@typo3.org>
161 */
162 public function getByTagRejectsInvalidTags() {
163 $backend = $this->getMock('t3lib_cache_backend_Backend', array(), array(), '', FALSE);
164 $backend->expects($this->never())->method('getByTag');
165
166 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
167 $cache->getByTag('SomeInvalid\Tag');
168 }
169
170 /**
171 * @test
172 * @author Karsten Dambekalns <karsten@typo3.org>
173 * @author Ingo Renner <ingo@typo3.org>
174 */
175 public function getByTagCallsBackend() {
176 $tag = 'sometag';
177 $identifiers = array('one', 'two');
178 $entries = array('one value', 'two value');
179 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
180
181 $backend->expects($this->once())->method('findIdentifiersByTag')->with($this->equalTo($tag))->will($this->returnValue($identifiers));
182 $backend->expects($this->exactly(2))->method('get')->will($this->onConsecutiveCalls(serialize('one value'), serialize('two value')));
183
184 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
185 $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');
186 }
187
188 }
189
190 ?>