Пример #1
0
$m->remove('module/action?key1=value1');
$t->is($m->has('module/action?key1=value1'), false, '->remove() removes accepts an internal URI as first parameter');
$t->is($m->has('module/action?key2=value2'), true, '->remove() does not remove cache content for keys not matching the internal URI');
$m = get_cache_manager($context);
$m->addCache('module', 'action', get_cache_config());
$m->set('test', 'module/action?key1=value1');
$m->set('test', 'module/action?key1=value2');
$m->set('test', 'module/action?key2=value1');
$m->remove('module/action?key1=*');
$t->is($m->has('module/action?key1=value1'), false, '->remove() accepts wildcards in URIs and then removes all keys matching the pattern');
$t->is($m->has('module/action?key1=value2'), false, '->remove() accepts wildcards in URIs and then removes all keys matching the pattern');
$t->is($m->has('module/action?key2=value1'), true, '->remove() accepts wildcards in URIs and lets keys not matching the pattern unchanged');
function get_cache_manager($context)
{
    myCache::clear();
    $m = new myViewCacheManager($context, new myCache());
    return $m;
}
function get_cache_config($contextual = false)
{
    return array('withLayout' => false, 'lifeTime' => 86400, 'clientLifeTime' => 86400, 'contextual' => $contextual, 'vary' => array());
}
// ->initialize()
$t->diag('Cache key generation options');
$m = new myViewCacheManager($context, $cache = new myCache(), array('cache_key_use_vary_headers' => false));
$t->is($m->generateCacheKey('mymodule/myaction'), '/localhost/mymodule/myaction', '->generateCacheKey() uses "cache_key_use_vary_headers" option to know if vary headers changes cache key.');
$m = new myViewCacheManager($context, $cache = new myCache(), array('cache_key_use_host_name' => false));
$t->is($m->generateCacheKey('mymodule/myaction'), '/all/mymodule/myaction', '->generateCacheKey() uses "cache_key_use_host_name" option to know if vary headers changes cache key.');
$m = new myViewCacheManager($context, $cache = new myCache(), array('cache_key_use_host_name' => false, 'cache_key_use_vary_headers' => false));
$t->is($m->generateCacheKey('mymodule/myaction'), '/mymodule/myaction', '->generateCacheKey() allows the use of both "cache_key_use_host_name" and "cache_key_use_vary_headers" options.');
Пример #2
0
$t->is($m->has('module/action'), false, '->remove() removes cache content for an action');
$m->set('test', 'module/action?key1=value1');
$m->set('test', 'module/action?key2=value2');
$m->remove('module/action?key1=value1');
$t->is($m->has('module/action?key1=value1'), false, '->remove() removes accepts an internal URI as first parameter');
$t->is($m->has('module/action?key2=value2'), true, '->remove() does not remove cache content for keys not matching the internal URI');
$m = get_cache_manager($context);
$m->addCache('module', 'action', get_cache_config());
$m->set('test', 'module/action?key1=value1');
$m->set('test', 'module/action?key1=value2');
$m->set('test', 'module/action?key2=value1');
$m->remove('module/action?key1=*');
$t->is($m->has('module/action?key1=value1'), false, '->remove() accepts wildcards in URIs and then removes all keys matching the pattern');
$t->is($m->has('module/action?key1=value2'), false, '->remove() accepts wildcards in URIs and then removes all keys matching the pattern');
$t->is($m->has('module/action?key2=value1'), true, '->remove() accepts wildcards in URIs and lets keys not matching the pattern unchanged');
$t->diag('Cache key generation options');
$m = new myViewCacheManager($context, $cache = new myCache(), array('cache_key_use_vary_headers' => false));
$t->is($m->generateCacheKey('mymodule/myaction'), '/localhost/mymodule/myaction', '->generateCacheKey() uses "cache_key_use_vary_headers" option to know if vary headers changes cache key.');
$m = new myViewCacheManager($context, $cache = new myCache(), array('cache_key_use_host_name' => false));
$t->is($m->generateCacheKey('mymodule/myaction'), '/all/mymodule/myaction', '->generateCacheKey() uses "cache_key_use_host_name" option to know if vary headers changes cache key.');
$m = new myViewCacheManager($context, $cache = new myCache(), array('cache_key_use_host_name' => false, 'cache_key_use_vary_headers' => false));
$t->is($m->generateCacheKey('mymodule/myaction'), '/mymodule/myaction', '->generateCacheKey() allows the use of both "cache_key_use_host_name" and "cache_key_use_vary_headers" options.');
$m = new myViewCacheManager($context, new myCache());
$t->is($m->generateCacheKey('mymodule/myaction?foo=../_bar'), '/localhost/all/mymodule/myaction/foo/_../__bar', '->generateCacheKey() prevents directory traversal');
$t->is($m->generateCacheKey('mymodule/myaction?foo=..\\_bar'), '/localhost/all/mymodule/myaction/foo/_..\\__bar', '->generateCacheKey() prevents directory traversal');
// ->getCurrentCacheKey()
$t->diag('->getCurrentCacheKey()');
$m = get_cache_manager($context);
$t->is($m->getCurrentCacheKey(), 'currentModule/currentAction?currentKey=currentValue&page=5&sort=asc', '->getCurrentCacheKey() appends GET parameters to an existing query string');
$context->getRouting()->currentInternalUri = 'currentModule/currentAction';
$t->is($m->getCurrentCacheKey(), 'currentModule/currentAction?page=5&sort=asc', '->getCurrentCacheKey() adds a query string of GET parameters if none is there');
Пример #3
0
{
    public function getCurrentInternalUri($with_route_name = false)
    {
        return 'currentModule/currentAction?currentKey=currentValue';
    }
}
$context = sfContext::getInstance(array('controller' => 'myController', 'routing' => 'myRouting', 'request' => 'myRequest'));
$r = $context->routing;
$r->connect('default', new sfRoute('/:module/:action/*'));
// ->initialize()
$t->diag('->initialize()');
$m = new myViewCacheManager($context, $cache = new myCache());
$t->is($m->getCache(), $cache, '->initialize() takes a sfCache object as its second argument');
// ->generateCacheKey()
$t->diag('->generateCacheKey');
$t->is($m->generateCacheKey('mymodule/myaction'), '/localhost/all/mymodule/myaction', '->generateCacheKey() creates a simple cache key from an internal URI');
$t->is($m->generateCacheKey('mymodule/myaction', 'foo'), '/foo/all/mymodule/myaction', '->generateCacheKey() can take a hostName as second parameter');
$t->is($m->generateCacheKey('mymodule/myaction', null, 'bar'), '/localhost/bar/mymodule/myaction', '->generateCacheKey() can take a serialized set of vary headers as third parameter');
$t->is($m->generateCacheKey('mymodule/myaction?key1=value1&key2=value2'), '/localhost/all/mymodule/myaction/key1/value1/key2/value2', '->generateCacheKey() includes request parameters as key/value pairs');
$t->is($m->generateCacheKey('mymodule/myaction?akey=value1&ckey=value2&bkey=value3'), '/localhost/all/mymodule/myaction/akey/value1/bkey/value3/ckey/value2', '->generateCacheKey() reorders request parameters alphabetically');
try {
    $m->generateCacheKey('@rule?key=value');
    $t->fail('->generateCacheKey() throws an sfException when passed an internal URI with a rule');
} catch (sfException $e) {
    $t->pass('->generateCacheKey() throws an sfException when passed an internal URI with a rule');
}
try {
    $m->generateCacheKey('@sf_cache_partial?module=mymodule&action=myaction');
    $t->pass('->generateCacheKey() does not throw an sfException when passed an internal URI with a @sf_cache_partial rule');
} catch (sfException $e) {
    $t->fail('->generateCacheKey() does not throw an sfException when passed an internal URI with a @sf_cache_partial rule');