CliRunner.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. require_once 'Sweety/Runner/AbstractTestRunner.php';
  3. /**
  4. * Runs SimpleTest cases as a group via the command line.
  5. * @package Sweety
  6. * @author Chris Corbyn
  7. */
  8. class Sweety_Runner_CliRunner extends Sweety_Runner_AbstractTestRunner
  9. {
  10. /**
  11. * Directories to scan for test cases.
  12. * @var string[]
  13. * @access private
  14. */
  15. private $_dirs = array();
  16. /**
  17. * The command to invoke when running test cases.
  18. * @var string
  19. * @access private
  20. */
  21. private $_command;
  22. /**
  23. * Creates a new CliRunner scanning the given directories, using the given
  24. * command and having the given name.
  25. * @param string[] $dirs
  26. * @param string $command
  27. * @param string $name
  28. */
  29. public function __construct(array $dirs, $command)
  30. {
  31. $this->_dirs = $dirs;
  32. $this->_command = $command;
  33. }
  34. /**
  35. * Runs all test cases found under the given directories.
  36. * @param string[] $directories to scan for test cases
  37. * @param string To be prepended to class names
  38. * @return int
  39. */
  40. public function runAllTests($dirs = array())
  41. {
  42. if (empty($dirs))
  43. {
  44. $dirs = $this->_dirs;
  45. }
  46. $reporter = $this->getReporter();
  47. if (!$reporter->isStarted())
  48. {
  49. $reporter->start();
  50. }
  51. $tests = $this->findTests($dirs);
  52. usort($tests, array($this, '_sort'));
  53. global $argv;
  54. if (!empty($argv[1]))
  55. {
  56. if (substr($argv[1], 0, 1) == '!')
  57. {
  58. $argv[1] = substr($argv[1], 1);
  59. foreach ($tests as $index => $name)
  60. {
  61. if (@preg_match($argv[1] . 'i', $name))
  62. {
  63. unset($tests[$index]);
  64. }
  65. }
  66. }
  67. else
  68. {
  69. foreach ($tests as $index => $name)
  70. {
  71. if (!@preg_match($argv[1] . 'i', $name))
  72. {
  73. unset($tests[$index]);
  74. }
  75. }
  76. }
  77. }
  78. $ret = $this->_runTestList($tests);
  79. $reporter->finish();
  80. return $ret;
  81. }
  82. /**
  83. * Run all possible tests from the given list.
  84. * @param string[] $tests
  85. * @return int
  86. */
  87. protected function _runTestList(array $tests)
  88. {
  89. foreach ($tests as $testCase)
  90. {
  91. if (preg_match($this->getIgnoredClassRegex(), $testCase))
  92. {
  93. continue;
  94. }
  95. $command = $this->_command;
  96. $command .= ' ' . $testCase;
  97. $command .= ' ' . Sweety_Runner::REPORT_XML;
  98. exec($command, $output, $status);
  99. $xml = implode(PHP_EOL, $output);
  100. $this->parseXml($xml, $testCase);
  101. unset($status);
  102. unset($output);
  103. }
  104. return 0;
  105. }
  106. }