PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/codeception/codeception/src/Codeception/Codecept.php

https://gitlab.com/itlboy/yii2-starter-installed
PHP | 249 lines | 180 code | 35 blank | 34 comment | 10 complexity | 173b4a9f744d84ab6ec2aa28fe95c9bc MD5 | raw file
  1. <?php
  2. namespace Codeception;
  3. use Codeception\Exception\ConfigurationException;
  4. use Symfony\Component\EventDispatcher\EventDispatcher;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. class Codecept
  7. {
  8. const VERSION = "2.2.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 array
  27. */
  28. protected $options = [
  29. 'silent' => false,
  30. 'debug' => false,
  31. 'steps' => false,
  32. 'html' => false,
  33. 'xml' => false,
  34. 'json' => false,
  35. 'tap' => false,
  36. 'report' => false,
  37. 'colors' => false,
  38. 'coverage' => false,
  39. 'coverage-xml' => false,
  40. 'coverage-html' => false,
  41. 'coverage-text' => false,
  42. 'groups' => null,
  43. 'excludeGroups' => null,
  44. 'filter' => null,
  45. 'env' => null,
  46. 'fail-fast' => false,
  47. 'ansi' => true,
  48. 'verbosity' => 1,
  49. 'interactive' => true,
  50. 'no-rebuild' => false,
  51. 'quiet' => false,
  52. ];
  53. protected $config = [];
  54. /**
  55. * @var array
  56. */
  57. protected $extensions = [];
  58. public function __construct($options = [])
  59. {
  60. $this->result = new \PHPUnit_Framework_TestResult;
  61. $this->dispatcher = new EventDispatcher();
  62. $baseOptions = $this->mergeOptions($options);
  63. $this->loadExtensions($baseOptions);
  64. $this->config = Configuration::config();
  65. $this->options = $this->mergeOptions($options);
  66. $this->registerSubscribers();
  67. $this->registerPHPUnitListeners();
  68. $printer = new PHPUnit\ResultPrinter\UI($this->dispatcher, $this->options);
  69. $this->runner = new PHPUnit\Runner();
  70. $this->runner->setPrinter($printer);
  71. }
  72. /**
  73. * Merges given options with default values and current configuration
  74. *
  75. * @param array $options options
  76. * @return array
  77. * @throws ConfigurationException
  78. */
  79. protected function mergeOptions($options)
  80. {
  81. $config = Configuration::config();
  82. $baseOptions = array_merge($this->options, $config['settings']);
  83. return array_merge($baseOptions, $options);
  84. }
  85. protected function loadExtensions($options)
  86. {
  87. $config = Configuration::config();
  88. foreach ($config['extensions']['enabled'] as $extensionClass) {
  89. if (!class_exists($extensionClass)) {
  90. throw new ConfigurationException(
  91. "Class `$extensionClass` is not defined. Autoload it or include into "
  92. . "'_bootstrap.php' file of 'tests' directory"
  93. );
  94. }
  95. $extensionConfig = isset($config['extensions']['config'][$extensionClass])
  96. ? $config['extensions']['config'][$extensionClass]
  97. : [];
  98. $extension = new $extensionClass($extensionConfig, $options);
  99. if (!$extension instanceof EventSubscriberInterface) {
  100. throw new ConfigurationException(
  101. "Class $extensionClass is not an EventListener. Please create it as Extension or Group class."
  102. );
  103. }
  104. $this->extensions[] = $extension;
  105. }
  106. }
  107. protected function registerPHPUnitListeners()
  108. {
  109. $listener = new PHPUnit\Listener($this->dispatcher);
  110. $this->result->addListener($listener);
  111. }
  112. public function registerSubscribers()
  113. {
  114. // required
  115. $this->dispatcher->addSubscriber(new Subscriber\GracefulTermination());
  116. $this->dispatcher->addSubscriber(new Subscriber\ErrorHandler());
  117. $this->dispatcher->addSubscriber(new Subscriber\Dependencies());
  118. $this->dispatcher->addSubscriber(new Subscriber\Bootstrap());
  119. $this->dispatcher->addSubscriber(new Subscriber\Module());
  120. $this->dispatcher->addSubscriber(new Subscriber\BeforeAfterTest());
  121. // optional
  122. if (!$this->options['no-rebuild']) {
  123. $this->dispatcher->addSubscriber(new Subscriber\AutoRebuild());
  124. }
  125. if (!$this->options['silent']) {
  126. $this->dispatcher->addSubscriber(new Subscriber\Console($this->options));
  127. }
  128. if ($this->options['fail-fast']) {
  129. $this->dispatcher->addSubscriber(new Subscriber\FailFast());
  130. }
  131. if ($this->options['coverage']) {
  132. $this->dispatcher->addSubscriber(new Coverage\Subscriber\Local($this->options));
  133. $this->dispatcher->addSubscriber(new Coverage\Subscriber\LocalServer($this->options));
  134. $this->dispatcher->addSubscriber(new Coverage\Subscriber\RemoteServer($this->options));
  135. $this->dispatcher->addSubscriber(new Coverage\Subscriber\Printer($this->options));
  136. }
  137. // extensions
  138. foreach ($this->extensions as $subscriber) {
  139. $this->dispatcher->addSubscriber($subscriber);
  140. }
  141. }
  142. public function run($suite, $test = null)
  143. {
  144. ini_set(
  145. 'memory_limit',
  146. isset($this->config['settings']['memory_limit']) ? $this->config['settings']['memory_limit'] : '1024M'
  147. );
  148. $settings = Configuration::suiteSettings($suite, Configuration::config());
  149. $selectedEnvironments = $this->options['env'];
  150. $environments = Configuration::suiteEnvironments($suite);
  151. if (!$selectedEnvironments or empty($environments)) {
  152. $this->runSuite($settings, $suite, $test);
  153. return;
  154. }
  155. foreach (array_unique($selectedEnvironments) as $envList) {
  156. $envArray = explode(',', $envList);
  157. $config = [];
  158. foreach ($envArray as $env) {
  159. if (isset($environments[$env])) {
  160. $currentEnvironment = isset($config['current_environment']) ? [$config['current_environment']] : [];
  161. $config = Configuration::mergeConfigs($config, $environments[$env]);
  162. $currentEnvironment[] = $config['current_environment'];
  163. $config['current_environment'] = implode(',', $currentEnvironment);
  164. }
  165. }
  166. if (empty($config)) {
  167. continue;
  168. }
  169. $suiteToRun = $suite;
  170. if (!empty($envList)) {
  171. $suiteToRun .= ' (' . implode(', ', $envArray) . ')';
  172. }
  173. $this->runSuite($config, $suiteToRun, $test);
  174. }
  175. }
  176. public function runSuite($settings, $suite, $test = null)
  177. {
  178. $suiteManager = new SuiteManager($this->dispatcher, $suite, $settings);
  179. $suiteManager->initialize();
  180. $suiteManager->loadTests($test);
  181. $suiteManager->run($this->runner, $this->result, $this->options);
  182. return $this->result;
  183. }
  184. public static function versionString()
  185. {
  186. return 'Codeception PHP Testing Framework v' . self::VERSION;
  187. }
  188. public function printResult()
  189. {
  190. $result = $this->getResult();
  191. $result->flushListeners();
  192. $printer = $this->runner->getPrinter();
  193. $printer->printResult($result);
  194. $this->dispatcher->dispatch(Events::RESULT_PRINT_AFTER, new Event\PrintResultEvent($result, $printer));
  195. }
  196. /**
  197. * @return \PHPUnit_Framework_TestResult
  198. */
  199. public function getResult()
  200. {
  201. return $this->result;
  202. }
  203. public function getOptions()
  204. {
  205. return $this->options;
  206. }
  207. /**
  208. * @return EventDispatcher
  209. */
  210. public function getDispatcher()
  211. {
  212. return $this->dispatcher;
  213. }
  214. }