2 /***************************************************************
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
27 * Testcase for the variable cache frontend
29 * This file is a backport from FLOW3
31 * @author Ingo Renner <ingo@typo3.org>
36 class t3lib_cache_frontend_VariableFrontendTestCase
extends tx_phpunit_testcase
{
39 * @expectedException InvalidArgumentException
41 * @author Robert Lemke <robert@typo3.org>
42 * @author Ingo Renner <ingo@typo3.org>
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');
52 * @author Robert Lemke <robert@typo3.org>
53 * @author Ingo Renner <ingo@typo3.org>
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)));
60 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
61 $cache->set('VariableCacheTest', $theString);
66 * @author Robert Lemke <robert@typo3.org>
67 * @author Ingo Renner <ingo@typo3.org>
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)));
74 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
75 $cache->set('VariableCacheTest', $theArray);
80 * @author Karsten Dambekalns <karsten@typo3.org>
81 * @author Ingo Renner <ingo@typo3.org>
83 public function setPassesLifetimeToBackend() {
84 $theString = 'Just some value';
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));
89 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
90 $cache->set('VariableCacheTest', $theString, array(), $theLifetime);
95 * @author Robert Lemke <robert@typo3.org>
96 * @author Ingo Renner <ingo@typo3.org>
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')));
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.');
108 * @author Robert Lemke <robert@typo3.org>
109 * @author Ingo Renner <ingo@typo3.org>
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)));
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.');
122 * @author Robert Lemke <robert@typo3.org>
123 * @author Ingo Renner <ingo@typo3.org>
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)));
129 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
130 $this->assertFalse($cache->get('VariableCacheTest'), 'The returned value was not the FALSE.');
135 * @author Robert Lemke <robert@typo3.org>
136 * @author Ingo Renner <ingo@typo3.org>
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));
142 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
143 $this->assertTrue($cache->has('VariableCacheTest'), 'has() did not return TRUE.');
148 * @author Sebastian Kurfuerst <sebastian@typo3.org>
149 * @author Ingo Renner <ingo@typo3.org>
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);
155 $backend->expects($this->once())->method('remove')->with($this->equalTo($cacheIdentifier))->will($this->returnValue(TRUE));
157 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
158 $this->assertTrue($cache->remove($cacheIdentifier), 'remove() did not return TRUE');
163 * @expectedException InvalidArgumentException
164 * @author Karsten Dambekalns <karsten@typo3.org>
165 * @author Ingo Renner <ingo@typo3.org>
167 public function getByTagRejectsInvalidTags() {
168 $backend = $this->getMock('t3lib_cache_backend_Backend', array(), array(), '', FALSE);
169 $backend->expects($this->never())->method('getByTag');
171 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
172 $cache->getByTag('SomeInvalid\Tag');
177 * @author Karsten Dambekalns <karsten@typo3.org>
178 * @author Ingo Renner <ingo@typo3.org>
180 public function getByTagCallsBackend() {
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);
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')));
189 $cache = new t3lib_cache_frontend_VariableFrontend('VariableFrontend', $backend);
190 $this->assertEquals($entries, $cache->getByTag($tag), 'Did not receive the expected entries');