1<?php 2include_once(Kohana::find_file('tests/cache', 'CacheBasicMethodsTest')); 3 4/** 5 * @package Kohana/Cache 6 * @group kohana 7 * @group kohana.cache 8 * @category Test 9 * @author Kohana Team 10 * @copyright (c) 2009-2012 Kohana Team 11 * @license http://kohanaphp.com/license 12 */ 13class Kohana_Cache_FileTest extends Kohana_CacheBasicMethodsTest { 14 15 /** 16 * This method MUST be implemented by each driver to setup the `Cache` 17 * instance for each test. 18 * 19 * This method should do the following tasks for each driver test: 20 * 21 * - Test the Cache instance driver is available, skip test otherwise 22 * - Setup the Cache instance 23 * - Call the parent setup method, `parent::setUp()` 24 * 25 * @return void 26 */ 27 public function setUp() 28 { 29 parent::setUp(); 30 31 if ( ! Kohana::$config->load('cache.file')) 32 { 33 Kohana::$config->load('cache') 34 ->set( 35 'file', 36 array( 37 'driver' => 'file', 38 'cache_dir' => APPPATH.'cache', 39 'default_expire' => 3600, 40 'ignore_on_delete' => array( 41 'file_we_want_to_keep.cache', 42 '.gitignore', 43 '.git', 44 '.svn' 45 ) 46 ) 47 ); 48 } 49 50 $this->cache(Cache::instance('file')); 51 } 52 53 /** 54 * Tests that ignored files are not removed from file cache 55 * 56 * @return void 57 */ 58 public function test_ignore_delete_file() 59 { 60 $cache = $this->cache(); 61 $config = Kohana::$config->load('cache')->file; 62 $file = $config['cache_dir'].'/file_we_want_to_keep.cache'; 63 64 // Lets pollute the cache folder 65 file_put_contents($file, 'foobar'); 66 67 $this->assertTrue($cache->delete_all()); 68 $this->assertTrue(file_exists($file)); 69 $this->assertEquals('foobar', file_get_contents($file)); 70 71 unlink($file); 72 } 73 74 /** 75 * Provider for test_utf8 76 * 77 * @return array 78 */ 79 public function provider_utf8() 80 { 81 return array( 82 array( 83 'This is â ütf-8 Ӝ☃ string', 84 'This is â ütf-8 Ӝ☃ string' 85 ), 86 array( 87 '㆓㆕㆙㆛', 88 '㆓㆕㆙㆛' 89 ), 90 array( 91 'அஆஇஈஊ', 92 'அஆஇஈஊ' 93 ) 94 ); 95 } 96 97 /** 98 * Tests the file driver supports utf-8 strings 99 * 100 * @dataProvider provider_utf8 101 * 102 * @return void 103 */ 104 public function test_utf8($input, $expected) 105 { 106 $cache = $this->cache(); 107 $cache->set('utf8', $input); 108 109 $this->assertSame($expected, $cache->get('utf8')); 110 } 111 112 /** 113 * Tests garbage collection. 114 * Tests if non-expired cache files withstand garbage collection 115 * 116 * @test 117 */ 118 public function test_garbage_collection() 119 { 120 $cache = $this->cache(); 121 $cache->set('persistent', 'dummy persistent data', 3); 122 $cache->set('volatile', 'dummy volatile data', 1); 123 124 $this->assertTrue($this->is_file('persistent')); 125 $this->assertTrue($this->is_file('volatile')); 126 127 // sleep for more than a second 128 sleep(2); 129 130 $cache->garbage_collect(); 131 132 $this->assertTrue($this->is_file('persistent')); 133 $this->assertFalse($this->is_file('volatile')); 134 } 135 136 /** 137 * helper method for test_garbage_collection. 138 * Tests if cache file exists given cache id. 139 * 140 * @param string $id cache id 141 * @return boolean TRUE if file exists FALSE otherwise 142 */ 143 protected function is_file($id) 144 { 145 $cache = $this->cache(); 146 147 $method_sanitize_id = new ReflectionMethod($cache, '_sanitize_id'); 148 $method_sanitize_id->setAccessible(TRUE); 149 $method_filename = new ReflectionMethod($cache, 'filename'); 150 $method_filename->setAccessible(TRUE); 151 $method_resolve_directory = new ReflectionMethod($cache, '_resolve_directory'); 152 $method_resolve_directory->setAccessible(TRUE); 153 154 $sanitized_id = $method_sanitize_id->invoke($cache, $id); 155 $filename = $method_filename->invoke($cache, $sanitized_id); 156 $directory = $method_resolve_directory->invoke($cache, $filename); 157 158 $file = new SplFileInfo($directory.$filename); 159 160 //var_dump($cache->_is_expired($file)); 161 return $file->isFile(); 162 } 163} // End Kohana_SqliteTest 164