You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 line
2.7KB

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