2008-01-07 00:17:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements an external test-case like RemoteTestCase that parses its
|
|
|
|
* output from XML returned by a command line call
|
|
|
|
*/
|
|
|
|
class CliTestCase
|
|
|
|
{
|
|
|
|
public $_command;
|
|
|
|
public $_out = false;
|
|
|
|
public $_quiet = false;
|
|
|
|
public $_errors = array();
|
2008-01-21 20:27:26 +00:00
|
|
|
public $_size = false;
|
2008-01-07 00:17:49 +00:00
|
|
|
/**
|
|
|
|
* @param $command Command to execute to retrieve XML
|
|
|
|
* @param $xml Whether or not to suppress error messages
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function __construct($command, $quiet = false, $size = false)
|
|
|
|
{
|
2008-01-07 00:17:49 +00:00
|
|
|
$this->_command = $command;
|
2008-01-21 20:27:26 +00:00
|
|
|
$this->_quiet = $quiet;
|
|
|
|
$this->_size = $size;
|
2008-01-07 00:17:49 +00:00
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
public function getLabel()
|
|
|
|
{
|
2008-01-07 00:17:49 +00:00
|
|
|
return $this->_command;
|
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
public function run($reporter)
|
|
|
|
{
|
2008-01-07 00:17:49 +00:00
|
|
|
if (!$this->_quiet) $reporter->paintFormattedMessage('Running ['.$this->_command.']');
|
2008-01-21 20:27:26 +00:00
|
|
|
return $this->_invokeCommand($this->_command, $reporter);
|
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
public function _invokeCommand($command, $reporter)
|
|
|
|
{
|
2008-01-21 20:27:26 +00:00
|
|
|
$xml = shell_exec($command);
|
2008-01-07 00:17:49 +00:00
|
|
|
if (! $xml) {
|
|
|
|
if (!$this->_quiet) {
|
2008-02-18 03:35:27 +00:00
|
|
|
$reporter->paintFail('Command did not have any output [' . $command . ']');
|
2008-01-07 00:17:49 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-18 23:10:16 +00:00
|
|
|
$parser = $this->_createParser($reporter);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-07 00:17:49 +00:00
|
|
|
set_error_handler(array($this, '_errorHandler'));
|
|
|
|
$status = $parser->parse($xml);
|
|
|
|
restore_error_handler();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-07 00:17:49 +00:00
|
|
|
if (! $status) {
|
|
|
|
if (!$this->_quiet) {
|
|
|
|
foreach ($this->_errors as $error) {
|
|
|
|
list($no, $str, $file, $line) = $error;
|
2008-02-18 03:35:27 +00:00
|
|
|
$reporter->paintFail("Error $no: $str on line $line of $file");
|
2008-01-07 00:17:49 +00:00
|
|
|
}
|
|
|
|
if (strlen($xml) > 120) {
|
2008-02-18 03:35:27 +00:00
|
|
|
$msg = substr($xml, 0, 50) . "...\n\n[snip]\n\n..." . substr($xml, -50);
|
2008-01-07 00:17:49 +00:00
|
|
|
} else {
|
2008-02-18 03:35:27 +00:00
|
|
|
$msg = $xml;
|
2008-01-07 00:17:49 +00:00
|
|
|
}
|
2008-02-18 03:35:27 +00:00
|
|
|
$reporter->paintFail("Command produced malformed XML");
|
2008-01-07 00:17:49 +00:00
|
|
|
$reporter->paintFormattedMessage($msg);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
public function _createParser($reporter)
|
|
|
|
{
|
2008-01-07 00:17:49 +00:00
|
|
|
$parser = new SimpleTestXmlParser($reporter);
|
|
|
|
return $parser;
|
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
public function getSize()
|
|
|
|
{
|
2008-01-21 20:27:26 +00:00
|
|
|
// This code properly does the dry run and allows for proper test
|
|
|
|
// case reporting but it's REALLY slow, so I don't recommend it.
|
|
|
|
if ($this->_size === false) {
|
|
|
|
$reporter = new SimpleReporter();
|
|
|
|
$this->_invokeCommand($this->_command . ' --dry', $reporter);
|
|
|
|
$this->_size = $reporter->getTestCaseCount();
|
|
|
|
}
|
|
|
|
return $this->_size;
|
2008-01-07 00:17:49 +00:00
|
|
|
}
|
2013-07-16 11:56:14 +00:00
|
|
|
public function _errorHandler($a, $b, $c, $d)
|
|
|
|
{
|
2008-01-07 00:17:49 +00:00
|
|
|
$this->_errors[] = array($a, $b, $c, $d); // see set_error_handler()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|