PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/codeception/codeception/src/Codeception/Lib/TestLoader.php

https://gitlab.com/merial/WETE_Ryhma3
PHP | 231 lines | 195 code | 5 blank | 31 comment | 0 complexity | 7ca7dfc59fedaae47178ccdfef08604a MD5 | raw file
  1. <?php
  2. namespace Codeception\Lib;
  3. use Codeception\Exception\TestParseException;
  4. use Codeception\TestCase\Cept;
  5. use Codeception\TestCase\Cest;
  6. use Symfony\Component\Finder\Finder;
  7. /**
  8. * Loads all Codeception supported test formats from a directory.
  9. *
  10. * ``` php
  11. * <?php
  12. * $testLoader = new \Codeception\TestLoader('tests/unit');
  13. * $testLoader->loadTests();
  14. * $tests = $testLoader->getTests();
  15. * ?>
  16. * ```
  17. * You can load specific file
  18. *
  19. * ``` php
  20. * <?php
  21. * $testLoader = new \Codeception\TestLoader('tests/unit');
  22. * $testLoader->loadTest('UserTest.php');
  23. * $testLoader->loadTest('PostTest.php');
  24. * $tests = $testLoader->getTests();
  25. * ?>
  26. * ```
  27. * or a subdirectory
  28. *
  29. * ``` php
  30. * <?php
  31. * $testLoader = new \Codeception\TestLoader('tests/unit');
  32. * $testLoader->loadTest('models'); // all tests from tests/unit/models
  33. * $tests = $testLoader->getTests();
  34. * ?>
  35. * ```
  36. *
  37. */
  38. class TestLoader
  39. {
  40. protected static $formats = ['Cest', 'Cept', 'Test'];
  41. protected $tests = [];
  42. protected $path;
  43. public function __construct($path)
  44. {
  45. $this->path = $path;
  46. }
  47. public function getTests()
  48. {
  49. return $this->tests;
  50. }
  51. protected function relativeName($file)
  52. {
  53. return str_replace([$this->path, '\\'], ['', '/'], $file);
  54. }
  55. protected function findPath($path)
  56. {
  57. if (!file_exists($path)
  58. && substr(strtolower($path), -strlen('.php')) !== '.php'
  59. && file_exists($newPath = $path . '.php')
  60. ) {
  61. return $newPath;
  62. }
  63. return $path;
  64. }
  65. protected function makePath($originalPath)
  66. {
  67. $path = $this->path . $this->relativeName($originalPath);
  68. if (file_exists($newPath = $this->findPath($path))
  69. || file_exists($newPath = $this->findPath(getcwd() . "/{$originalPath}"))
  70. ) {
  71. $path = $newPath;
  72. }
  73. if (!file_exists($path)) {
  74. throw new \Exception("File or path $originalPath not found");
  75. }
  76. return $path;
  77. }
  78. public function loadTest($path)
  79. {
  80. $path = $this->makePath($path);
  81. foreach (self::$formats as $format) {
  82. if (preg_match("~$format.php$~", $path)) {
  83. call_user_func([$this, "add$format"], $path);
  84. return;
  85. }
  86. }
  87. if (is_dir($path)) {
  88. $currentPath = $this->path;
  89. $this->path = $path;
  90. $this->loadTests();
  91. $this->path = $currentPath;
  92. return;
  93. }
  94. throw new \Exception('Test format not supported. Please, check you use the right suffix. Available filetypes: Cept, Cest, Test');
  95. }
  96. public function loadTests()
  97. {
  98. $finder = Finder::create()->files()->sortByName()->in($this->path)->followLinks();
  99. foreach (self::$formats as $format) {
  100. $formatFinder = clone($finder);
  101. $testFiles = $formatFinder->name("*$format.php");
  102. foreach ($testFiles as $test) {
  103. $pathname = str_replace("//", "/", $test->getPathname());
  104. call_user_func([$this, "add$format"], $pathname);
  105. }
  106. }
  107. }
  108. public function addTest($path)
  109. {
  110. Parser::load($path);
  111. $testClasses = Parser::getClassesFromFile($path);
  112. foreach ($testClasses as $testClass) {
  113. $reflected = new \ReflectionClass($testClass);
  114. if (!$reflected->isInstantiable()) {
  115. continue;
  116. }
  117. foreach ($reflected->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
  118. $test = $this->createTestFromPhpUnitMethod($reflected, $method);
  119. if (!$test) {
  120. continue;
  121. }
  122. $this->tests[] = $test;
  123. }
  124. }
  125. }
  126. public function addCept($file)
  127. {
  128. Parser::validate($file);
  129. $name = $this->relativeName($file);
  130. $cept = new Cept();
  131. $cept->configName($name)
  132. ->configFile($file);
  133. $this->tests[] = $cept;
  134. }
  135. public function addCest($file)
  136. {
  137. Parser::load($file);
  138. $testClasses = Parser::getClassesFromFile($file);
  139. foreach ($testClasses as $testClass) {
  140. if (substr($testClass, -strlen('Cest')) !== 'Cest') {
  141. continue;
  142. }
  143. if (!(new \ReflectionClass($testClass))->isInstantiable()) {
  144. continue;
  145. }
  146. $unit = new $testClass;
  147. $methods = get_class_methods($testClass);
  148. foreach ($methods as $method) {
  149. $test = $this->createTestFromCestMethod($unit, $method, $file);
  150. if (!$test) {
  151. continue;
  152. }
  153. $this->tests[] = $test;
  154. }
  155. }
  156. }
  157. protected function createTestFromPhpUnitMethod(\ReflectionClass $class, \ReflectionMethod $method)
  158. {
  159. if (!\PHPUnit_Framework_TestSuite::isTestMethod($method)) {
  160. return;
  161. }
  162. $test = \PHPUnit_Framework_TestSuite::createTest($class, $method->name);
  163. if ($test instanceof \PHPUnit_Framework_TestSuite_DataProvider) {
  164. foreach ($test->tests() as $t) {
  165. $this->enhancePhpunitTest($t);
  166. }
  167. return $test;
  168. }
  169. $this->enhancePhpunitTest($test);
  170. return $test;
  171. }
  172. protected function enhancePhpunitTest(\PHPUnit_Framework_TestCase $test)
  173. {
  174. $className = get_class($test);
  175. $methodName = $test->getName(false);
  176. $test->setDependencies(\PHPUnit_Util_Test::getDependencies($className, $methodName));
  177. if (!$test instanceof \Codeception\TestCase) {
  178. return;
  179. }
  180. }
  181. protected function createTestFromCestMethod($cestInstance, $methodName, $file)
  182. {
  183. if ((strpos($methodName, '_') === 0) || ($methodName == '__construct')) {
  184. return null;
  185. }
  186. $testClass = get_class($cestInstance);
  187. $cest = new Cest();
  188. $cest->configName($methodName)
  189. ->configFile($file)
  190. ->config('testClassInstance', $cestInstance)
  191. ->config('testMethod', $methodName);
  192. $cest->setDependencies(\PHPUnit_Util_Test::getDependencies($testClass, $methodName));
  193. return $cest;
  194. }
  195. }