Skip to content
This repository has been archived by the owner on Mar 31, 2018. It is now read-only.

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Heitz committed Aug 1, 2016
1 parent 00d6222 commit 0114c6e
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions src/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,48 @@ public function before(\Codeception\Event\SuiteEvent $se)
$tests = $suite->tests();
foreach ($tests as $id => $test) {
if (is_a($test, 'Codeception\Test\Cest')) {

$testClass = $test->getTestClass();
$testClassName = get_class($testClass);
$testMethod = $test->getTestMethod();
$testFile = $test->getMetadata()->getFilename();
$testActor = $test->getMetadata()->getCurrent('actor');

$dataMethod = Annotation::forMethod($testClass, $testMethod)->fetch('dataprovider');
if (false === empty($dataMethod)) {
try {
if (false === is_callable([$testClass, $dataMethod])) {
throw new \Exception();
}
$dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider();
$examples = $testClassName::$dataMethod();
foreach ($examples as $example) {
if ($example === null) {
throw new TestParseException(
$testFile, "Invalid values format returned by DataProvider {$dataMethod} for ${$testClassName}->${testMethod}."
);
}
$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);

try {
if (false === is_callable([$testClass, $dataMethod])) {
throw new Exception();
}

$dataProvider = new \PHPUnit_Framework_TestSuite_DataProvider();
$examples = $testClassName::$dataMethod();
foreach ($examples as $example) {
if ($example === null) {
throw new TestParseException(
$testFile, "Invalid values format returned by DataProvider {$dataMethod} for ${testClassName}->${testMethod}."
);
}
$tests[$id] = $dataProvider;
} catch (\Exception $e) {
throw new TestParseException(
$testFile, "DataProvider {$dataMethod} for ${$testClassName}->${testMethod} is invalid or not callable."
. PHP_EOL .
"Make sure this is a public static function."
);
$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 {$dataMethod} for ${testClassName}->${testMethod} is invalid or not callable."
. PHP_EOL .
"Make sure this is a public static function."
);
}
}
}
Expand Down

0 comments on commit 0114c6e

Please sign in to comment.