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