-
-
Notifications
You must be signed in to change notification settings - Fork 73
TestCase: possibility of named sets in dataprovider #293
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -126,7 +131,7 @@ public function runTest($method, array $args = NULL) | |
| } | ||
|
|
||
|
|
||
| foreach ($data as $params) { | ||
| foreach ($data as $setName => $params) { | ||
| try { | ||
| $this->setUp(); | ||
|
|
||
|
|
@@ -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)) . ')'); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thining about better message composition.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean wording, format or code? |
||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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__; | ||
|
|
@@ -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) | ||
| { | ||
|
|
@@ -59,6 +73,12 @@ class MyTest extends Tester\TestCase | |
| { | ||
| Assert::true(FALSE); | ||
| } | ||
|
|
||
| /** @dataProvider dataProviderNamedSets */ | ||
| public function testAssertionNamedSets() | ||
| { | ||
| Assert::true(FALSE); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| } | ||
|
|
||
|
|
||
|
|
@@ -83,6 +103,14 @@ Assert::same([ | |
| ], $test->order); | ||
|
|
||
|
|
||
| $test = new MyTest; | ||
| $test->runTest('testDataProviderNamedSets'); | ||
| Assert::same([ | ||
| 'MyTest::dataProviderNamedSets', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use short arrays notation now :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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([ | ||
|
|
@@ -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)'); | ||
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 ;-)There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.