PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Codeception/SuiteManager.php

https://github.com/pmcjury/Codeception
PHP | 180 lines | 153 code | 15 blank | 12 comment | 2 complexity | 3ed9cbe075fdba7297ce0c382e7fd734 MD5 | raw file
  1. <?php
  2. namespace Codeception;
  3. use Codeception\Event\Suite;
  4. use Codeception\Event\SuiteEvent;
  5. use Codeception\Lib\GroupManager;
  6. use Symfony\Component\EventDispatcher\EventDispatcher;
  7. class SuiteManager
  8. {
  9. public static $modules = array();
  10. public static $actions = array();
  11. public static $environment;
  12. public static $name;
  13. /**
  14. * @var \PHPUnit_Framework_TestSuite
  15. */
  16. protected $suite = null;
  17. /**
  18. * @var null|\Symfony\Component\EventDispatcher\EventDispatcher
  19. */
  20. protected $dispatcher = null;
  21. /**
  22. * @var GroupManager
  23. */
  24. protected $groupManager;
  25. /**
  26. * @var TestLoader
  27. */
  28. protected $testLoader;
  29. protected $tests = array();
  30. protected $debug = false;
  31. protected $path = '';
  32. protected $printer = null;
  33. protected $env = null;
  34. public function __construct(EventDispatcher $dispatcher, $name, array $settings)
  35. {
  36. $this->settings = $settings;
  37. $this->dispatcher = $dispatcher;
  38. $this->suite = $this->createSuite($name);
  39. $this->path = $settings['path'];
  40. $this->groupManager = new GroupManager($settings['groups']);
  41. if (isset($settings['current_environment'])) {
  42. $this->env = $settings['current_environment'];
  43. }
  44. $this->suite = $this->createSuite($name);
  45. }
  46. public function initialize()
  47. {
  48. $this->initializeModules();
  49. $this->dispatcher->dispatch(Events::SUITE_INIT, new SuiteEvent($this->suite, null, $this->settings));
  50. $this->initializeActors();
  51. ini_set('xdebug.show_exception_trace', 0); // Issue https://github.com/symfony/symfony/issues/7646
  52. }
  53. protected function initializeModules()
  54. {
  55. self::$modules = Configuration::modules($this->settings);
  56. self::$actions = Configuration::actions(self::$modules);
  57. foreach (self::$modules as $module) {
  58. $module->_initialize();
  59. }
  60. }
  61. protected function initializeActors()
  62. {
  63. if (!file_exists($this->path . $this->settings['class_name'] . '.php')) {
  64. throw new Exception\Configuration($this->settings['class_name'] . " class doesn't exists in suite folder.\nRun the 'build' command to generate it");
  65. }
  66. require_once $this->settings['path'] . DIRECTORY_SEPARATOR . $this->settings['class_name'] . '.php';
  67. }
  68. public static function hasModule($moduleName)
  69. {
  70. return isset(self::$modules[$moduleName]);
  71. }
  72. public function loadTests($path = null)
  73. {
  74. $testLoader = new TestLoader($this->settings['path']);
  75. $path
  76. ? $testLoader->loadTest($path)
  77. : $testLoader->loadTests();
  78. $tests = $testLoader->getTests();
  79. foreach ($tests as $test) {
  80. $this->addToSuite($test);
  81. }
  82. }
  83. protected function addToSuite($test)
  84. {
  85. if ($test instanceof TestCase\Interfaces\Configurable) {
  86. $test->configDispatcher($this->dispatcher);
  87. $test->configActor($this->getActor());
  88. $test->configEnv($this->env);
  89. }
  90. if ($test instanceof \PHPUnit_Framework_TestSuite_DataProvider) {
  91. foreach ($test->tests() as $t) {
  92. if (!$t instanceof TestCase\Interfaces\Configurable) {
  93. continue;
  94. }
  95. $t->configDispatcher($this->dispatcher);
  96. $t->configActor($this->getActor());
  97. $t->configEnv($this->env);
  98. }
  99. }
  100. if ($test instanceof TestCase\Interfaces\ScenarioDriven) {
  101. if (!$this->isCurrentEnvironment($test->getScenario()->getEnv())) {
  102. return;
  103. }
  104. $test->preload();
  105. }
  106. $groups = $this->groupManager->groupsForTest($test);
  107. $this->suite->addTest($test, $groups);
  108. }
  109. protected function createSuite($name)
  110. {
  111. $suite = new \PHPUnit_Framework_TestSuite();
  112. $suite->baseName = $this->env
  113. ? substr($name, 0, strpos($name, '-' . $this->env))
  114. : $name;
  115. if ($this->settings['namespace']) {
  116. $name = $this->settings['namespace'] . ".$name";
  117. }
  118. $suite->setName($name);
  119. if (!($suite instanceof \PHPUnit_Framework_TestSuite)) {
  120. throw new Exception\Configuration("Suite class is not inherited from PHPUnit_Framework_TestSuite");
  121. }
  122. return $suite;
  123. }
  124. public function run(PHPUnit\Runner $runner, \PHPUnit_Framework_TestResult $result, $options)
  125. {
  126. $this->dispatcher->dispatch(Events::SUITE_BEFORE, new Event\SuiteEvent($this->suite, $result, $this->settings));
  127. $runner->doEnhancedRun($this->suite, $result, $options);
  128. $this->dispatcher->dispatch(Events::SUITE_AFTER, new Event\SuiteEvent($this->suite, $result, $this->settings));
  129. }
  130. /**
  131. * @return null|\PHPUnit_Framework_TestSuite
  132. */
  133. public function getSuite()
  134. {
  135. return $this->suite;
  136. }
  137. protected function isCurrentEnvironment($envs)
  138. {
  139. if (empty($envs)) {
  140. return true;
  141. }
  142. return $this->env and in_array($this->env, $envs);
  143. }
  144. protected function getActor()
  145. {
  146. return $this->settings['namespace']
  147. ? $this->settings['namespace'] . '\\' . $this->settings['class_name']
  148. : $this->settings['class_name'];
  149. }
  150. }