|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Before suite hook that provide dataprovider annotation |
|
4
|
|
|
* for datadriven tests using non-static data source |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Codeception\Extension; |
|
7
|
|
|
|
|
8
|
|
|
use Codeception\Util\Annotation; |
|
9
|
|
|
use Codeception\Step\Comment; |
|
10
|
|
|
use Codeception\Test\Cest as CestFormat; |
|
11
|
|
|
use Codeception\Exception\TestParseException; |
|
12
|
|
|
|
|
13
|
|
|
class DataProvider extends \Codeception\Platform\Extension |
|
14
|
|
|
{ |
|
15
|
|
|
// list events to listen to |
|
16
|
|
|
public static $events = ['suite.before' => 'before']; |
|
17
|
|
|
|
|
18
|
|
|
public function before(\Codeception\Event\SuiteEvent $se) |
|
19
|
|
|
{ |
|
20
|
|
|
$suite = $se->getSuite(); |
|
21
|
|
|
$tests = $suite->tests(); |
|
22
|
|
|
foreach ($tests as $id => $test) { |
|
23
|
|
|
if (is_a($test, 'Codeception\Test\Cest')) { |
|
24
|
|
|
$testClass = $test->getTestClass(); |
|
25
|
|
|
$testClassName = get_class($testClass); |
|
26
|
|
|
$testMethod = $test->getTestMethod(); |
|
27
|
|
|
$testFile = $test->getMetadata()->getFilename(); |
|
28
|
|
|
$testActor = $test->getMetadata()->getCurrent('actor'); |
|
29
|
|
|
$dataMethod = Annotation::forMethod($testClass, $testMethod)->fetch('dataprovider'); |
|
30
|
|
|
if (false === empty($dataMethod)) { |
|
31
|
|
|
try { |
|
32
|
|
|
if (false === is_callable([$testClass, $dataMethod])) { |
|
33
|
|
|
throw new Exception(); |
|
34
|
|
|
} |
|
35
|
|
|
$dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider(); |
|
36
|
|
|
$examples = $testClassName::$dataMethod(); |
|
37
|
|
|
foreach ($examples as $example) { |
|
38
|
|
|
if ($example === null) { |
|
39
|
|
|
throw new TestParseException( |
|
40
|
|
|
$testFile, "Invalid values format returned by DataProvider {$dataMethod} for ${$testClassName}->${testMethod}." |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
$dataTest = new CestFormat($testClass, $testMethod, $testFile); |
|
44
|
|
|
$dataTest->getMetadata()->setServices([ |
|
45
|
|
|
'di' => $test->getMetadata()->getService('di'), |
|
46
|
|
|
'dispatcher' => $test->getMetadata()->getService('dispatcher'), |
|
47
|
|
|
'modules' => $test->getMetadata()->getService('modules') |
|
48
|
|
|
]); |
|
49
|
|
|
$dataTest->getMetadata()->setCurrent(['actor' => $testActor, 'example' => $example]); |
|
50
|
|
|
$step = new Comment('', $dataTest->getMetadata()->getCurrent('example')); |
|
51
|
|
|
$dataTest->getScenario()->setFeature($dataTest->getSpecFromMethod() . ' | ' . $step->getArgumentsAsString(100)); |
|
52
|
|
|
$groups = Annotation::forMethod($testClass, $testMethod)->fetchAll('group'); |
|
53
|
|
|
$dataProvider->addTest($dataTest, $groups); |
|
54
|
|
|
} |
|
55
|
|
|
$tests[$id] = $dataProvider; |
|
56
|
|
|
} catch (\Exception $e) { |
|
57
|
|
|
throw new TestParseException( |
|
58
|
|
|
$testFile, "DataProvider {$dataMethod} for ${$testClassName}->${testMethod} is invalid or not callable." |
|
59
|
|
|
. PHP_EOL . |
|
60
|
|
|
"Make sure this is a public static function." |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
$suite->setTests($tests); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|