LimeTestAnalyzer.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. class LimeTestAnalyzer
  12. {
  13. protected
  14. $suppressedMethods = array(),
  15. $output = null,
  16. $errors = '',
  17. $file = null,
  18. $process = null,
  19. $done = true,
  20. $parser = null;
  21. public function __construct(LimeOutputInterface $output, array $suppressedMethods = array())
  22. {
  23. $this->suppressedMethods = $suppressedMethods;
  24. $this->output = $output;
  25. }
  26. public function getConnectedFile()
  27. {
  28. return $this->file;
  29. }
  30. public function connect($file, array $arguments = array())
  31. {
  32. $arguments['output'] = 'raw';
  33. $this->file = $file;
  34. $this->done = false;
  35. $this->parser = null;
  36. $this->process = new LimeShellProcess($file, $arguments);
  37. $this->process->execute();
  38. }
  39. public function proceed()
  40. {
  41. $data = $this->process->getOutput();
  42. if (is_null($this->parser))
  43. {
  44. if (substr($data, 0, 5) == "\0raw\0")
  45. {
  46. $this->parser = new LimeParserRaw($this->output, $this->suppressedMethods);
  47. $data = substr($data, 5);
  48. }
  49. else
  50. {
  51. $this->parser = new LimeParserTap($this->output);
  52. }
  53. }
  54. $this->parser->parse($data);
  55. $this->errors .= $this->process->getErrors();
  56. while (preg_match('/^(.+)\n/', $this->errors, $matches))
  57. {
  58. $this->output->warning($matches[1], $this->file, 0);
  59. $this->errors = substr($this->errors, strlen($matches[0]));
  60. }
  61. if ($this->process->isClosed())
  62. {
  63. if (!$this->parser->done())
  64. {
  65. // FIXME: Should be handled in a better way
  66. $buffer = substr($this->parser->buffer, 0, strpos($this->parser->buffer, "\n"));
  67. $this->output->warning(sprintf('Could not parse test output: "%s"', $buffer), $this->file, 1);
  68. }
  69. // if the last error was not followed by \n, it is still in the buffer
  70. if (!empty($this->errors))
  71. {
  72. $this->output->warning($this->errors, $this->file, 0);
  73. $this->errors = '';
  74. }
  75. $this->done = true;
  76. }
  77. }
  78. public function done()
  79. {
  80. return $this->done;
  81. }
  82. }