PSR-2 reformatting PHPDoc corrections
[htmlpurifier.git] / tests / HTMLPurifier / ComplexHarness.php
blobeee718a972f3f87b6695ad099fa01c91118d723e
1 <?php
3 /**
4 * General-purpose test-harness that makes testing functions that require
5 * configuration and context objects easier when those two parameters are
6 * meaningless. See HTMLPurifier_ChildDefTest for a good example of usage.
7 */
8 class HTMLPurifier_ComplexHarness extends HTMLPurifier_Harness
11 /**
12 * Instance of the object that will execute the method.
13 * @type object
15 protected $obj;
17 /**
18 * Name of the function to be executed.
19 * @type string
21 protected $func;
23 /**
24 * Whether or not the method deals in tokens.
25 * If set to true, assertResult()
26 * will transparently convert HTML to and back from tokens.
27 * @type bool
29 protected $to_tokens = false;
31 /**
32 * Whether or not to convert tokens back into HTML before performing
33 * equality check, has no effect on bools.
34 * @type bool
36 protected $to_html = false;
38 /**
39 * Instance of an HTMLPurifier_Lexer implementation.
40 * @type HTMLPurifier_Lexer
42 protected $lexer;
44 public function __construct()
46 $this->lexer = new HTMLPurifier_Lexer_DirectLex();
47 parent::__construct();
50 /**
51 * Asserts a specific result from a one parameter + config/context function
52 * @param string $input Input parameter
53 * @param bool|string $expect Expectation
55 protected function assertResult($input, $expect = true)
57 if ($this->to_tokens && is_string($input)) {
58 // $func may cause $input to change, so "clone" another copy
59 // to sacrifice
60 $input = $this->tokenize($temp = $input);
61 $input_c = $this->tokenize($temp);
62 } else {
63 $input_c = $input;
66 // call the function
67 $func = $this->func;
68 $result = $this->obj->$func($input_c, $this->config, $this->context);
70 // test a bool result
71 if (is_bool($result)) {
72 $this->assertIdentical($expect, $result);
73 return;
74 } elseif (is_bool($expect)) {
75 $expect = $input;
78 if ($this->to_html) {
79 $result = $this->generate($result);
80 if (is_array($expect)) {
81 $expect = $this->generate($expect);
84 $this->assertIdentical($expect, $result);
86 if ($expect !== $result) {
87 echo '<pre>' . var_dump($result) . '</pre>';
92 /**
93 * Tokenize HTML into tokens, uses member variables for common variables
95 protected function tokenize($html)
97 return $this->lexer->tokenizeHTML($html, $this->config, $this->context);
101 * Generate textual HTML from tokens
103 protected function generate($tokens)
105 $generator = new HTMLPurifier_Generator($this->config, $this->context);
106 return $generator->generateFromTokens($tokens);
111 // vim: et sw=4 sts=4