Skip to content

Commit 143f9d5

Browse files
authored
Merge pull request #151 from Ocramius/feature/support-multiple-data-providers
Ensure that all `@dataProvider` and `#[DataProvider]` (multiple) are considered
2 parents 8474743 + cc6b85b commit 143f9d5

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

src/Hooks/TestCaseHandler.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,19 @@ private static function attributeValue(ClassMethod $method, Aliases $aliases, st
566566
$aliases,
567567
),
568568
);
569+
$matchingAttributes = array_merge(...array_values(array_map(
570+
$attributesInGroupMatchingRequestedAttributeName,
571+
$method->getAttrGroups(),
572+
)));
569573

570-
foreach ($method->getAttrGroups() as $group) {
571-
foreach ($attributesInGroupMatchingRequestedAttributeName($group) as $attribute) {
572-
return $onlyStringLiteralExpressions($attribute);
573-
}
574+
if ($matchingAttributes === []) {
575+
return null;
574576
}
575577

576-
return null;
578+
return array_merge(...array_values(array_map(
579+
$onlyStringLiteralExpressions,
580+
$matchingAttributes,
581+
)));
577582
}
578583

579584
/** @return array<string, array<int,string>> */

tests/acceptance/TestCase.feature

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,62 @@ Feature: TestCase
231231
When I run Psalm
232232
Then I see no errors
233233

234+
Scenario: Multiple valid iterable @dataProvider are allowed, and all are used
235+
Given I have the following code
236+
"""
237+
use PHPUnit\Framework\Attributes;
238+
239+
final class MyTestCase extends TestCase
240+
{
241+
/** @return iterable<int,array<int,int>> */
242+
public function provider1() {
243+
yield [1];
244+
}
245+
246+
/** @return iterable<int,array<int,int>> */
247+
public function provider2() {
248+
yield [1];
249+
}
250+
251+
/**
252+
* @dataProvider provider1
253+
* @dataProvider provider2
254+
*/
255+
public function testSomething(int $int): void {
256+
$this->assertEquals(1, $int);
257+
}
258+
}
259+
"""
260+
When I run Psalm with dead code detection
261+
Then I see no errors
262+
263+
Scenario: Multiple valid iterable #[DataProvider]s are allowed, and all are used
264+
Given I have the following code
265+
"""
266+
use PHPUnit\Framework\Attributes;
267+
268+
final class MyTestCase extends TestCase
269+
{
270+
/** @return iterable<int,array<int,int>> */
271+
public function provider1() {
272+
yield [1];
273+
}
274+
275+
/** @return iterable<int,array<int,int>> */
276+
public function provider2() {
277+
yield [1];
278+
}
279+
280+
#[Attributes\DataProvider('provider1')]
281+
#[Attributes\DataProvider('provider2')]
282+
public function testSomething(int $int): void {
283+
$this->assertEquals(1, $int);
284+
}
285+
}
286+
"""
287+
When I run Psalm with dead code detection
288+
Then I see no errors
289+
234290
Scenario: Invalid generator data provider is reported
235291
Given I have the following code
236292
"""

0 commit comments

Comments
 (0)