1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-31 19:30:21 +02:00

[1.7.0] Add DefinitionCache decorators, implement Memory decorator

- Move serialization responsibility to Config
- Create DefinitionCacheFactory
- Implement Null definition cache

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1117 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2007-05-29 20:21:33 +00:00
parent d1187ed331
commit 002395de09
16 changed files with 509 additions and 75 deletions

View File

@@ -0,0 +1,79 @@
<?php
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
require_once 'HTMLPurifier/DefinitionCache/Decorator/Memory.php';
generate_mock_once('HTMLPurifier_DefinitionCache');
class HTMLPurifier_DefinitionCache_Decorator_MemoryTest extends HTMLPurifier_DefinitionCacheHarness
{
function setup() {
unset($this->mock);
unset($this->cache);
$this->mock =& new HTMLPurifier_DefinitionCacheMock($this);
$this->mock->type = 'Test';
$this->cache = new HTMLPurifier_DefinitionCache_Decorator_Memory();
$this->cache = $this->cache->decorate($this->mock);
$this->def = $this->generateDefinition();
$this->config = $this->generateConfigMock();
}
function test_get() {
$this->mock->expectOnce('get', array($this->config)); // only ONE call!
$this->mock->setReturnValue('get', $this->def, array($this->config));
$this->assertEqual($this->cache->get($this->config), $this->def);
$this->assertEqual($this->cache->get($this->config), $this->def);
}
function setupMockForSuccess($op) {
$this->mock->expectOnce($op, array($this->def, $this->config));
$this->mock->setReturnValue($op, true, array($this->def, $this->config));
$this->mock->expectNever('get');
}
function setupMockForFailure($op) {
$this->mock->expectOnce($op, array($this->def, $this->config));
$this->mock->setReturnValue($op, false, array($this->def, $this->config));
$this->mock->expectOnce('get', array($this->config));
}
function test_set() {
$this->setupMockForSuccess('set');
$this->assertEqual($this->cache->set($this->def, $this->config), true);
$this->assertEqual($this->cache->get($this->config), $this->def);
}
function test_set_failure() {
$this->setupMockForFailure('set');
$this->assertEqual($this->cache->set($this->def, $this->config), false);
$this->cache->get($this->config);
}
function test_replace() {
$this->setupMockForSuccess('replace');
$this->assertEqual($this->cache->replace($this->def, $this->config), true);
$this->assertEqual($this->cache->get($this->config), $this->def);
}
function test_replace_failure() {
$this->setupMockForFailure('replace');
$this->assertEqual($this->cache->replace($this->def, $this->config), false);
$this->cache->get($this->config);
}
function test_add() {
$this->setupMockForSuccess('add');
$this->assertEqual($this->cache->add($this->def, $this->config), true);
$this->assertEqual($this->cache->get($this->config), $this->def);
}
function test_add_failure() {
$this->setupMockForFailure('add');
$this->assertEqual($this->cache->add($this->def, $this->config), false);
$this->cache->get($this->config);
}
}
?>

View File

@@ -0,0 +1,45 @@
<?php
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
class HTMLPurifier_DefinitionCache_DecoratorTest extends HTMLPurifier_DefinitionCacheHarness
{
function test() {
generate_mock_once('HTMLPurifier_DefinitionCache');
$mock =& new HTMLPurifier_DefinitionCacheMock($this);
$mock->type = 'Test';
$cache = new HTMLPurifier_DefinitionCache_Decorator();
$cache = $cache->decorate($mock);
$this->assertIdentical($cache->type, $mock->type);
$def = $this->generateDefinition();
$config = $this->generateConfigMock();
$mock->expectOnce('add', array($def, $config));
$cache->add($def, $config);
$mock->expectOnce('set', array($def, $config));
$cache->set($def, $config);
$mock->expectOnce('replace', array($def, $config));
$cache->replace($def, $config);
$mock->expectOnce('get', array($config));
$cache->get($config);
$mock->expectOnce('flush', array());
$cache->flush();
$mock->expectOnce('cleanup', array($config));
$cache->cleanup($config);
}
}
?>

View File

@@ -3,62 +3,18 @@
require_once 'HTMLPurifier/DefinitionCacheHarness.php';
require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
class HTMLPurifier_Definition_SerializerMock extends HTMLPurifier_Definition
{
var $_test;
var $_expect = false;
function HTMLPurifier_Definition_SerializerMock(&$test_case) {
$this->_test =& $test_case;
}
function expectDoSetupOnce() {$this->_expect = true;}
function doSetup($config) {
if ($this->_expect) {
$this->_test->pass();
} else {
$this->_test->fail('Unexpected call to doSetup');
}
unset($this->_test, $this->_expect);
}
}
class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_DefinitionCacheHarness
{
function test__SerializerMock_pass() {
$config = 'config';
generate_mock_once('UnitTestCase');
$test =& new UnitTestCaseMock($this);
$test->expectOnce('pass');
$mock = new HTMLPurifier_Definition_SerializerMock($test);
$mock->expectDoSetupOnce();
$mock->doSetup($config);
}
function test__SerializerMock_fail() {
$config = 'config';
generate_mock_once('UnitTestCase');
$test =& new UnitTestCaseMock($this);
$test->expectOnce('fail');
$mock = new HTMLPurifier_Definition_SerializerMock($test);
$mock->doSetup($config);
}
function test() {
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
$config_array = array('Foo' => 'Bar');
$config = $this->generateConfigMock($config_array);
$config = $this->generateConfigMock('serial');
$config->version = '1.0.0';
$config->revision = 2;
$config_md5 = '1.0.0-' . $config->revision . '-' . md5(serialize($config_array));
$config_md5 = '1.0.0-' . $config->revision . '-serial';
$file = realpath(
$rel_file = dirname(__FILE__) .
@@ -114,7 +70,7 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
$def = new HTMLPurifier_Definition();
$def->setup = true;
$def->type = 'NotTest';
$config = $this->generateConfigMock(array('Test' => 'foo'));
$config = $this->generateConfigMock('testfoo');
$this->expectError('Cannot use definition of type NotTest in cache for Test');
$cache->add($def, $config);
@@ -130,9 +86,9 @@ class HTMLPurifier_DefinitionCache_SerializerTest extends HTMLPurifier_Definitio
$cache = new HTMLPurifier_DefinitionCache_Serializer('Test');
$config1 = $this->generateConfigMock(array('Test' => 1));
$config2 = $this->generateConfigMock(array('Test' => 2));
$config3 = $this->generateConfigMock(array('Test' => 3));
$config1 = $this->generateConfigMock('test1');
$config2 = $this->generateConfigMock('test2');
$config3 = $this->generateConfigMock('test3');
$def1 = $this->generateDefinition(array('info_candles' => 1));
$def2 = $this->generateDefinition(array('info_candles' => 2));