Skip to content

Commit 459eb1d

Browse files
committed
support named args
1 parent 9b69121 commit 459eb1d

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

src/Rules/PHPUnit/DataProviderDataRule.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,27 @@ private function arrayItemsToArgs(ConstantArrayType $array): ?array
142142
{
143143
$args = [];
144144

145-
foreach ($array->getValueTypes() as $valueType) {
146-
// XXX named args
147-
$arg = new Node\Arg(new TypeExpr($valueType));
145+
$keyTypes = $array->getKeyTypes();
146+
foreach ($array->getValueTypes() as $i => $valueType) {
147+
$key = $keyTypes[$i]->getConstantStrings();
148+
if (count($key) > 1) {
149+
return null;
150+
}
151+
152+
if (count($key) === 0) {
153+
$arg = new Node\Arg(new TypeExpr($valueType));
154+
$args[] = $arg;
155+
continue;
156+
157+
}
158+
159+
$arg = new Node\Arg(
160+
new TypeExpr($valueType),
161+
false,
162+
false,
163+
[],
164+
new Node\Identifier($key[0]->getValue()),
165+
);
148166
$args[] = $arg;
149167
}
150168

tests/Rules/PHPUnit/DataProviderDataRuleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,17 @@ public function testRule(): void
9999
172,
100100
],
101101
[
102-
'Parameter #2 $input of method DataProviderDataTest\UnionTypeReturnTest::testWithAnnotation() expects string, int given.',
102+
'Parameter #2 $input of method DataProviderDataTest\UnionTypeReturnTest::testFoo() expects string, int given.',
103103
216,
104104
],
105+
[
106+
'Parameter $input of method DataProviderDataTest\NamedArgsInProvider::testFoo() expects string, int given.',
107+
255,
108+
],
109+
[
110+
'Parameter $input of method DataProviderDataTest\NamedArgsInProvider::testFoo() expects string, false given.',
111+
255,
112+
],
105113
]);
106114
}
107115

tests/Rules/PHPUnit/data/data-provider-data.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class UnionTypeReturnTest extends TestCase
191191
{
192192

193193
/** @dataProvider aProvider */
194-
public function testWithAnnotation(string $expectedResult, string $input): void
194+
public function testFoo(string $expectedResult, string $input): void
195195
{
196196
}
197197

@@ -216,3 +216,42 @@ public function aProvider(): array
216216
return $arr;
217217
}
218218
}
219+
220+
221+
class NamedArgsInProvider extends TestCase
222+
{
223+
224+
/** @dataProvider aProvider */
225+
public function testFoo(string $expectedResult, string $input): void
226+
{
227+
}
228+
229+
public function aProvider(): array
230+
{
231+
$arr = [
232+
[
233+
"input" => 'Hello World',
234+
"expectedResult" => " Hello World \n"
235+
]
236+
];
237+
238+
if (rand(0,1)) {
239+
$arr = [
240+
[
241+
"input" => 123,
242+
"expectedResult" => " Hello World \n"
243+
]
244+
];
245+
}
246+
if (rand(0,1)) {
247+
$arr = [
248+
[
249+
"input" => false,
250+
"expectedResult" => " Hello World \n"
251+
]
252+
];
253+
}
254+
255+
return $arr;
256+
}
257+
}

0 commit comments

Comments
 (0)