| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend_Cache
- * @package UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
-
- require_once 'Zend/Cache.php';
- require_once 'Zend/Cache/Manager.php';
- require_once 'Zend/Config.php';
- /**
- * @category Zend_Cache
- * @package UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Cache_ManagerTest extends PHPUnit_Framework_TestCase
- {
- public function setUp()
- {
- $this->_cache_dir = $this->mkdir();
- $this->_cache = Zend_Cache::factory(
- 'Core', 'File',
- array('automatic_serialization'=>true),
- array('cache_dir'=>$this->_cache_dir)
- );
- }
- public function tearDown()
- {
- $this->rmdir();
- $this->_cache = null;
- }
- public function testSetsCacheObject()
- {
- $manager = new Zend_Cache_Manager;
- $manager->setCache('cache1', $this->_cache);
- $this->assertTrue($manager->getCache('cache1') instanceof Zend_Cache_Core);
- }
- /**
- * @group ZF-10220
- */
- public function testGetAvialableCaches()
- {
- $manager = new Zend_Cache_Manager();
- $caches = $manager->getCaches();
- $this->assertTrue(is_array($caches));
- $this->assertArrayHasKey('default', $caches);
- }
- public function testLazyLoadsDefaultPageCache()
- {
- $manager = new Zend_Cache_Manager;
- $manager->setTemplateOptions('pagetag',array(
- 'backend' => array(
- 'options' => array(
- 'cache_dir' => $this->_cache_dir
- )
- )
- ));
- $this->assertTrue($manager->getCache('page') instanceof Zend_Cache_Frontend_Capture);
- }
- public function testCanOverrideCacheFrontendNameConfiguration()
- {
- $manager = new Zend_Cache_Manager;
- $manager->setTemplateOptions('pagetag',array(
- 'backend' => array(
- 'options' => array(
- 'cache_dir' => $this->_cache_dir
- )
- )
- ));
- $manager->setTemplateOptions('page', array(
- 'frontend' => array(
- 'name'=> 'Page'
- )
- ));
- $this->assertTrue($manager->getCache('page') instanceof Zend_Cache_Frontend_Page);
- }
- public function testCanMergeTemplateCacheOptionsFromZendConfig()
- {
- $manager = new Zend_Cache_Manager;
- $config = new Zend_Config(array(
- 'backend' => array(
- 'options' => array(
- 'cache_dir' => $this->_cache_dir
- )
- )
- ));
- $manager->setTemplateOptions('pagetag', $config);
- $options = $manager->getCacheTemplate('pagetag');
- $this->assertEquals($this->_cache_dir, $options['backend']['options']['cache_dir']);
- }
- public function testCanOverrideCacheBackendendNameConfiguration()
- {
- $manager = new Zend_Cache_Manager;
- $manager->setTemplateOptions('pagetag',array(
- 'backend' => array(
- 'options' => array(
- 'cache_dir' => $this->_cache_dir
- )
- )
- ));
- $manager->setTemplateOptions('page', array(
- 'backend' => array(
- 'name'=> 'File'
- )
- ));
- $this->assertTrue($manager->getCache('page')->getBackend() instanceof Zend_Cache_Backend_File);
- }
- public function testCanOverrideCacheFrontendOptionsConfiguration()
- {
- $manager = new Zend_Cache_Manager;
- $manager->setTemplateOptions('page', array(
- 'frontend' => array(
- 'options'=> array(
- 'lifetime' => 9999
- )
- )
- ));
- $config = $manager->getCacheTemplate('page');
- $this->assertEquals(9999, $config['frontend']['options']['lifetime']);
- }
- public function testCanOverrideCacheBackendOptionsConfiguration()
- {
- $manager = new Zend_Cache_Manager;
- $manager->setTemplateOptions('page', array(
- 'backend' => array(
- 'options'=> array(
- 'public_dir' => './cacheDir'
- )
- )
- ));
- $config = $manager->getCacheTemplate('page');
- $this->assertEquals('./cacheDir', $config['backend']['options']['public_dir']);
- }
- public function testSetsConfigTemplate()
- {
- $manager = new Zend_Cache_Manager;
- $config = array(
- 'frontend' => array(
- 'name' => 'Core',
- 'options' => array(
- 'automatic_serialization' => true
- )
- ),
- 'backend' => array(
- 'name' => 'File',
- 'options' => array(
- 'cache_dir' => '../cache',
- )
- )
- );
- $manager->setCacheTemplate('myCache', $config);
- $this->assertSame($config, $manager->getCacheTemplate('myCache'));
- }
-
- public function testSetsConfigTemplateWithoutMultipartNameNormalisation()
- {
- $manager = new Zend_Cache_Manager;
- $config = array(
- 'frontend' => array(
- 'name' => 'Core',
- 'options' => array(
- 'automatic_serialization' => true
- )
- ),
- 'backend' => array(
- 'name' => 'BlackHole'
- )
- );
- $manager->setCacheTemplate('myCache', $config);
- $this->assertSame($config, $manager->getCacheTemplate('myCache'));
- }
- public function testSetsOptionsTemplateUsingZendConfig()
- {
- $manager = new Zend_Cache_Manager;
- $config = array(
- 'frontend' => array(
- 'name' => 'Core',
- 'options' => array(
- 'automatic_serialization' => true
- )
- ),
- 'backend' => array(
- 'name' => 'File',
- 'options' => array(
- 'cache_dir' => '../cache',
- )
- )
- );
- $manager->setCacheTemplate('myCache', new Zend_Config($config));
- $this->assertSame($config, $manager->getCacheTemplate('myCache'));
- }
- public function testConfigTemplatesDetectedAsAvailableCaches()
- {
- $manager = new Zend_Cache_Manager;
- $this->assertTrue($manager->hasCache('page'));
- }
- public function testGettingPageCacheAlsoCreatesTagCache()
- {
- $manager = new Zend_Cache_Manager;
- $tagCacheConfig = $manager->getCacheTemplate('tagCache');
- $tagCacheConfig['backend']['options']['cache_dir'] = $this->getTmpDir();
- $manager->setTemplateOptions('pagetag', $tagCacheConfig);
- $tagCache = $manager->getCache('page')->getBackend()->getOption('tag_cache');
- $this->assertTrue($tagCache instanceof Zend_Cache_Core);
- }
- /**
- * @group GH-189
- */
- public function testSetsOptionsWithCustomFrontendAndBackendNamingAndAutoload()
- {
- $manager = new Zend_Cache_Manager;
- $manager->setTemplateOptions(
- 'page',
- array(
- 'frontend' => array(
- 'customFrontendNaming' => true,
- ),
- 'backend' => array(
- 'customBackendNaming' => true,
- ),
- 'frontendBackendAutoload' => true,
- )
- );
- $config = $manager->getCacheTemplate('page');
- $this->assertTrue($config['frontend']['customFrontendNaming']);
- $this->assertTrue($config['backend']['customBackendNaming']);
- $this->assertTrue($config['frontendBackendAutoload']);
- }
- // Helper Methods
- public function mkdir()
- {
- $tmp = $this->getTmpDir();
- @mkdir($tmp);
- return $tmp;
- }
- public function rmdir()
- {
- $tmpDir = $this->getTmpDir(false);
- foreach (glob("$tmpDir*") as $dirname) {
- @rmdir($dirname);
- }
- }
- public function getTmpDir($date = true)
- {
- $suffix = '';
- $tmp = sys_get_temp_dir();
- if ($date) {
- $suffix = date('mdyHis');
- }
- if (is_writeable($tmp)) {
- return $tmp . DIRECTORY_SEPARATOR . 'zend_cache_tmp_dir_' . $suffix;
- } else {
- throw new Exception("no writable tmpdir found");
- }
- }
- }
|