PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Codeception/Codecept.php

https://github.com/pmcjury/Codeception
PHP | 209 lines | 147 code | 34 blank | 28 comment | 13 complexity | 2300b62bc41b9d817dd5c4efe6ced667 MD5 | raw file
  1. <?php
  2. namespace Codeception;
  3. use Codeception\Configuration;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use \Symfony\Component\Finder\Finder;
  6. use \Symfony\Component\EventDispatcher\EventDispatcher;
  7. use Codeception\Exception\Configuration as ConfigurationException;
  8. class Codecept
  9. {
  10. const VERSION = "2.0.0";
  11. /**
  12. * @var \Codeception\PHPUnit\Runner
  13. */
  14. protected $runner;
  15. /**
  16. * @var \PHPUnit_Framework_TestResult
  17. */
  18. protected $result;
  19. /**
  20. * @var \Codeception\CodeCoverage
  21. */
  22. protected $coverage;
  23. /**
  24. * @var \Symfony\Component\EventDispatcher\EventDispatcher
  25. */
  26. protected $dispatcher;
  27. /**
  28. * @var array
  29. */
  30. protected $options = array(
  31. 'silent' => false,
  32. 'debug' => false,
  33. 'steps' => false,
  34. 'html' => false,
  35. 'xml' => false,
  36. 'json' => false,
  37. 'tap' => false,
  38. 'report' => false,
  39. 'colors' => false,
  40. 'coverage' => false,
  41. 'coverage-xml' => false,
  42. 'coverage-html' => false,
  43. 'coverage-text' => false,
  44. 'groups' => null,
  45. 'excludeGroups' => null,
  46. 'filter' => null,
  47. 'env' => null,
  48. 'fail-fast' => false,
  49. );
  50. /**
  51. * @var array
  52. */
  53. protected $extensions = array();
  54. public function __construct($options = array()) {
  55. $this->result = new \PHPUnit_Framework_TestResult;
  56. $this->dispatcher = new EventDispatcher();
  57. $this->loadExtensions($options);
  58. $this->config = Configuration::config();
  59. $this->options = $this->mergeOptions($options);
  60. $this->registerSubscribers();
  61. $this->registerPHPUnitListeners();
  62. $printer = new PHPUnit\ResultPrinter\UI($this->dispatcher, $this->options);
  63. $this->runner = new PHPUnit\Runner($this->options);
  64. $this->runner->setPrinter($printer);
  65. }
  66. private function mergeOptions($options) {
  67. foreach ($this->options as $option => $default) {
  68. $value = isset($options[$option]) ? $options[$option] : $default;
  69. if (!$value) {
  70. $options[$option] = isset($this->config['settings'][$option])
  71. ? $this->config['settings'][$option]
  72. : $this->options[$option];
  73. }
  74. }
  75. if ($options['no-colors']) $options['colors'] = false;
  76. if ($options['report']) $options['silent'] = true;
  77. if ($options['group']) $options['groups'] = $options['group'];
  78. if ($options['skip-group']) $options['excludeGroups'] = $options['skip-group'];
  79. if ($options['coverage-xml'] or $options['coverage-html']) $options['coverage'] = true;
  80. return $options;
  81. }
  82. protected function loadExtensions($options)
  83. {
  84. $config = Configuration::config();
  85. // custom event listeners
  86. foreach ($config['extensions']['enabled'] as $extension) {
  87. if (!class_exists($extension)) {
  88. throw new ConfigurationException("Class $extension not defined. Autoload it or include into '_bootstrap.php' file of 'tests' directory");
  89. }
  90. if ($extension instanceof EventSubscriberInterface) {
  91. throw new ConfigurationException("Class $extension is not a EventListener. Please create it as Extension or Group class.");
  92. }
  93. $extensionConfig = isset($this->config['extensions']['config'][$extension])
  94. ? $this->config['extensions']['config'][$extension]
  95. : [];
  96. $this->extensions[] = new $extension($extensionConfig, $options);
  97. }
  98. }
  99. protected function registerPHPUnitListeners() {
  100. $listener = new PHPUnit\Listener($this->dispatcher);
  101. $this->result->addListener($listener);
  102. }
  103. public function registerSubscribers() {
  104. // required
  105. $this->dispatcher->addSubscriber(new Subscriber\ErrorHandler());
  106. $this->dispatcher->addSubscriber(new Subscriber\Module());
  107. $this->dispatcher->addSubscriber(new Subscriber\BeforeAfterTest());
  108. $this->dispatcher->addSubscriber(new Subscriber\AutoRebuild());
  109. $this->dispatcher->addSubscriber(new Subscriber\Bootstrap());
  110. // optional
  111. if (!$this->options['silent']) $this->dispatcher->addSubscriber(new Subscriber\Console($this->options));
  112. if ($this->options['fail-fast']) $this->dispatcher->addSubscriber(new Subscriber\FailFast());
  113. if ($this->options['coverage']) {
  114. $this->dispatcher->addSubscriber(new Coverage\Subscriber\Local($this->options));
  115. $this->dispatcher->addSubscriber(new Coverage\Subscriber\LocalServer($this->options));
  116. $this->dispatcher->addSubscriber(new Coverage\Subscriber\RemoteServer($this->options));
  117. $this->dispatcher->addSubscriber(new Coverage\Subscriber\Printer($this->options));
  118. }
  119. // extensions
  120. foreach ($this->extensions as $subscriber) {
  121. $this->dispatcher->addSubscriber($subscriber);
  122. }
  123. }
  124. public function run($suite, $test = null)
  125. {
  126. ini_set('memory_limit', isset($this->config['settings']['memory_limit']) ? $this->config['settings']['memory_limit'] : '1024M');
  127. $settings = Configuration::suiteSettings($suite, Configuration::config());
  128. $selectedEnvironments = $this->options['env'];
  129. $environments = Configuration::suiteEnvironments($suite);
  130. if (!$selectedEnvironments or empty($environments)) {
  131. $this->runSuite($settings, $suite, $test);
  132. return;
  133. }
  134. foreach ($environments as $env => $config) {
  135. if (!in_array($env, $selectedEnvironments)) {
  136. continue;
  137. }
  138. $suiteToRun = is_int($env) ? $suite : "{$suite}-{$env}";
  139. $this->runSuite($config, $suiteToRun, $test);
  140. }
  141. }
  142. public function runSuite($settings, $suite, $test = null) {
  143. $suiteManager = new SuiteManager($this->dispatcher, $suite, $settings);
  144. $suiteManager->initialize();
  145. $suiteManager->loadTests($test);
  146. $suiteManager->run($this->runner, $this->result, $this->options);
  147. return $this->result;
  148. }
  149. public static function versionString() {
  150. return 'Codeception PHP Testing Framework v'.self::VERSION;
  151. }
  152. public function printResult() {
  153. $result = $this->getResult();
  154. $result->flushListeners();
  155. $printer = $this->runner->getPrinter();
  156. $printer->printResult($result);
  157. $this->dispatcher->dispatch(Events::RESULT_PRINT_AFTER, new Event\PrintResultEvent($result, $printer));
  158. }
  159. /**
  160. * @return \PHPUnit_Framework_TestResult
  161. */
  162. public function getResult() {
  163. return $this->result;
  164. }
  165. public function getOptions() {
  166. return $this->options;
  167. }
  168. /**
  169. * @return EventDispatcher
  170. */
  171. public function getDispatcher()
  172. {
  173. return $this->dispatcher;
  174. }
  175. }