[TASK] Update caching framework to latest FLOW3 version
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / class.t3lib_cache_managerTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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 * Testcase for the Cache Manager
27 *
28 * This file is a backport from FLOW3
29 *
30 * @author Ingo Renner <ingo@typo3.org>
31 * @package TYPO3
32 * @subpackage tests
33 */
34 class t3lib_cache_ManagerTest extends tx_phpunit_testcase {
35
36 /**
37 * @test
38 * @author Robert Lemke <robert@typo3.org>
39 * @author Ingo Renner <ingo@typo3.org>
40 * @expectedException t3lib_cache_exception_DuplicateIdentifier
41 */
42 public function managerThrowsExceptionOnCacheRegistrationWithAlreadyExistingIdentifier() {
43 $manager = new t3lib_cache_Manager();
44
45 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
46 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
47
48 $cache2 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
49 $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('test'));
50
51 $manager->registerCache($cache1);
52 $manager->registerCache($cache2);
53 }
54
55 /**
56 * @test
57 * @author Robert Lemke <robert@typo3.org>
58 * @author Ingo Renner <ingo@typo3.org>
59 */
60 public function managerReturnsThePreviouslyRegisteredCache() {
61 $manager = new t3lib_cache_Manager();
62
63 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
64 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
65
66 $cache2 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
67 $cache2->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache2'));
68
69 $manager->registerCache($cache1);
70 $manager->registerCache($cache2);
71
72 $this->assertSame($cache2, $manager->getCache('cache2'), 'The cache returned by getCache() was not the same I registered.');
73 }
74
75 /**
76 * @test
77 * @author Robert Lemke <robert@typo3.org>
78 * @author Ingo Renner <ingo@typo3.org>
79 * @expectedException t3lib_cache_exception_NoSuchCache
80 */
81 public function getCacheThrowsExceptionForNonExistingIdentifier() {
82 $manager = new t3lib_cache_Manager();
83 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
84 $cache->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('someidentifier'));
85
86 $manager->registerCache($cache);
87 $manager->getCache('someidentifier');
88
89 $manager->getCache('doesnotexist');
90 }
91
92 /**
93 * @test
94 * @author Robert Lemke <robert@typo3.org>
95 * @author Ingo Renner <ingo@typo3.org>
96 */
97 public function hasCacheReturnsCorrectResult() {
98 $manager = new t3lib_cache_Manager();
99 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
100 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
101 $manager->registerCache($cache1);
102
103 $this->assertTrue($manager->hasCache('cache1'), 'hasCache() did not return TRUE.');
104 $this->assertFalse($manager->hasCache('cache2'), 'hasCache() did not return FALSE.');
105 }
106
107 /**
108 * @test
109 * @author Robert Lemke <robert@typo3.org>
110 * @author Ingo Renner <ingo@typo3.org>
111 */
112 public function flushCachesByTagCallsTheFlushByTagMethodOfAllRegisteredCaches() {
113 $manager = new t3lib_cache_Manager();
114
115 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
116 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
117 $cache1->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
118 $manager->registerCache($cache1);
119
120 $cache2 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
121 $cache2->expects($this->once())->method('flushByTag')->with($this->equalTo('theTag'));
122 $manager->registerCache($cache2);
123
124 $manager->flushCachesByTag('theTag');
125 }
126
127 /**
128 * @test
129 * @author Robert Lemke <robert@typo3.org>
130 * @author Ingo Renner <ingo@typo3.org>
131 */
132 public function flushCachesCallsTheFlushMethodOfAllRegisteredCaches() {
133 $manager = new t3lib_cache_Manager();
134
135 $cache1 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
136 $cache1->expects($this->atLeastOnce())->method('getIdentifier')->will($this->returnValue('cache1'));
137 $cache1->expects($this->once())->method('flush');
138 $manager->registerCache($cache1);
139
140 $cache2 = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag'), array(), '', FALSE);
141 $cache2->expects($this->once())->method('flush');
142 $manager->registerCache($cache2);
143
144 $manager->flushCaches();
145 }
146
147 }
148
149 ?>