0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-19 18:55:19 +00:00
htmlpurifier/tests/HTMLPurifier/ComplexHarness.php
Marcus Bointon fac747bdbd PSR-2 reformatting PHPDoc corrections
With minor corrections.

Signed-off-by: Marcus Bointon <marcus@synchromedia.co.uk>
Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
2013-08-17 22:27:26 -04:00

112 lines
2.9 KiB
PHP

<?php
/**
* General-purpose test-harness that makes testing functions that require
* configuration and context objects easier when those two parameters are
* meaningless. See HTMLPurifier_ChildDefTest for a good example of usage.
*/
class HTMLPurifier_ComplexHarness extends HTMLPurifier_Harness
{
/**
* Instance of the object that will execute the method.
* @type object
*/
protected $obj;
/**
* Name of the function to be executed.
* @type string
*/
protected $func;
/**
* Whether or not the method deals in tokens.
* If set to true, assertResult()
* will transparently convert HTML to and back from tokens.
* @type bool
*/
protected $to_tokens = false;
/**
* Whether or not to convert tokens back into HTML before performing
* equality check, has no effect on bools.
* @type bool
*/
protected $to_html = false;
/**
* Instance of an HTMLPurifier_Lexer implementation.
* @type HTMLPurifier_Lexer
*/
protected $lexer;
public function __construct()
{
$this->lexer = new HTMLPurifier_Lexer_DirectLex();
parent::__construct();
}
/**
* Asserts a specific result from a one parameter + config/context function
* @param string $input Input parameter
* @param bool|string $expect Expectation
*/
protected function assertResult($input, $expect = true)
{
if ($this->to_tokens && is_string($input)) {
// $func may cause $input to change, so "clone" another copy
// to sacrifice
$input = $this->tokenize($temp = $input);
$input_c = $this->tokenize($temp);
} else {
$input_c = $input;
}
// call the function
$func = $this->func;
$result = $this->obj->$func($input_c, $this->config, $this->context);
// test a bool result
if (is_bool($result)) {
$this->assertIdentical($expect, $result);
return;
} elseif (is_bool($expect)) {
$expect = $input;
}
if ($this->to_html) {
$result = $this->generate($result);
if (is_array($expect)) {
$expect = $this->generate($expect);
}
}
$this->assertIdentical($expect, $result);
if ($expect !== $result) {
echo '<pre>' . var_dump($result) . '</pre>';
}
}
/**
* Tokenize HTML into tokens, uses member variables for common variables
*/
protected function tokenize($html)
{
return $this->lexer->tokenizeHTML($html, $this->config, $this->context);
}
/**
* Generate textual HTML from tokens
*/
protected function generate($tokens)
{
$generator = new HTMLPurifier_Generator($this->config, $this->context);
return $generator->generateFromTokens($tokens);
}
}
// vim: et sw=4 sts=4