This repository has been archived by the owner on Mar 31, 2018. It is now read-only.
/
DataProvider.php
75 lines (71 loc) · 3.33 KB
/
DataProvider.php
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
75
<?php
/**
* Before suite hook that provide dataprovider annotation
* for datadriven tests using non-static data source
*/
namespace Codeception\Extension;
use Codeception\Util\Annotation;
use Codeception\Step\Comment;
use Codeception\Test\Cest as CestFormat;
use Codeception\Exception\TestParseException;
class DataProvider extends \Codeception\Platform\Extension
{
// list events to listen to
public static $events = array(
//run before any test`
'suite.before' => 'before'
);
public function before(\Codeception\Event\SuiteEvent $se)
{
$suite = $se->getSuite();
$tests = $suite->tests();
foreach ($tests as $id => $test) {
if (get_class($test) == 'Codeception\Test\Cest') {
$testClass = $test->getTestClass();
$testMethod = $test->getTestMethod();
$testFile = $test->getMetadata()->getFilename();
$testActor = $test->getMetadata()->getCurrent('actor');
$dataMethod = Annotation::forMethod($testClass, $testMethod)->fetch('dataprovider');
if (false === empty($dataMethod)) {
if (false === is_callable([$testClass, $testMethod])) {
throw new TestParseException(
$testFile, "DataProvider for ${testClass}->${testMethod} is invalid or not callable"
. PHP_EOL .
"Make sure this is a public static function."
);
}
try {
$dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider();
$examples = $testClass::$dataMethod();
foreach ($examples as $example) {
if ($example === null) {
throw new TestParseException(
$testFile, "Values return by DataProvider for ${testClass}->${testMethod} is invalid"
);
}
$dataTest = new CestFormat($testClass, $testMethod, $testFile);
$dataTest->getMetadata()->setServices([
'di' => $test->getMetadata()->getService('di'),
'dispatcher' => $test->getMetadata()->getService('dispatcher'),
'modules' => $test->getMetadata()->getService('modules')
]);
$dataTest->getMetadata()->setCurrent(['actor' => $testActor, 'example' => $example]);
$step = new Comment('', $dataTest->getMetadata()->getCurrent('example'));
$dataTest->getScenario()->setFeature($dataTest->getSpecFromMethod() . ' | '. $step->getArgumentsAsString(100));
$groups = Annotation::forMethod($testClass, $testMethod)->fetchAll('group');
$dataProvider->addTest($dataTest, $groups);
}
$tests[$id] = $dataProvider;
} catch(\Exception $e) {
throw new TestParseException(
$testFile, "DataProvider for ${testClass}->${testMethod} is invalid or not callable"
. PHP_EOL .
"Make sure this is a public static function."
);
}
}
}
}
$suite->setTests($tests);
}
}