Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / frontend / t3lib_cache_frontend_abstractfrontendtestcase.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 abstract 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_AbstractFrontendTestCase extends tx_phpunit_testcase {
39
40 /**
41 * @test
42 * @author Robert Lemke <robert@typo3.org>
43 * @author Ingo Renner <ingo@typo3.org>
44 */
45 public function theConstructorAcceptsValidIdentifiers() {
46 $mockBackend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
47 foreach (array('x', 'someValue', '123fivesixseveneight', 'some&', 'ab_cd%', rawurlencode('package://some/äöü$&% sadf'), str_repeat('x', 250)) as $identifier) {
48 $abstractCache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'), array($identifier, $mockBackend));
49 }
50 }
51
52 /**
53 * @test
54 * @author Robert Lemke <robert@typo3.org>
55 * @author Ingo Renner <ingo@typo3.org>
56 */
57 public function theConstructorRejectsInvalidIdentifiers() {
58 $mockBackend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
59 foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#') as $identifier) {
60 try {
61 $abstractCache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag', 'flush', 'flushByTag', 'collectGarbage'), array($identifier, $mockBackend));
62 $this->fail('Identifier "' . $identifier . '" was not rejected.');
63 } catch (InvalidArgumentException $exception) {
64 }
65 }
66 }
67
68 /**
69 * @test
70 * @author Karsten Dambekalns <karsten@typo3.org>
71 * @author Ingo Renner <ingo@typo3.org>
72 */
73 public function flushCallsBackend() {
74 $identifier = 'someCacheIdentifier';
75 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
76 $backend->expects($this->once())->method('flush');
77
78 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
79 $cache->flush();
80 }
81
82
83 /**
84 * @test
85 * @expectedException InvalidArgumentException
86 * @author Robert Lemke <robert@typo3.org>
87 * @author Ingo Renner <ingo@typo3.org>
88 */
89 public function flushByTagRejectsInvalidTags() {
90 $identifier = 'someCacheIdentifier';
91 $backend = $this->getMock('t3lib_cache_backend_Backend', array(), array(), '', FALSE);
92 $backend->expects($this->never())->method('flushByTag');
93
94 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
95 $cache->flushByTag('SomeInvalid\Tag');
96 }
97
98 /**
99 * @test
100 * @author Karsten Dambekalns <karsten@typo3.org>
101 * @author Ingo Renner <ingo@typo3.org>
102 */
103 public function flushByTagCallsBackend() {
104 $tag = 'sometag';
105 $identifier = 'someCacheIdentifier';
106 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
107 $backend->expects($this->once())->method('flushByTag')->with($tag);
108
109 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
110 $cache->flushByTag($tag);
111 }
112
113 /**
114 * @test
115 * @author Karsten Dambekalns <karsten@typo3.org>
116 * @author Ingo Renner <ingo@typo3.org>
117 */
118 public function collectGarbageCallsBackend() {
119 $identifier = 'someCacheIdentifier';
120 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
121 $backend->expects($this->once())->method('collectGarbage');
122
123 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
124 $cache->collectGarbage();
125 }
126
127 /**
128 * @test
129 * @author Robert Lemke <robert@typo3.org>
130 * @author Ingo Renner <ingo@typo3.org>
131 */
132 public function getClassTagRendersTagWhichCanBeUsedToTagACacheEntryWithACertainClass() {
133 $identifier = 'someCacheIdentifier';
134 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array('get', 'set', 'has', 'remove', 'findIdentifiersByTag', 'findIdentifiersByTags', 'flush', 'flushByTag', 'flushByTags', 'collectGarbage'), array(), '', FALSE);
135
136 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
137 $this->assertEquals('%CLASS%F3_Foo_Bar_Baz', $cache->getClassTag('F3\Foo\Bar\Baz'));
138 }
139
140 /**
141 * @test
142 * @author Robert Lemke <robert@typo3.org>
143 * @author Ingo Renner <ingo@typo3.org>
144 */
145 public function invalidEntryIdentifiersAreRecognizedAsInvalid() {
146 $identifier = 'someCacheIdentifier';
147 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
148 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
149 foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#') as $entryIdentifier) {
150 $this->assertFalse($cache->isValidEntryIdentifier($entryIdentifier), 'Invalid identifier "' . $entryIdentifier . '" was not rejected.');
151 }
152 }
153
154 /**
155 * @test
156 * @author Robert Lemke <robert@typo3.org>
157 * @author Ingo Renner <ingo@typo3.org>
158 */
159 public function validEntryIdentifiersAreRecognizedAsValid() {
160 $identifier = 'someCacheIdentifier';
161 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
162 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
163 foreach (array('_', 'abcdef', 'foo', 'bar123', '3some', '_bl_a', 'some&', 'one%TWO', str_repeat('x', 250)) as $entryIdentifier) {
164 $this->assertTrue($cache->isValidEntryIdentifier($entryIdentifier), 'Valid identifier "' . $entryIdentifier . '" was not accepted.');
165 }
166 }
167
168 /**
169 * @test
170 * @author Robert Lemke <robert@typo3.org>
171 * @author Ingo Renner <ingo@typo3.org>
172 */
173 public function invalidTagsAreRecognizedAsInvalid() {
174 $identifier = 'someCacheIdentifier';
175 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
176 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
177 foreach (array('', 'abc def', 'foo!', 'bar:', 'some/', 'bla*', 'one+', 'äöü', str_repeat('x', 251), 'x$', '\\a', 'b#') as $tag) {
178 $this->assertFalse($cache->isValidTag($tag), 'Invalid tag "' . $tag . '" was not rejected.');
179 }
180 }
181
182 /**
183 * @test
184 * @author Robert Lemke <robert@typo3.org>
185 * @author Ingo Renner <ingo@typo3.org>
186 */
187 public function validTagsAreRecognizedAsValid() {
188 $identifier = 'someCacheIdentifier';
189 $backend = $this->getMock('t3lib_cache_backend_AbstractBackend', array(), array(), '', FALSE);
190 $cache = $this->getMock('t3lib_cache_frontend_StringFrontend', array('__construct', 'get', 'set', 'has', 'remove', 'getByTag'), array($identifier, $backend));
191 foreach (array('abcdef', 'foo-bar', 'foo_baar', 'bar123', '3some', 'file%Thing', 'some&', '%x%', str_repeat('x', 250)) as $tag) {
192 $this->assertTrue($cache->isValidTag($tag), 'Valid tag "' . $tag . '" was not accepted.');
193 }
194 }
195 }
196
197 ?>