Merge "[BUGFIX] Adjust unit tests to changes"
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Service / CacheServiceTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Extbase Team
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 class Tx_Extbase_Service_CacheService
28 *
29 * @package Extbase
30 * @subpackage extbase
31 */
32
33 class Tx_Extbase_Tests_Unit_Service_CacheServiceTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
34
35 /**
36 * @var Tx_Extbase_Service_CacheService
37 */
38 protected $cacheService;
39
40 /**
41 * @var t3lib_DB
42 */
43 protected $typo3DbBackup;
44
45 /**
46 * @var t3lib_cache_Manager
47 */
48 protected $cacheManagerBackup;
49
50 public function setUp() {
51 $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
52 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB');
53
54 $this->cacheManagerBackup = $GLOBALS['typo3CacheManager'];
55 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager');
56
57 $this->cacheService = $this->getAccessibleMock('Tx_Extbase_Service_CacheService', array('dummy'));
58 }
59
60 public function tearDown() {
61 $GLOBALS['TYPO3_DB'] = $this->typo3DbBackup;
62 $GLOBALS['typo3CacheManager'] = $this->cacheManagerBackup;
63 }
64
65 /**
66 * @test
67 */
68 public function clearPageCacheConvertsPageIdsToArray() {
69 $cacheService = $this->getMock('Tx_Extbase_Service_CacheService', array('flushPageCache', 'flushPageSectionCache'));
70 $cacheService->expects($this->once())->method('flushPageCache')->with(array(123));
71 $cacheService->expects($this->once())->method('flushPageSectionCache')->with(array(123));
72 $cacheService->clearPageCache(123);
73 }
74
75 /**
76 * @test
77 */
78 public function clearPageCacheConvertsPageIdsToNumericArray() {
79 $cacheService = $this->getMock('Tx_Extbase_Service_CacheService', array('flushPageCache', 'flushPageSectionCache'));
80 $cacheService->expects($this->once())->method('flushPageCache')->with(array(0));
81 $cacheService->expects($this->once())->method('flushPageSectionCache')->with(array(0));
82 $cacheService->clearPageCache('Foo');
83 }
84
85 /**
86 * @test
87 */
88 public function clearPageCacheDoesNotConvertPageIdsIfNoneAreSpecified() {
89 $cacheService = $this->getMock('Tx_Extbase_Service_CacheService', array('flushPageCache', 'flushPageSectionCache'));
90 $cacheService->expects($this->once())->method('flushPageCache')->with(NULL);
91 $cacheService->expects($this->once())->method('flushPageSectionCache')->with(NULL);
92 $cacheService->clearPageCache();
93 }
94
95 /**
96 * @test
97 */
98 public function flushPageCacheUsesCacheManagerToFlushCacheOfSpecifiedPagesIfCachingFrameworkIsEnabled() {
99 $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
100 $mockCacheFrontend->expects($this->at(0))->method('flushByTag')->with('pageId_1');
101 $mockCacheFrontend->expects($this->at(1))->method('flushByTag')->with('pageId_2');
102 $mockCacheFrontend->expects($this->at(2))->method('flushByTag')->with('pageId_3');
103
104 $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pages')->will($this->returnValue($mockCacheFrontend));
105
106 $this->cacheService->_set('useCachingFramework', TRUE);
107 $this->cacheService->_call('flushPageCache', array(1,2,3));
108 }
109
110 /**
111 * @test
112 */
113 public function flushPageCacheUsesCacheManagerToFlushCacheOfAllPagesIfCachingFrameworkIsEnabledAndPageIdsIsNull() {
114 $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
115 $mockCacheFrontend->expects($this->once())->method('flush');
116
117 $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pages')->will($this->returnValue($mockCacheFrontend));
118
119 $this->cacheService->_set('useCachingFramework', TRUE);
120 $this->cacheService->_call('flushPageCache');
121 }
122
123 /**
124 * @test
125 */
126 public function flushPageCacheFlushesCacheOfSpecifiedPagesDirectlyIfCachingFrameworkIsDisabled() {
127 $GLOBALS['TYPO3_DB']->expects($this->once())->method('cleanIntArray')->with(array(1,2,3))->will($this->returnValue(array(3,2,1)));
128 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with(
129 'cache_pages',
130 'page_id IN (3,2,1)'
131 );
132
133 $this->cacheService->_set('useCachingFramework', FALSE);
134 $this->cacheService->_call('flushPageCache', array(1,2,3));
135 }
136
137 /**
138 * test
139 */
140 public function flushPageCacheFlushesCacheOfAllPagesDirectlyIfCachingFrameworkIsDisabled() {
141 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_TRUNCATEquery')->with('cache_pages');
142
143 $this->cacheService->_set('useCachingFramework', FALSE);
144 $this->cacheService->_call('flushPageCache');
145 }
146
147 /**
148 * @test
149 */
150 public function flushPageSectionCacheUsesCacheManagerToFlushCacheOfSpecifiedPageSectionsIfCachingFrameworkIsEnabled() {
151 $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
152 $mockCacheFrontend->expects($this->at(0))->method('flushByTag')->with('pageId_1');
153 $mockCacheFrontend->expects($this->at(1))->method('flushByTag')->with('pageId_2');
154 $mockCacheFrontend->expects($this->at(2))->method('flushByTag')->with('pageId_3');
155
156 $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pagesection')->will($this->returnValue($mockCacheFrontend));
157
158 $this->cacheService->_set('useCachingFramework', TRUE);
159 $this->cacheService->_call('flushPageSectionCache', array(1,2,3));
160 }
161
162 /**
163 * @test
164 */
165 public function flushPageSectionCacheUsesCacheManagerToFlushCacheOfAllPageSectionsIfCachingFrameworkIsEnabledAndPageIdsIsNull() {
166 $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
167 $mockCacheFrontend->expects($this->once())->method('flush');
168
169 $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pagesection')->will($this->returnValue($mockCacheFrontend));
170
171 $this->cacheService->_set('useCachingFramework', TRUE);
172 $this->cacheService->_call('flushPageSectionCache');
173 }
174
175 /**
176 * @test
177 */
178 public function flushPageSectionCacheFlushesCacheOfSpecifiedPageSectionsDirectlyIfCachingFrameworkIsDisabled() {
179 $GLOBALS['TYPO3_DB']->expects($this->once())->method('cleanIntArray')->with(array(1,2,3))->will($this->returnValue(array(3,2,1)));
180 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with(
181 'cache_pagesection',
182 'page_id IN (3,2,1)'
183 );
184
185 $this->cacheService->_set('useCachingFramework', FALSE);
186 $this->cacheService->_call('flushPageSectionCache', array(1,2,3));
187 }
188
189 /**
190 * test
191 */
192 public function flushPageSectionCacheCacheFlushesCacheOfAllPageSectionsDirectlyIfCachingFrameworkIsDisabled() {
193 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_TRUNCATEquery')->with('cache_pagesection');
194
195 $this->cacheService->_set('useCachingFramework', FALSE);
196 $this->cacheService->_call('flushPageSectionCache');
197 }
198
199 }
200 ?>