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 = array( |
17
|
|
|
//run before any test` |
18
|
|
|
'suite.before' => 'before' |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
public function before(\Codeception\Event\SuiteEvent $e) |
22
|
|
|
{ |
23
|
|
|
$suite = $e->getSuite(); |
|
|
|
|
24
|
|
|
$tests = $suite->tests(); |
25
|
|
|
foreach ($tests as $id => $test) { |
26
|
|
|
if (get_class($test) == 'Codeception\Test\Cest') { |
27
|
|
|
$testClass = $test->getTestClass(); |
28
|
|
|
$testMethod = $test->getTestMethod(); |
29
|
|
|
$testFile = $test->getMetadata()->getFilename(); |
30
|
|
|
$testActor = $test->getMetadata()->getCurrent('actor'); |
31
|
|
|
$dataMethod = Annotation::forMethod($testClass, $testMethod)->fetch('dataprovider'); |
32
|
|
|
if (false === empty($dataMethod)) { |
33
|
|
|
if (false === is_callable([$testClass, $testMethod])) { |
34
|
|
|
throw new TestParseException( |
35
|
|
|
$testFile, "DataProvider for ${testClass}->${testMethod} is invalid or not callable" |
36
|
|
|
. PHP_EOL . |
37
|
|
|
"Make sure this is a public static function." |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
try { |
41
|
|
|
$dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider(); |
42
|
|
|
$examples = $testClass::$dataMethod(); |
43
|
|
|
foreach ($examples as $example) { |
44
|
|
|
if ($example === null) { |
45
|
|
|
throw new TestParseException( |
46
|
|
|
$testFile, "Values return by DataProvider for ${testClass}->${testMethod} is invalid" |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
$dataTest = new CestFormat($testClass, $testMethod, $testFile); |
50
|
|
|
$dataTest->getMetadata()->setServices([ |
51
|
|
|
'di' => $test->getMetadata()->getService('di'), |
52
|
|
|
'dispatcher' => $test->getMetadata()->getService('dispatcher'), |
53
|
|
|
'modules' => $test->getMetadata()->getService('modules') |
54
|
|
|
]); |
55
|
|
|
$dataTest->getMetadata()->setCurrent(['actor' => $testActor, 'example' => $example]); |
56
|
|
|
$step = new Comment('', $dataTest->getMetadata()->getCurrent('example')); |
57
|
|
|
$dataTest->getScenario()->setFeature($dataTest->getSpecFromMethod() . ' | '. $step->getArgumentsAsString(100)); |
58
|
|
|
$groups = Annotation::forMethod($testClass, $testMethod)->fetchAll('group'); |
59
|
|
|
$dataProvider->addTest($dataTest, $groups); |
60
|
|
|
} |
61
|
|
|
$tests[$id] = $dataProvider; |
62
|
|
|
} catch(\Exception $e) { |
63
|
|
|
throw new TestParseException( |
64
|
|
|
$testFile, "DataProvider for ${testClass}->${testMethod} is invalid or not callable" |
65
|
|
|
. PHP_EOL . |
66
|
|
|
"Make sure this is a public static function." |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
$suite->setTests($tests); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: