LimeTestSuite.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 LimeTestSuite extends LimeRegistration
  12. {
  13. protected
  14. $options = array(),
  15. $executable = null,
  16. $output = null;
  17. public function __construct(array $options = array())
  18. {
  19. $this->options = array_merge(array(
  20. 'base_dir' => null,
  21. 'executable' => null,
  22. 'output' => 'summary',
  23. 'force_colors' => false,
  24. 'verbose' => false,
  25. 'serialize' => false,
  26. 'processes' => 1,
  27. ), $options);
  28. foreach (LimeShell::parseArguments($GLOBALS['argv']) as $argument => $value)
  29. {
  30. $this->options[str_replace('-', '_', $argument)] = $value;
  31. }
  32. $this->options['base_dir'] = realpath($this->options['base_dir']);
  33. if (is_string($this->options['output']))
  34. {
  35. $factory = new LimeOutputFactory($this->options);
  36. $type = $this->options['output'];
  37. $output = $factory->create($type);
  38. }
  39. else
  40. {
  41. $output = $this->options['output'];
  42. $type = get_class($output);
  43. }
  44. if ($this->options['processes'] > 1 && !$output->supportsThreading())
  45. {
  46. throw new LogicException(sprintf('The output "%s" does not support multi-processing', $type));
  47. }
  48. $this->output = new LimeOutputInspectable($output);
  49. }
  50. public function run()
  51. {
  52. if (!count($this->files))
  53. {
  54. throw new Exception('You must register some test files before running them!');
  55. }
  56. // sort the files to be able to predict the order
  57. sort($this->files);
  58. reset($this->files);
  59. $connectors = array();
  60. for ($i = 0; $i < $this->options['processes']; ++$i)
  61. {
  62. $connectors[] = new LimeTestAnalyzer($this->output, array('focus', 'close', 'flush'));
  63. }
  64. do
  65. {
  66. $done = true;
  67. foreach ($connectors as $connector)
  68. {
  69. if ($connector->done() && !is_null(key($this->files)))
  70. {
  71. // start and close the file explicitly in case the file contains syntax errors
  72. $this->output->focus(current($this->files));
  73. $connector->connect(current($this->files));
  74. next($this->files);
  75. }
  76. if (!$connector->done())
  77. {
  78. $this->output->focus($connector->getConnectedFile());
  79. $connector->proceed();
  80. $done = false;
  81. if ($connector->done())
  82. {
  83. // start and close the file explicitly in case the file contains syntax errors
  84. $this->output->close();
  85. }
  86. }
  87. }
  88. }
  89. while (!$done);
  90. $this->output->flush();
  91. $planned = $this->output->getPlanned();
  92. $passed = $this->output->getPassed();
  93. $failed = $this->output->getFailed();
  94. $errors = $this->output->getErrors();
  95. $warnings = $this->output->getWarnings();
  96. return 0 == ($failed + $errors + $warnings) && $planned == $passed;
  97. }
  98. }