Codecept.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace Codeception;
  3. use Codeception\Exception\ConfigurationException;
  4. use Codeception\Subscriber\ExtensionLoader;
  5. use Symfony\Component\EventDispatcher\EventDispatcher;
  6. class Codecept
  7. {
  8. const VERSION = "2.3.6";
  9. /**
  10. * @var \Codeception\PHPUnit\Runner
  11. */
  12. protected $runner;
  13. /**
  14. * @var \PHPUnit_Framework_TestResult
  15. */
  16. protected $result;
  17. /**
  18. * @var \Codeception\CodeCoverage
  19. */
  20. protected $coverage;
  21. /**
  22. * @var \Symfony\Component\EventDispatcher\EventDispatcher
  23. */
  24. protected $dispatcher;
  25. /**
  26. * @var ExtensionLoader
  27. */
  28. protected $extensionLoader;
  29. /**
  30. * @var array
  31. */
  32. protected $options = [
  33. 'silent' => false,
  34. 'debug' => false,
  35. 'steps' => false,
  36. 'html' => false,
  37. 'xml' => false,
  38. 'json' => false,
  39. 'tap' => false,
  40. 'report' => false,
  41. 'colors' => false,
  42. 'coverage' => false,
  43. 'coverage-xml' => false,
  44. 'coverage-html' => false,
  45. 'coverage-text' => false,
  46. 'coverage-crap4j' => false,
  47. 'groups' => null,
  48. 'excludeGroups' => null,
  49. 'filter' => null,
  50. 'env' => null,
  51. 'fail-fast' => false,
  52. 'ansi' => true,
  53. 'verbosity' => 1,
  54. 'interactive' => true,
  55. 'no-rebuild' => false,
  56. 'quiet' => false,
  57. ];
  58. protected $config = [];
  59. /**
  60. * @var array
  61. */
  62. protected $extensions = [];
  63. public function __construct($options = [])
  64. {
  65. $this->result = new \PHPUnit_Framework_TestResult;
  66. $this->dispatcher = new EventDispatcher();
  67. $this->extensionLoader = new ExtensionLoader($this->dispatcher);
  68. $baseOptions = $this->mergeOptions($options);
  69. $this->extensionLoader->bootGlobalExtensions($baseOptions); // extensions may override config
  70. $this->config = Configuration::config();
  71. $this->options = $this->mergeOptions($options); // options updated from config
  72. $this->registerSubscribers();
  73. $this->registerPHPUnitListeners();
  74. $this->registerPrinter();
  75. }
  76. /**
  77. * Merges given options with default values and current configuration
  78. *
  79. * @param array $options options
  80. * @return array
  81. * @throws ConfigurationException
  82. */
  83. protected function mergeOptions($options)
  84. {
  85. $config = Configuration::config();
  86. $baseOptions = array_merge($this->options, $config['settings']);
  87. return array_merge($baseOptions, $options);
  88. }
  89. protected function registerPHPUnitListeners()
  90. {
  91. $listener = new PHPUnit\Listener($this->dispatcher);
  92. $this->result->addListener($listener);
  93. }
  94. public function registerSubscribers()
  95. {
  96. // required
  97. $this->dispatcher->addSubscriber(new Subscriber\GracefulTermination());
  98. $this->dispatcher->addSubscriber(new Subscriber\ErrorHandler());
  99. $this->dispatcher->addSubscriber(new Subscriber\Dependencies());
  100. $this->dispatcher->addSubscriber(new Subscriber\Bootstrap());
  101. $this->dispatcher->addSubscriber(new Subscriber\PrepareTest());
  102. $this->dispatcher->addSubscriber(new Subscriber\Module());
  103. $this->dispatcher->addSubscriber(new Subscriber\BeforeAfterTest());
  104. // optional
  105. if (!$this->options['no-rebuild']) {
  106. $this->dispatcher->addSubscriber(new Subscriber\AutoRebuild());
  107. }
  108. if (!$this->options['silent']) {
  109. $this->dispatcher->addSubscriber(new Subscriber\Console($this->options));
  110. }
  111. if ($this->options['fail-fast']) {
  112. $this->dispatcher->addSubscriber(new Subscriber\FailFast());
  113. }
  114. if ($this->options['coverage']) {
  115. $this->dispatcher->addSubscriber(new Coverage\Subscriber\Local($this->options));
  116. $this->dispatcher->addSubscriber(new Coverage\Subscriber\LocalServer($this->options));
  117. $this->dispatcher->addSubscriber(new Coverage\Subscriber\RemoteServer($this->options));
  118. $this->dispatcher->addSubscriber(new Coverage\Subscriber\Printer($this->options));
  119. }
  120. $this->dispatcher->addSubscriber($this->extensionLoader);
  121. $this->extensionLoader->registerGlobalExtensions();
  122. }
  123. public function run($suite, $test = null, array $config = null)
  124. {
  125. ini_set(
  126. 'memory_limit',
  127. isset($this->config['settings']['memory_limit']) ? $this->config['settings']['memory_limit'] : '1024M'
  128. );
  129. $config = $config ?: Configuration::config();
  130. $settings = Configuration::suiteSettings($suite, $config);
  131. $selectedEnvironments = $this->options['env'];
  132. $environments = Configuration::suiteEnvironments($suite);
  133. if (!$selectedEnvironments or empty($environments)) {
  134. $this->runSuite($settings, $suite, $test);
  135. return;
  136. }
  137. foreach (array_unique($selectedEnvironments) as $envList) {
  138. $envArray = explode(',', $envList);
  139. $config = [];
  140. foreach ($envArray as $env) {
  141. if (isset($environments[$env])) {
  142. $currentEnvironment = isset($config['current_environment']) ? [$config['current_environment']] : [];
  143. $config = Configuration::mergeConfigs($config, $environments[$env]);
  144. $currentEnvironment[] = $config['current_environment'];
  145. $config['current_environment'] = implode(',', $currentEnvironment);
  146. }
  147. }
  148. if (empty($config)) {
  149. continue;
  150. }
  151. $suiteToRun = $suite;
  152. if (!empty($envList)) {
  153. $suiteToRun .= ' (' . implode(', ', $envArray) . ')';
  154. }
  155. $this->runSuite($config, $suiteToRun, $test);
  156. }
  157. }
  158. public function runSuite($settings, $suite, $test = null)
  159. {
  160. $suiteManager = new SuiteManager($this->dispatcher, $suite, $settings);
  161. $suiteManager->initialize();
  162. $suiteManager->loadTests($test);
  163. $suiteManager->run($this->runner, $this->result, $this->options);
  164. return $this->result;
  165. }
  166. public static function versionString()
  167. {
  168. return 'Codeception PHP Testing Framework v' . self::VERSION;
  169. }
  170. public function printResult()
  171. {
  172. $result = $this->getResult();
  173. $result->flushListeners();
  174. $printer = $this->runner->getPrinter();
  175. $printer->printResult($result);
  176. $this->dispatcher->dispatch(Events::RESULT_PRINT_AFTER, new Event\PrintResultEvent($result, $printer));
  177. }
  178. /**
  179. * @return \PHPUnit_Framework_TestResult
  180. */
  181. public function getResult()
  182. {
  183. return $this->result;
  184. }
  185. public function getOptions()
  186. {
  187. return $this->options;
  188. }
  189. /**
  190. * @return EventDispatcher
  191. */
  192. public function getDispatcher()
  193. {
  194. return $this->dispatcher;
  195. }
  196. protected function registerPrinter()
  197. {
  198. $printer = new PHPUnit\ResultPrinter\UI($this->dispatcher, $this->options);
  199. $this->runner = new PHPUnit\Runner();
  200. $this->runner->setPrinter($printer);
  201. }
  202. }