CliTestCase.php 2.7 KB

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