Release 1.6.1, merged in 931 to HEAD.
[htmlpurifier.git] / tests / HTMLPurifier / Harness.php
blob6e94e5ab0198bfd49e6ab7f12f96979c48d3e3ac
1 <?php
3 require_once 'HTMLPurifier/Lexer/DirectLex.php';
5 /**
6 * General-purpose test-harness that makes testing functions that require
7 * configuration and context objects easier when those two parameters are
8 * meaningless. See HTMLPurifier_ChildDefTest for a good example of usage.
9 */
10 class HTMLPurifier_Harness extends UnitTestCase
13 /**
14 * Instance of the object that will execute the method
16 var $obj;
18 /**
19 * Name of the function to be executed
21 var $func;
23 /**
24 * Whether or not the method deals in tokens. If set to true, assertResult()
25 * will transparently convert HTML to and back from tokens.
27 var $to_tokens = false;
29 /**
30 * Whether or not to convert tokens back into HTML before performing
31 * equality check, has no effect on bools.
33 var $to_html = false;
35 /**
36 * Instance of an HTMLPurifier_Lexer implementation.
38 var $lexer;
40 /**
41 * Instance of HTMLPurifier_Generator
43 var $generator;
45 function HTMLPurifier_Harness() {
46 $this->lexer = new HTMLPurifier_Lexer_DirectLex();
47 $this->generator = new HTMLPurifier_Generator();
48 parent::UnitTestCase();
51 /**
52 * Asserts a specific result from a one parameter + config/context function
53 * @param $input Input parameter
54 * @param $expect Expectation
55 * @param $config_array Configuration array in form of
56 * Namespace.Directive => Value or an actual config
57 * object.
58 * @param $context_array Context array in form of Key => Value or an actual
59 * context object.
61 function assertResult($input, $expect = true,
62 $config_array = array(), $context_array = array()
63 ) {
65 // setup config object
66 $config = HTMLPurifier_Config::createDefault();
67 $config->loadArray($config_array);
69 // setup context object. Note that we are operating on a copy of it!
70 // We will extend the test harness to allow you to do post-tests
71 // on the context object
72 $context = new HTMLPurifier_Context();
73 $context->loadArray($context_array);
75 if ($this->to_tokens && is_string($input)) {
76 $input = $this->lexer->tokenizeHTML($input, $config, $context);
79 // call the function
80 $func = $this->func;
81 $result = $this->obj->$func($input, $config, $context);
83 // test a bool result
84 if (is_bool($result)) {
85 $this->assertIdentical($expect, $result);
86 return;
87 } elseif (is_bool($expect)) {
88 $expect = $input;
91 if ($this->to_html) {
92 $result = $this->generator->
93 generateFromTokens($result, $config, $context);
94 if (is_array($expect)) {
95 $expect = $this->generator->
96 generateFromTokens($expect, $config, $context);
100 $this->assertIdentical($expect, $result);