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
|
|
|
|
25
|
|
|
$testClass = $test->getTestClass(); |
26
|
|
|
$testClassName = get_class($testClass); |
27
|
|
|
$testMethod = $test->getTestMethod(); |
28
|
|
|
$testFile = $test->getMetadata()->getFilename(); |
29
|
|
|
$testActor = $test->getMetadata()->getCurrent('actor'); |
30
|
|
|
|
31
|
|
|
$dataMethod = Annotation::forMethod($testClass, $testMethod)->fetch('dataprovider'); |
32
|
|
|
|
33
|
|
|
try { |
34
|
|
|
if (false === is_callable([$testClass, $dataMethod])) { |
35
|
|
|
throw new Exception(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider(); |
39
|
|
|
$examples = $testClassName::$dataMethod(); |
40
|
|
|
foreach ($examples as $example) { |
41
|
|
|
if ($example === null) { |
42
|
|
|
throw new TestParseException( |
43
|
|
|
$testFile, "Invalid values format returned by DataProvider {$dataMethod} for ${testClassName}->${testMethod}." |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
$dataTest = new CestFormat($testClass, $testMethod, $testFile); |
47
|
|
|
$dataTest->getMetadata()->setServices([ |
48
|
|
|
'di' => $test->getMetadata()->getService('di'), |
49
|
|
|
'dispatcher' => $test->getMetadata()->getService('dispatcher'), |
50
|
|
|
'modules' => $test->getMetadata()->getService('modules') |
51
|
|
|
]); |
52
|
|
|
$dataTest->getMetadata()->setCurrent(['actor' => $testActor, 'example' => $example]); |
53
|
|
|
$step = new Comment('', $dataTest->getMetadata()->getCurrent('example')); |
54
|
|
|
$dataTest->getScenario()->setFeature($dataTest->getSpecFromMethod() . ' | ' . $step->getArgumentsAsString(100)); |
55
|
|
|
$groups = Annotation::forMethod($testClass, $testMethod)->fetchAll('group'); |
56
|
|
|
$dataProvider->addTest($dataTest, $groups); |
57
|
|
|
} |
58
|
|
|
$tests[$id] = $dataProvider; |
59
|
|
|
|
60
|
|
|
} catch (\Exception $e) { |
61
|
|
|
throw new TestParseException( |
62
|
|
|
$testFile, "DataProvider {$dataMethod} for ${testClassName}->${testMethod} is invalid or not callable." |
63
|
|
|
. PHP_EOL . |
64
|
|
|
"Make sure this is a public static function." |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
$suite->setTests($tests); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|