Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataProvider can use default values of target method #123

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Tester/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private function runMethod($method)
if (!is_array($res)) {
throw new TestCaseException("Data provider $provider() doesn't return array.");
}
$data = array_merge($data, $res);
$data = array_merge($data, $this->fillDefaultParameters($res, $method));
}
if (!$info['dataprovider']) {
if ($method->getNumberOfRequiredParameters()) {
Expand Down Expand Up @@ -146,6 +146,30 @@ protected function getData($provider)
}


/**
* Fills unknown parameters by default ones.
* @return array
*/
protected function fillDefaultParameters($parameters, \ReflectionMethod $method)
{
if (!count(array_filter(array_keys($parameters), 'is_string'))) {
// not associative array of parameters
return $parameters;
}

$defaultParametrs = array();
foreach ($method->getParameters() as $parameter) {
$defaultParametrs[$parameter->getName()] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : NULL;
}

array_walk($parameters, function(&$item) use ($defaultParametrs) {
$item = array_merge($defaultParametrs, $item);
});

return $parameters;
}


/**
* This method is called before a test is executed.
* @return void
Expand Down
18 changes: 16 additions & 2 deletions tests/TestCase.dataProvider.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ class MyTest extends Tester\TestCase
}

/** @dataProvider fixtures/dataprovider.query.ini != foo */
public function testFileDataProvider($data)
public function testFileDataProvider($a = '1')
{
$this->order[] = array(__METHOD__, func_get_args());
}

/** @dataProvider fixtures/dataprovider.query.ini != foo */
public function testFileDataProviderDefaultValues($a = '1', $b = '3')
{
$this->order[] = array(__METHOD__, func_get_args());
}
Expand Down Expand Up @@ -72,7 +78,15 @@ $test = new MyTest;
$test->run('testFileDataProvider');
Assert::same(array(
array('MyTest::testFileDataProvider', array('1')),
array('MyTest::testFileDataProvider', array('2')),
array('MyTest::testFileDataProvider', array('1', '2')),
), $test->order);


$test = new MyTest;
$test->run('testFileDataProviderDefaultValues');
Assert::same(array(
array('MyTest::testFileDataProviderDefaultValues', array('1', '3')),
array('MyTest::testFileDataProviderDefaultValues', array('1', '2')),
), $test->order);


Expand Down