Deal with old libxml incompatibilities.
[htmlpurifier.git] / tests / CliTestCase.php
blob0fc20ef05b6236c1a6502f1e4b1b52c337991170
1 <?php
3 /**
4 * Implements an external test-case like RemoteTestCase that parses its
5 * output from XML returned by a command line call
6 */
7 class CliTestCase
9 public $_command;
10 public $_out = false;
11 public $_quiet = false;
12 public $_errors = array();
13 public $_size = false;
14 /**
15 * @param $command Command to execute to retrieve XML
16 * @param $xml Whether or not to suppress error messages
18 public function __construct($command, $quiet = false, $size = false)
20 $this->_command = $command;
21 $this->_quiet = $quiet;
22 $this->_size = $size;
24 public function getLabel()
26 return $this->_command;
28 public function run($reporter)
30 if (!$this->_quiet) $reporter->paintFormattedMessage('Running ['.$this->_command.']');
31 return $this->_invokeCommand($this->_command, $reporter);
33 public function _invokeCommand($command, $reporter)
35 $xml = shell_exec($command);
36 if (! $xml) {
37 if (!$this->_quiet) {
38 $reporter->paintFail('Command did not have any output [' . $command . ']');
40 return false;
42 $parser = $this->_createParser($reporter);
44 set_error_handler(array($this, '_errorHandler'));
45 $status = $parser->parse($xml);
46 restore_error_handler();
48 if (! $status) {
49 if (!$this->_quiet) {
50 foreach ($this->_errors as $error) {
51 list($no, $str, $file, $line) = $error;
52 $reporter->paintFail("Error $no: $str on line $line of $file");
54 if (strlen($xml) > 120) {
55 $msg = substr($xml, 0, 50) . "...\n\n[snip]\n\n..." . substr($xml, -50);
56 } else {
57 $msg = $xml;
59 $reporter->paintFail("Command produced malformed XML");
60 $reporter->paintFormattedMessage($msg);
62 return false;
64 return true;
66 public function _createParser($reporter)
68 $parser = new SimpleTestXmlParser($reporter);
69 return $parser;
71 public function getSize()
73 // This code properly does the dry run and allows for proper test
74 // case reporting but it's REALLY slow, so I don't recommend it.
75 if ($this->_size === false) {
76 $reporter = new SimpleReporter();
77 $this->_invokeCommand($this->_command . ' --dry', $reporter);
78 $this->_size = $reporter->getTestCaseCount();
80 return $this->_size;
82 public function _errorHandler($a, $b, $c, $d)
84 $this->_errors[] = array($a, $b, $c, $d); // see set_error_handler()
88 // vim: et sw=4 sts=4