Kohana v3.3.0

This commit is contained in:
Deon George
2013-04-22 14:09:50 +10:00
commit f96694b18f
1280 changed files with 145034 additions and 0 deletions

View File

@@ -0,0 +1,299 @@
<?php
/**
* @package Kohana/Cache
* @group kohana
* @group kohana.cache
* @category Test
* @author Kohana Team
* @copyright (c) 2009-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
abstract class Kohana_CacheBasicMethodsTest extends PHPUnit_Framework_TestCase {
/**
* @var Cache driver for this test
*/
protected $_cache_driver;
/**
* This method MUST be implemented by each driver to setup the `Cache`
* instance for each test.
*
* This method should do the following tasks for each driver test:
*
* - Test the Cache instance driver is available, skip test otherwise
* - Setup the Cache instance
* - Call the parent setup method, `parent::setUp()`
*
* @return void
*/
public function setUp()
{
parent::setUp();
}
/**
* Accessor method to `$_cache_driver`.
*
* @return Cache
* @return self
*/
public function cache(Cache $cache = NULL)
{
if ($cache === NULL)
return $this->_cache_driver;
$this->_cache_driver = $cache;
return $this;
}
/**
* Data provider for test_set_get()
*
* @return array
*/
public function provider_set_get()
{
$object = new StdClass;
$object->foo = 'foo';
$object->bar = 'bar';
$html_text = <<<TESTTEXT
<!doctype html>
<head>
</head>
<body>
</body>
</html>
TESTTEXT;
return array(
array(
array(
'id' => 'string', // Key to set to cache
'value' => 'foobar', // Value to set to key
'ttl' => 0, // Time to live
'wait' => FALSE, // Test wait time to let cache expire
'type' => 'string', // Type test
'default' => NULL // Default value get should return
),
'foobar'
),
array(
array(
'id' => 'integer',
'value' => 101010,
'ttl' => 0,
'wait' => FALSE,
'type' => 'integer',
'default' => NULL
),
101010
),
array(
array(
'id' => 'float',
'value' => 10.00,
'ttl' => 0,
'wait' => FALSE,
'type' => 'float',
'default' => NULL
),
10.00
),
array(
array(
'id' => 'array',
'value' => array(
'key' => 'foo',
'value' => 'bar'
),
'ttl' => 0,
'wait' => FALSE,
'type' => 'array',
'default' => NULL
),
array(
'key' => 'foo',
'value' => 'bar'
)
),
array(
array(
'id' => 'boolean',
'value' => TRUE,
'ttl' => 0,
'wait' => FALSE,
'type' => 'boolean',
'default' => NULL
),
TRUE
),
array(
array(
'id' => 'null',
'value' => NULL,
'ttl' => 0,
'wait' => FALSE,
'type' => 'null',
'default' => NULL
),
NULL
),
array(
array(
'id' => 'object',
'value' => $object,
'ttl' => 0,
'wait' => FALSE,
'type' => 'object',
'default' => NULL
),
$object
),
array(
array(
'id' => 'bar\\ with / troublesome key',
'value' => 'foo bar snafu',
'ttl' => 0,
'wait' => FALSE,
'type' => 'string',
'default' => NULL
),
'foo bar snafu'
),
array(
array(
'id' => 'bar',
'value' => 'foo',
'ttl' => 3,
'wait' => 5,
'type' => 'null',
'default' => NULL
),
NULL
),
array(
array(
'id' => 'snafu',
'value' => 'fubar',
'ttl' => 3,
'wait' => 5,
'type' => 'string',
'default' => 'something completely different!'
),
'something completely different!'
),
array(
array(
'id' => 'new line test with HTML',
'value' => $html_text,
'ttl' => 10,
'wait' => FALSE,
'type' => 'string',
'default' => NULL,
),
$html_text
)
);
}
/**
* Tests the [Cache::set()] method, testing;
*
* - The value is cached
* - The lifetime is respected
* - The returned value type is as expected
* - The default not-found value is respected
*
* @dataProvider provider_set_get
*
* @param array data
* @param mixed expected
* @return void
*/
public function test_set_get(array $data, $expected)
{
$cache = $this->cache();
extract($data);
$this->assertTrue($cache->set($id, $value, $ttl));
if ($wait !== FALSE)
{
// Lets let the cache expire
sleep($wait);
}
$result = $cache->get($id, $default);
$this->assertEquals($expected, $result);
$this->assertInternalType($type, $result);
unset($id, $value, $ttl, $wait, $type, $default);
}
/**
* Tests the [Cache::delete()] method, testing;
*
* - The a cached value is deleted from cache
* - The cache returns a TRUE value upon deletion
* - The cache returns a FALSE value if no value exists to delete
*
* @return void
*/
public function test_delete()
{
// Init
$cache = $this->cache();
$cache->delete_all();
// Test deletion if real cached value
if ( ! $cache->set('test_delete_1', 'This should not be here!', 0))
{
$this->fail('Unable to set cache value to delete!');
}
// Test delete returns TRUE and check the value is gone
$this->assertTrue($cache->delete('test_delete_1'));
$this->assertNull($cache->get('test_delete_1'));
// Test non-existant cache value returns FALSE if no error
$this->assertFalse($cache->delete('test_delete_1'));
}
/**
* Tests [Cache::delete_all()] works as specified
*
* @return void
* @uses Kohana_CacheBasicMethodsTest::provider_set_get()
*/
public function test_delete_all()
{
// Init
$cache = $this->cache();
$data = $this->provider_set_get();
foreach ($data as $key => $values)
{
extract($values[0]);
if ( ! $cache->set($id, $value))
{
$this->fail('Unable to set: '.$key.' => '.$value.' to cache');
}
unset($id, $value, $ttl, $wait, $type, $default);
}
// Test delete_all is successful
$this->assertTrue($cache->delete_all());
foreach ($data as $key => $values)
{
// Verify data has been purged
$this->assertSame('Cache Deleted!', $cache->get($values[0]['id'],
'Cache Deleted!'));
}
}
} // End Kohana_CacheBasicMethodsTest

242
modules/cache/tests/cache/CacheTest.php vendored Normal file
View File

@@ -0,0 +1,242 @@
<?php
/**
* @package Kohana/Cache
* @group kohana
* @group kohana.cache
* @category Test
* @author Kohana Team
* @copyright (c) 2009-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_CacheTest extends PHPUnit_Framework_TestCase {
const BAD_GROUP_DEFINITION = 1010;
const EXPECT_SELF = 1001;
/**
* Data provider for test_instance
*
* @return array
*/
public function provider_instance()
{
$tmp = realpath(sys_get_temp_dir());
$base = array();
if (Kohana::$config->load('cache.file'))
{
$base = array(
// Test default group
array(
NULL,
Cache::instance('file')
),
// Test defined group
array(
'file',
Cache::instance('file')
),
);
}
return array(
// Test bad group definition
$base+array(
Kohana_CacheTest::BAD_GROUP_DEFINITION,
'Failed to load Kohana Cache group: 1010'
),
);
}
/**
* Tests the [Cache::factory()] method behaves as expected
*
* @dataProvider provider_instance
*
* @return void
*/
public function test_instance($group, $expected)
{
if (in_array($group, array(
Kohana_CacheTest::BAD_GROUP_DEFINITION,
)
))
{
$this->setExpectedException('Cache_Exception');
}
try
{
$cache = Cache::instance($group);
}
catch (Cache_Exception $e)
{
$this->assertSame($expected, $e->getMessage());
throw $e;
}
$this->assertInstanceOf(get_class($expected), $cache);
$this->assertSame($expected->config(), $cache->config());
}
/**
* Tests that `clone($cache)` will be prevented to maintain singleton
*
* @return void
* @expectedException Cache_Exception
*/
public function test_cloning_fails()
{
if ( ! Kohana::$config->load('cache.file'))
{
$this->markTestSkipped('Unable to load File configuration');
}
try
{
$cache_clone = clone(Cache::instance('file'));
}
catch (Cache_Exception $e)
{
$this->assertSame('Cloning of Kohana_Cache objects is forbidden',
$e->getMessage());
throw $e;
}
}
/**
* Data provider for test_config
*
* @return array
*/
public function provider_config()
{
return array(
array(
array(
'server' => 'otherhost',
'port' => 5555,
'persistent' => TRUE,
),
NULL,
Kohana_CacheTest::EXPECT_SELF,
array(
'server' => 'otherhost',
'port' => 5555,
'persistent' => TRUE,
),
),
array(
'foo',
'bar',
Kohana_CacheTest::EXPECT_SELF,
array(
'foo' => 'bar'
)
),
array(
'server',
NULL,
NULL,
array()
),
array(
NULL,
NULL,
array(),
array()
)
);
}
/**
* Tests the config method behaviour
*
* @dataProvider provider_config
*
* @param mixed key value to set or get
* @param mixed value to set to key
* @param mixed expected result from [Cache::config()]
* @param array expected config within cache
* @return void
*/
public function test_config($key, $value, $expected_result, array $expected_config)
{
$cache = $this->getMock('Cache_File', NULL, array(), '', FALSE);
if ($expected_result === Kohana_CacheTest::EXPECT_SELF)
{
$expected_result = $cache;
}
$this->assertSame($expected_result, $cache->config($key, $value));
$this->assertSame($expected_config, $cache->config());
}
/**
* Data provider for test_sanitize_id
*
* @return array
*/
public function provider_sanitize_id()
{
return array(
array(
'foo',
'foo'
),
array(
'foo+-!@',
'foo+-!@'
),
array(
'foo/bar',
'foo_bar',
),
array(
'foo\\bar',
'foo_bar'
),
array(
'foo bar',
'foo_bar'
),
array(
'foo\\bar snafu/stfu',
'foo_bar_snafu_stfu'
)
);
}
/**
* Tests the [Cache::_sanitize_id()] method works as expected.
* This uses some nasty reflection techniques to access a protected
* method.
*
* @dataProvider provider_sanitize_id
*
* @param string id
* @param string expected
* @return void
*/
public function test_sanitize_id($id, $expected)
{
$cache = $this->getMock('Cache', array(
'get',
'set',
'delete',
'delete_all'
), array(array()),
'', FALSE
);
$cache_reflection = new ReflectionClass($cache);
$sanitize_id = $cache_reflection->getMethod('_sanitize_id');
$sanitize_id->setAccessible(TRUE);
$this->assertSame($expected, $sanitize_id->invoke($cache, $id));
}
} // End Kohana_CacheTest

98
modules/cache/tests/cache/FileTest.php vendored Normal file
View File

@@ -0,0 +1,98 @@
<?php
include_once(Kohana::find_file('tests/cache', 'CacheBasicMethodsTest'));
/**
* @package Kohana/Cache
* @group kohana
* @group kohana.cache
* @category Test
* @author Kohana Team
* @copyright (c) 2009-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_Cache_FileTest extends Kohana_CacheBasicMethodsTest {
/**
* This method MUST be implemented by each driver to setup the `Cache`
* instance for each test.
*
* This method should do the following tasks for each driver test:
*
* - Test the Cache instance driver is available, skip test otherwise
* - Setup the Cache instance
* - Call the parent setup method, `parent::setUp()`
*
* @return void
*/
public function setUp()
{
parent::setUp();
if ( ! Kohana::$config->load('cache.file'))
{
$this->markTestSkipped('Unable to load File configuration');
}
$this->cache(Cache::instance('file'));
}
/**
* Tests that ignored files are not removed from file cache
*
* @return void
*/
public function test_ignore_delete_file()
{
$cache = $this->cache();
$config = Kohana::$config->load('cache')->file;
$file = $config['cache_dir'].'/.gitignore';
// Lets pollute the cache folder
file_put_contents($file, 'foobar');
$this->assertTrue($cache->delete_all());
$this->assertTrue(file_exists($file));
$this->assertEquals('foobar', file_get_contents($file));
unlink($file);
}
/**
* Provider for test_utf8
*
* @return array
*/
public function provider_utf8()
{
return array(
array(
'This is â ütf-8 Ӝ☃ string',
'This is â ütf-8 Ӝ☃ string'
),
array(
'㆓㆕㆙㆛',
'㆓㆕㆙㆛'
),
array(
'அஆஇஈஊ',
'அஆஇஈஊ'
)
);
}
/**
* Tests the file driver supports utf-8 strings
*
* @dataProvider provider_utf8
*
* @return void
*/
public function test_utf8($input, $expected)
{
$cache = $this->cache();
$cache->set('utf8', $input);
$this->assertSame($expected, $cache->get('utf8'));
}
} // End Kohana_SqliteTest

View File

@@ -0,0 +1,44 @@
<?php
include_once(Kohana::find_file('tests/cache', 'CacheBasicMethodsTest'));
/**
* @package Kohana/Cache
* @group kohana
* @group kohana.cache
* @category Test
* @author Kohana Team
* @copyright (c) 2009-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_SqliteTest extends Kohana_CacheBasicMethodsTest {
/**
* This method MUST be implemented by each driver to setup the `Cache`
* instance for each test.
*
* This method should do the following tasks for each driver test:
*
* - Test the Cache instance driver is available, skip test otherwise
* - Setup the Cache instance
* - Call the parent setup method, `parent::setUp()`
*
* @return void
*/
public function setUp()
{
parent::setUp();
if ( ! extension_loaded('pdo_sqlite'))
{
$this->markTestSkipped('SQLite PDO PHP Extension is not available');
}
if ( ! Kohana::$config->load('cache.sqlite'))
{
$this->markTestIncomplete('Unable to load sqlite configuration');
}
$this->cache(Cache::instance('sqlite'));
}
} // End Kohana_SqliteTest

View File

@@ -0,0 +1,39 @@
<?php
include_once(Kohana::find_file('tests/cache', 'CacheBasicMethodsTest'));
/**
* @package Kohana/Cache
* @group kohana
* @group kohana.cache
* @category Test
* @author Kohana Team
* @copyright (c) 2009-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_WincacheTest extends Kohana_CacheBasicMethodsTest {
/**
* This method MUST be implemented by each driver to setup the `Cache`
* instance for each test.
*
* This method should do the following tasks for each driver test:
*
* - Test the Cache instance driver is available, skip test otherwise
* - Setup the Cache instance
* - Call the parent setup method, `parent::setUp()`
*
* @return void
*/
public function setUp()
{
parent::setUp();
if ( ! extension_loaded('wincache'))
{
$this->markTestSkipped('Wincache PHP Extension is not available');
}
$this->cache(Cache::instance('wincache'));
}
} // End Kohana_WincacheTest

View File

@@ -0,0 +1,75 @@
<?php
include_once(Kohana::find_file('tests/cache/arithmetic', 'CacheArithmeticMethods'));
/**
* @package Kohana/Cache
* @group kohana
* @group kohana.cache
* @category Test
* @author Kohana Team
* @copyright (c) 2009-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_ApcTest extends Kohana_CacheArithmeticMethodsTest {
/**
* This method MUST be implemented by each driver to setup the `Cache`
* instance for each test.
*
* This method should do the following tasks for each driver test:
*
* - Test the Cache instance driver is available, skip test otherwise
* - Setup the Cache instance
* - Call the parent setup method, `parent::setUp()`
*
* @return void
*/
public function setUp()
{
parent::setUp();
if ( ! extension_loaded('apc'))
{
$this->markTestSkipped('APC PHP Extension is not available');
}
if (ini_get('apc.enable_cli') != '1')
{
$this->markTestSkipped('Unable to test APC in CLI mode. To fix '.
'place "apc.enable_cli=1" in your php.ini file');
}
$this->cache(Cache::instance('apc'));
}
/**
* Tests the [Cache::set()] method, testing;
*
* - The value is cached
* - The lifetime is respected
* - The returned value type is as expected
* - The default not-found value is respected
*
* This test doesn't test the TTL as there is a known bug/feature
* in APC that prevents the same request from killing cache on timeout.
*
* @link http://pecl.php.net/bugs/bug.php?id=16814
*
* @dataProvider provider_set_get
*
* @param array data
* @param mixed expected
* @return void
*/
public function test_set_get(array $data, $expected)
{
if ($data['wait'] !== FALSE)
{
$this->markTestSkipped('Unable to perform TTL test in CLI, see: '.
'http://pecl.php.net/bugs/bug.php?id=16814 for more info!');
}
parent::test_set_get($data, $expected);
}
} // End Kohana_ApcTest

View File

@@ -0,0 +1,173 @@
<?php
include_once(Kohana::find_file('tests/cache', 'CacheBasicMethodsTest'));
/**
* @package Kohana/Cache/Memcache
* @group kohana
* @group kohana.cache
* @category Test
* @author Kohana Team
* @copyright (c) 2009-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
abstract class Kohana_CacheArithmeticMethodsTest extends Kohana_CacheBasicMethodsTest {
public function tearDown()
{
parent::tearDown();
// Cleanup
$cache = $this->cache();
if ($cache instanceof Cache)
{
$cache->delete_all();
}
}
/**
* Provider for test_increment
*
* @return array
*/
public function provider_increment()
{
return array(
array(
0,
array(
'id' => 'increment_test_1',
'step' => 1
),
1
),
array(
1,
array(
'id' => 'increment_test_2',
'step' => 1
),
2
),
array(
5,
array(
'id' => 'increment_test_3',
'step' => 5
),
10
),
array(
NULL,
array(
'id' => 'increment_test_4',
'step' => 1
),
FALSE
),
);
}
/**
* Test for [Cache_Arithmetic::increment()]
*
* @dataProvider provider_increment
*
* @param integer start state
* @param array increment arguments
* @return void
*/
public function test_increment(
$start_state = NULL,
array $inc_args,
$expected)
{
$cache = $this->cache();
if ($start_state !== NULL)
{
$cache->set($inc_args['id'], $start_state, 0);
}
$this->assertSame(
$expected,
$cache->increment(
$inc_args['id'],
$inc_args['step']
)
);
}
/**
* Provider for test_decrement
*
* @return array
*/
public function provider_decrement()
{
return array(
array(
10,
array(
'id' => 'decrement_test_1',
'step' => 1
),
9
),
array(
10,
array(
'id' => 'decrement_test_2',
'step' => 2
),
8
),
array(
50,
array(
'id' => 'decrement_test_3',
'step' => 5
),
45
),
array(
NULL,
array(
'id' => 'decrement_test_4',
'step' => 1
),
FALSE
),
); }
/**
* Test for [Cache_Arithmetic::decrement()]
*
* @dataProvider provider_decrement
*
* @param integer start state
* @param array decrement arguments
* @return void
*/
public function test_decrement(
$start_state = NULL,
array $dec_args,
$expected)
{
$cache = $this->cache();
if ($start_state !== NULL)
{
$cache->set($dec_args['id'], $start_state, 0);
}
$this->assertSame(
$expected,
$cache->decrement(
$dec_args['id'],
$dec_args['step']
)
);
}
} // End Kohana_CacheArithmeticMethodsTest

View File

@@ -0,0 +1,103 @@
<?php
include_once(Kohana::find_file('tests/cache/arithmetic', 'CacheArithmeticMethods'));
/**
* @package Kohana/Cache/Memcache
* @group kohana
* @group kohana.cache
* @category Test
* @author Kohana Team
* @copyright (c) 2009-2012 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_CacheArithmeticMemcacheTest extends Kohana_CacheArithmeticMethodsTest {
/**
* This method MUST be implemented by each driver to setup the `Cache`
* instance for each test.
*
* This method should do the following tasks for each driver test:
*
* - Test the Cache instance driver is available, skip test otherwise
* - Setup the Cache instance
* - Call the parent setup method, `parent::setUp()`
*
* @return void
*/
public function setUp()
{
parent::setUp();
if ( ! extension_loaded('memcache'))
{
$this->markTestSkipped('Memcache PHP Extension is not available');
}
if ( ! $config = Kohana::$config->load('cache.memcache'))
{
$this->markTestSkipped('Unable to load Memcache configuration');
}
$memcache = new Memcache;
if ( ! $memcache->connect($config['servers']['local']['host'],
$config['servers']['local']['port']))
{
$this->markTestSkipped('Unable to connect to memcache server @ '.
$config['servers']['local']['host'].':'.
$config['servers']['local']['port']);
}
if ($memcache->getVersion() === FALSE)
{
$this->markTestSkipped('Memcache server @ '.
$config['servers']['local']['host'].':'.
$config['servers']['local']['port'].
' not responding!');
}
unset($memcache);
$this->cache(Cache::instance('memcache'));
}
/**
* Tests that multiple values set with Memcache do not cause unexpected
* results. For accurate results, this should be run with a memcache
* configuration that includes multiple servers.
*
* This is to test #4110
*
* @link http://dev.kohanaframework.org/issues/4110
* @return void
*/
public function test_multiple_set()
{
$cache = $this->cache();
$id_set = 'set_id';
$ttl = 300;
$data = array(
'foobar',
0,
1.0,
new stdClass,
array('foo', 'bar' => 1),
TRUE,
NULL,
FALSE
);
$previous_set = $cache->get($id_set, NULL);
foreach ($data as $value)
{
// Use Equals over Sames as Objects will not be equal
$this->assertEquals($previous_set, $cache->get($id_set, NULL));
$cache->set($id_set, $value, $ttl);
$previous_set = $value;
}
}
} // End Kohana_CacheArithmeticMemcacheTest

View File

@@ -0,0 +1,265 @@
<?php defined('SYSPATH') OR die('Kohana bootstrap needs to be included before tests run');
/**
* Unit tests for request client cache logic
*
* @group kohana
* @group kohana.request
* @group kohana.request.client
* @group kohana.request.client.cache
*
* @package Kohana
* @category Tests
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Request_Client_CacheTest extends Unittest_TestCase {
/**
* Sets up a test route for caching
*
* @return void
*/
public function setUp()
{
Route::set('welcome', 'welcome/index')
->defaults(array(
'controller' => 'welcome',
'action' => 'index'
));
parent::setUp();
}
/**
* Tests the Client does not attempt to load cache if no Cache library
* is present
*
* @return void
*/
public function test_cache_not_called_with_no_cache()
{
$request = new Request('welcome/index');
$response = new Response;
$client_mock = $this->getMock('Request_Client_Internal');
$request->client($client_mock);
$client_mock->expects($this->exactly(0))
->method('execute_request');
$client_mock->expects($this->once())
->method('execute')
->will($this->returnValue($response));
$this->assertSame($response, $request->execute());
}
/**
* Tests that the client attempts to load a cached response from the
* cache library, but fails.
*
* @return void
*/
public function test_cache_miss()
{
$route = new Route('welcome/index');
$route->defaults(array(
'controller' => 'Kohana_Request_CacheTest_Dummy',
'action' => 'index',
));
$request = new Request('welcome/index', NULL, array($route));
$cache_mock = $this->_get_cache_mock();
$request->client()->cache(HTTP_Cache::factory($cache_mock));
$cache_mock->expects($this->once())
->method('get')
->with($request->client()->cache()->create_cache_key($request))
->will($this->returnValue(FALSE));
$response = $request->client()->execute($request);
$this->assertSame(HTTP_Cache::CACHE_STATUS_MISS,
$response->headers(HTTP_Cache::CACHE_STATUS_KEY));
}
/**
* Tests the client saves a response if the correct headers are set
*
* @return void
*/
public function test_cache_save()
{
$lifetime = 800;
$request = new Request('welcome/index');
$cache_mock = $this->_get_cache_mock();
$response = Response::factory();
$request->client()->cache(new HTTP_Cache(array(
'cache' => $cache_mock
)
));
$response->headers('cache-control', 'max-age='.$lifetime);
$key = $request->client()->cache()->create_cache_key($request);
$cache_mock->expects($this->at(0))
->method('set')
->with($this->stringEndsWith($key), $this->identicalTo(0));
$cache_mock->expects($this->at(1))
->method('set')
->with($this->identicalTo($key), $this->anything(), $this->identicalTo($lifetime))
->will($this->returnValue(TRUE));
$this->assertTrue(
$request->client()->cache()
->cache_response($key, $request, $response)
);
$this->assertSame(HTTP_Cache::CACHE_STATUS_SAVED,
$response->headers(HTTP_Cache::CACHE_STATUS_KEY));
}
/**
* Tests the client handles a cache HIT event correctly
*
* @return void
*/
public function test_cache_hit()
{
$lifetime = 800;
$request = new Request('welcome/index');
$cache_mock = $this->_get_cache_mock();
$request->client()->cache(new HTTP_Cache(array(
'cache' => $cache_mock
)
));
$response = Response::factory();
$response->headers(array(
'cache-control' => 'max-age='.$lifetime,
HTTP_Cache::CACHE_STATUS_KEY =>
HTTP_Cache::CACHE_STATUS_HIT
));
$key = $request->client()->cache()->create_cache_key($request);
$cache_mock->expects($this->exactly(2))
->method('get')
->with($this->stringContains($key))
->will($this->returnValue($response));
$request->client()->cache()->cache_response($key, $request);
$this->assertSame(HTTP_Cache::CACHE_STATUS_HIT,
$response->headers(HTTP_Cache::CACHE_STATUS_KEY));
}
/**
* Data provider for test_set_cache
*
* @return array
*/
public function provider_set_cache()
{
return array(
array(
new HTTP_Header(array('cache-control' => 'no-cache')),
array('no-cache' => NULL),
FALSE,
),
array(
new HTTP_Header(array('cache-control' => 'no-store')),
array('no-store' => NULL),
FALSE,
),
array(
new HTTP_Header(array('cache-control' => 'max-age=100')),
array('max-age' => '100'),
TRUE
),
array(
new HTTP_Header(array('cache-control' => 'private')),
array('private' => NULL),
FALSE
),
array(
new HTTP_Header(array('cache-control' => 'private, max-age=100')),
array('private' => NULL, 'max-age' => '100'),
FALSE
),
array(
new HTTP_Header(array('cache-control' => 'private, s-maxage=100')),
array('private' => NULL, 's-maxage' => '100'),
TRUE
),
array(
new HTTP_Header(array(
'expires' => date('m/d/Y', strtotime('-1 day')),
)),
array(),
FALSE
),
array(
new HTTP_Header(array(
'expires' => date('m/d/Y', strtotime('+1 day')),
)),
array(),
TRUE
),
array(
new HTTP_Header(array()),
array(),
TRUE
),
);
}
/**
* Tests the set_cache() method
*
* @test
* @dataProvider provider_set_cache
*
* @return null
*/
public function test_set_cache($headers, $cache_control, $expected)
{
/**
* Set up a mock response object to test with
*/
$response = $this->getMock('Response');
$response->expects($this->any())
->method('headers')
->will($this->returnValue($headers));
$request = new Request_Client_Internal;
$request->cache(new HTTP_Cache);
$this->assertEquals($request->cache()->set_cache($response), $expected);
}
/**
* Returns a mock object for Cache
*
* @return Cache
*/
protected function _get_cache_mock()
{
return $this->getMock('Cache_File', array(), array(), '', FALSE);
}
} // End Kohana_Request_Client_CacheTest
class Controller_Kohana_Request_CacheTest_Dummy extends Controller
{
public function action_index()
{
}
}

19
modules/cache/tests/phpunit.xml vendored Normal file
View File

@@ -0,0 +1,19 @@
<!--
This is an example phpunit.xml file to get you started
Copy it to a directory, update the relative paths and rename to phpunit.xml
Then to run tests cd into it's directory and just run
phpunit
(it'll automatically use any phpunit.xml file in the current directory)
Any options you specify when calling phpunit will override the ones in here
-->
<php>
<includePath>./cache</includePath>
</php>
<phpunit colors="true" bootstrap="/path/to/your/unittest/bootstrap.php">
<testsuites>
<testsuite name="Kohana Cache Tests">
<directory suffix=".php">cache/</directory>
</testsuite>
</testsuites>
</phpunit>