Skip to content
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
13 changes: 9 additions & 4 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ public function runTest($method, array $args = NULL)
if (!is_array($res) && !$res instanceof \Traversable) {
throw new TestCaseException("Data provider $provider() doesn't return array or Traversable.");
}
foreach ($res as $set) {
$data[] = is_string(key($set)) ? array_merge($defaultParams, $set) : $set;
foreach ($res as $setName => $set) {
$set = is_string(key($set)) ? array_merge($defaultParams, $set) : $set;
if (is_string($setName)) {
$data[$setName] = $set;
} else {
$data[] = $set;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stekycz All original keys can be preserved in $data[], not only strings.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@milo Unfortunately I can't because it breaks possibility of multiple data providers. The string key must be unique because it is explicit value however two different data providers without naming returns array with indexes 0, 1, 2, ... so it results in wrong data set. You can try it on your own ;-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the same string may replace previous dataset too.

Copy link
Copy Markdown
Contributor Author

@stekycz stekycz Apr 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it can. However the probability of that is lower then for regular array. The same string name must be specified explicitly however same array index is quite hard to find where the problem (overriding) is.

}
}

Expand All @@ -126,7 +131,7 @@ public function runTest($method, array $args = NULL)
}


foreach ($data as $params) {
foreach ($data as $setName => $params) {
try {
$this->setUp();

Expand All @@ -152,7 +157,7 @@ public function runTest($method, array $args = NULL)
$this->tearDown();

} catch (AssertException $e) {
throw $e->setMessage("$e->origMessage in {$method->getName()}(" . (substr(Dumper::toLine($params), 1, -1)) . ')');
throw $e->setMessage("$e->origMessage in " . (is_string($setName) ? "\"" . $setName . "\": " : "") . "{$method->getName()}(" . (substr(Dumper::toLine($params), 1, -1)) . ')');
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thining about better message composition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean wording, format or code?

}
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Framework/TestCase.dataProvider.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ class MyTest extends Tester\TestCase
];
}

public function dataProviderNamedSets()
{
$this->order[] = __METHOD__;
return [
'Test 1 and 2' => [1, 2],
];
}

public function dataProviderIterator()
{
$this->order[] = __METHOD__;
Expand All @@ -42,6 +50,12 @@ class MyTest extends Tester\TestCase
$this->order[] = [__METHOD__, func_get_args()];
}

/** @dataProvider dataProviderNamedSets */
public function testDataProviderNamedSets($a, $b)
{
$this->order[] = [__METHOD__, func_get_args()];
}

/** @dataProvider dataProviderIterator */
public function testIteratorDataProvider($a, $b)
{
Expand All @@ -59,6 +73,12 @@ class MyTest extends Tester\TestCase
{
Assert::true(FALSE);
}

/** @dataProvider dataProviderNamedSets */
public function testAssertionNamedSets()
{
Assert::true(FALSE);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FALSE

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

}


Expand All @@ -83,6 +103,14 @@ Assert::same([
], $test->order);


$test = new MyTest;
$test->runTest('testDataProviderNamedSets');
Assert::same([
'MyTest::dataProviderNamedSets',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use short arrays notation now :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

['MyTest::testDataProviderNamedSets', [1, 2]],
], $test->order);


$test = new MyTest;
$test->runTest('testIteratorDataProvider');
Assert::same([
Expand All @@ -104,3 +132,9 @@ Assert::exception(function () {
$test = new MyTest;
$test->runTest('testAssertion');
}, 'Tester\AssertException', 'FALSE should be TRUE in testAssertion(1, 2)');


Assert::exception(function () {
$test = new MyTest;
$test->runTest('testAssertionNamedSets');
}, 'Tester\AssertException', 'FALSE should be TRUE in "Test 1 and 2": testAssertionNamedSets(1, 2)');