1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Codeception\Test\Loader;
use Codeception\Lib\Parser;
use Codeception\Test\Descriptor;
use Codeception\Test\Unit as UnitFormat;
use Codeception\Util\Annotation;
class Unit implements LoaderInterface
{
protected $tests = [];
public function getPattern()
{
return '~Test\.php$~';
}
public function loadTests($path)
{
Parser::load($path);
$testClasses = Parser::getClassesFromFile($path);
foreach ($testClasses as $testClass) {
$reflected = new \ReflectionClass($testClass);
if (!$reflected->isInstantiable()) {
continue;
}
foreach ($reflected->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$test = $this->createTestFromPhpUnitMethod($reflected, $method);
if (!$test) {
continue;
}
$this->tests[] = $test;
}
}
}
public function getTests()
{
return $this->tests;
}
protected function createTestFromPhpUnitMethod(\ReflectionClass $class, \ReflectionMethod $method)
{
if (!\PHPUnit_Framework_TestSuite::isTestMethod($method)) {
return;
}
$test = \PHPUnit_Framework_TestSuite::createTest($class, $method->name);
if ($test instanceof \PHPUnit_Framework_TestSuite_DataProvider) {
foreach ($test->tests() as $t) {
$this->enhancePhpunitTest($t);
}
return $test;
}
$this->enhancePhpunitTest($test);
return $test;
}
protected function enhancePhpunitTest(\PHPUnit_Framework_TestCase $test)
{
$className = get_class($test);
$methodName = $test->getName(false);
$dependencies = \PHPUnit_Util_Test::getDependencies($className, $methodName);
$test->setDependencies($dependencies);
if ($test instanceof UnitFormat) {
$test->getMetadata()->setFilename(Descriptor::getTestFileName($test));
$test->getMetadata()->setDependencies($dependencies);
$test->getMetadata()->setEnv(Annotation::forMethod($test, $methodName)->fetchAll('env'));
}
}
}