Fix newline issues in tests.
[htmlpurifier/darkodev.git] / tests / CliTestCase.php
blob53694c770bf3ea07262afce51f27195ff933c4ce
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) {
19 $this->_command = $command;
20 $this->_quiet = $quiet;
21 $this->_size = $size;
23 public function getLabel() {
24 return $this->_command;
26 public function run(&$reporter) {
27 if (!$this->_quiet) $reporter->paintFormattedMessage('Running ['.$this->_command.']');
28 return $this->_invokeCommand($this->_command, $reporter);
30 public function _invokeCommand($command, &$reporter) {
31 $xml = shell_exec($command);
32 if (! $xml) {
33 if (!$this->_quiet) {
34 $reporter->paintFail('Command did not have any output [' . $command . ']');
36 return false;
38 $parser = &$this->_createParser($reporter);
40 set_error_handler(array($this, '_errorHandler'));
41 $status = $parser->parse($xml);
42 restore_error_handler();
44 if (! $status) {
45 if (!$this->_quiet) {
46 foreach ($this->_errors as $error) {
47 list($no, $str, $file, $line) = $error;
48 $reporter->paintFail("Error $no: $str on line $line of $file");
50 if (strlen($xml) > 120) {
51 $msg = substr($xml, 0, 50) . "...\n\n[snip]\n\n..." . substr($xml, -50);
52 } else {
53 $msg = $xml;
55 $reporter->paintFail("Command produced malformed XML");
56 $reporter->paintFormattedMessage($msg);
58 return false;
60 return true;
62 public function &_createParser(&$reporter) {
63 $parser = new SimpleTestXmlParser($reporter);
64 return $parser;
66 public function getSize() {
67 // This code properly does the dry run and allows for proper test
68 // case reporting but it's REALLY slow, so I don't recommend it.
69 if ($this->_size === false) {
70 $reporter = new SimpleReporter();
71 $this->_invokeCommand($this->_command . ' --dry', $reporter);
72 $this->_size = $reporter->getTestCaseCount();
74 return $this->_size;
76 public function _errorHandler($a, $b, $c, $d) {
77 $this->_errors[] = array($a, $b, $c, $d); // see set_error_handler()