2 /***************************************************************
5 * (c) 2011 Extbase Team
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 class Tx_Extbase_Service_CacheService
33 class Tx_Extbase_Tests_Unit_Service_CacheServiceTest
extends Tx_Extbase_Tests_Unit_BaseTestCase
{
36 * @var Tx_Extbase_Service_CacheService
38 protected $cacheService;
43 protected $typo3DbBackup;
46 * @var t3lib_cache_Manager
48 protected $cacheManagerBackup;
50 public function setUp() {
51 $this->typo3DbBackup
= $GLOBALS['TYPO3_DB'];
52 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB');
54 $this->cacheManagerBackup
= $GLOBALS['typo3CacheManager'];
55 $GLOBALS['typo3CacheManager'] = $this->getMock('t3lib_cache_Manager');
57 $this->cacheService
= $this->getAccessibleMock('Tx_Extbase_Service_CacheService', array('dummy'));
60 public function tearDown() {
61 $GLOBALS['TYPO3_DB'] = $this->typo3DbBackup
;
62 $GLOBALS['typo3CacheManager'] = $this->cacheManagerBackup
;
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);
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');
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();
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');
104 $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pages')->will($this->returnValue($mockCacheFrontend));
106 $this->cacheService
->_set('useCachingFramework', TRUE);
107 $this->cacheService
->_call('flushPageCache', array(1,2,3));
113 public function flushPageCacheUsesCacheManagerToFlushCacheOfAllPagesIfCachingFrameworkIsEnabledAndPageIdsIsNull() {
114 $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
115 $mockCacheFrontend->expects($this->once())->method('flush');
117 $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pages')->will($this->returnValue($mockCacheFrontend));
119 $this->cacheService
->_set('useCachingFramework', TRUE);
120 $this->cacheService
->_call('flushPageCache');
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(
133 $this->cacheService
->_set('useCachingFramework', FALSE);
134 $this->cacheService
->_call('flushPageCache', array(1,2,3));
140 public function flushPageCacheFlushesCacheOfAllPagesDirectlyIfCachingFrameworkIsDisabled() {
141 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_TRUNCATEquery')->with('cache_pages');
143 $this->cacheService
->_set('useCachingFramework', FALSE);
144 $this->cacheService
->_call('flushPageCache');
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');
156 $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pagesection')->will($this->returnValue($mockCacheFrontend));
158 $this->cacheService
->_set('useCachingFramework', TRUE);
159 $this->cacheService
->_call('flushPageSectionCache', array(1,2,3));
165 public function flushPageSectionCacheUsesCacheManagerToFlushCacheOfAllPageSectionsIfCachingFrameworkIsEnabledAndPageIdsIsNull() {
166 $mockCacheFrontend = $this->getMock('t3lib_cache_frontend_Frontend');
167 $mockCacheFrontend->expects($this->once())->method('flush');
169 $GLOBALS['typo3CacheManager']->expects($this->once())->method('getCache')->with('cache_pagesection')->will($this->returnValue($mockCacheFrontend));
171 $this->cacheService
->_set('useCachingFramework', TRUE);
172 $this->cacheService
->_call('flushPageSectionCache');
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(
185 $this->cacheService
->_set('useCachingFramework', FALSE);
186 $this->cacheService
->_call('flushPageSectionCache', array(1,2,3));
192 public function flushPageSectionCacheCacheFlushesCacheOfAllPageSectionsDirectlyIfCachingFrameworkIsDisabled() {
193 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_TRUNCATEquery')->with('cache_pagesection');
195 $this->cacheService
->_set('useCachingFramework', FALSE);
196 $this->cacheService
->_call('flushPageSectionCache');