Skip to content

Commit a36e967

Browse files
committed
GetMethodParametersTest: remove even more duplicate code
Implement use of the new AbstractMethodUnitTest::getTargetToken() method as well as abstract out the actual testing to a helper method as the logic was the same in all methods. I've chosen not to implement this with a data provider as the separate test methods creating the `$expected` arrays make the tests more readable and easier to understand. Also switched the actual assertion to `assertArraySubset()` which removes the need for all the `unset()`s for the exact token positions. Ref: https://phpunit.de/manual/4.8/en/appendixes.assertions.html#appendixes.assertions.assertArraySubset
1 parent 281fc7d commit a36e967

File tree

2 files changed

+29
-180
lines changed

2 files changed

+29
-180
lines changed

tests/Core/File/GetMethodParametersTest.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ function defaultValues($var1=1, $var2='value') {}
1919
function typeHint(foo $var1, bar $var2) {}
2020

2121
class MyClass {
22-
/* testSelfTypeHint */ function typeSelfHint(self $var) {}
22+
/* testSelfTypeHint */
23+
function typeSelfHint(self $var) {}
2324
}
2425

2526
/* testNullableTypeHint */

tests/Core/File/GetMethodParametersTest.php

Lines changed: 27 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,7 @@ public function testPassByReference()
3232
'nullable_type' => false,
3333
];
3434

35-
$start = (self::$phpcsFile->numTokens - 1);
36-
$function = self::$phpcsFile->findPrevious(
37-
T_COMMENT,
38-
$start,
39-
null,
40-
false,
41-
'/* testPassByReference */'
42-
);
43-
44-
$found = self::$phpcsFile->getMethodParameters(($function + 2));
45-
unset($found[0]['token']);
46-
unset($found[0]['type_hint_token']);
47-
unset($found[0]['type_hint_end_token']);
48-
unset($found[0]['comma_token']);
49-
unset($found[0]['reference_token']);
50-
unset($found[0]['variadic_token']);
51-
$this->assertSame($expected, $found);
35+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
5236

5337
}//end testPassByReference()
5438

@@ -70,23 +54,7 @@ public function testArrayHint()
7054
'nullable_type' => false,
7155
];
7256

73-
$start = (self::$phpcsFile->numTokens - 1);
74-
$function = self::$phpcsFile->findPrevious(
75-
T_COMMENT,
76-
$start,
77-
null,
78-
false,
79-
'/* testArrayHint */'
80-
);
81-
82-
$found = self::$phpcsFile->getMethodParameters(($function + 2));
83-
unset($found[0]['token']);
84-
unset($found[0]['type_hint_token']);
85-
unset($found[0]['type_hint_end_token']);
86-
unset($found[0]['comma_token']);
87-
unset($found[0]['reference_token']);
88-
unset($found[0]['variadic_token']);
89-
$this->assertSame($expected, $found);
57+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
9058

9159
}//end testArrayHint()
9260

@@ -117,29 +85,7 @@ public function testTypeHint()
11785
'nullable_type' => false,
11886
];
11987

120-
$start = (self::$phpcsFile->numTokens - 1);
121-
$function = self::$phpcsFile->findPrevious(
122-
T_COMMENT,
123-
$start,
124-
null,
125-
false,
126-
'/* testTypeHint */'
127-
);
128-
129-
$found = self::$phpcsFile->getMethodParameters(($function + 2));
130-
unset($found[0]['token']);
131-
unset($found[1]['token']);
132-
unset($found[0]['type_hint_token']);
133-
unset($found[1]['type_hint_token']);
134-
unset($found[0]['type_hint_end_token']);
135-
unset($found[1]['type_hint_end_token']);
136-
unset($found[0]['comma_token']);
137-
unset($found[1]['comma_token']);
138-
unset($found[0]['reference_token']);
139-
unset($found[1]['reference_token']);
140-
unset($found[0]['variadic_token']);
141-
unset($found[1]['variadic_token']);
142-
$this->assertSame($expected, $found);
88+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
14389

14490
}//end testTypeHint()
14591

@@ -161,23 +107,7 @@ public function testSelfTypeHint()
161107
'nullable_type' => false,
162108
];
163109

164-
$start = (self::$phpcsFile->numTokens - 1);
165-
$function = self::$phpcsFile->findPrevious(
166-
T_COMMENT,
167-
$start,
168-
null,
169-
false,
170-
'/* testSelfTypeHint */'
171-
);
172-
173-
$found = self::$phpcsFile->getMethodParameters(($function + 2));
174-
unset($found[0]['token']);
175-
unset($found[0]['type_hint_token']);
176-
unset($found[0]['type_hint_end_token']);
177-
unset($found[0]['comma_token']);
178-
unset($found[0]['reference_token']);
179-
unset($found[0]['variadic_token']);
180-
$this->assertSame($expected, $found);
110+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
181111

182112
}//end testSelfTypeHint()
183113

@@ -208,29 +138,7 @@ public function testNullableTypeHint()
208138
'nullable_type' => true,
209139
];
210140

211-
$start = (self::$phpcsFile->numTokens - 1);
212-
$function = self::$phpcsFile->findPrevious(
213-
T_COMMENT,
214-
$start,
215-
null,
216-
false,
217-
'/* testNullableTypeHint */'
218-
);
219-
220-
$found = self::$phpcsFile->getMethodParameters(($function + 2));
221-
unset($found[0]['token']);
222-
unset($found[1]['token']);
223-
unset($found[0]['type_hint_token']);
224-
unset($found[1]['type_hint_token']);
225-
unset($found[0]['type_hint_end_token']);
226-
unset($found[1]['type_hint_end_token']);
227-
unset($found[0]['comma_token']);
228-
unset($found[1]['comma_token']);
229-
unset($found[0]['reference_token']);
230-
unset($found[1]['reference_token']);
231-
unset($found[0]['variadic_token']);
232-
unset($found[1]['variadic_token']);
233-
$this->assertSame($expected, $found);
141+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
234142

235143
}//end testNullableTypeHint()
236144

@@ -252,23 +160,7 @@ public function testVariable()
252160
'nullable_type' => false,
253161
];
254162

255-
$start = (self::$phpcsFile->numTokens - 1);
256-
$function = self::$phpcsFile->findPrevious(
257-
T_COMMENT,
258-
$start,
259-
null,
260-
false,
261-
'/* testVariable */'
262-
);
263-
264-
$found = self::$phpcsFile->getMethodParameters(($function + 2));
265-
unset($found[0]['token']);
266-
unset($found[0]['type_hint_token']);
267-
unset($found[0]['type_hint_end_token']);
268-
unset($found[0]['comma_token']);
269-
unset($found[0]['reference_token']);
270-
unset($found[0]['variadic_token']);
271-
$this->assertSame($expected, $found);
163+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
272164

273165
}//end testVariable()
274166

@@ -291,25 +183,7 @@ public function testSingleDefaultValue()
291183
'nullable_type' => false,
292184
];
293185

294-
$start = (self::$phpcsFile->numTokens - 1);
295-
$function = self::$phpcsFile->findPrevious(
296-
T_COMMENT,
297-
$start,
298-
null,
299-
false,
300-
'/* testSingleDefaultValue */'
301-
);
302-
303-
$found = self::$phpcsFile->getMethodParameters(($function + 2));
304-
unset($found[0]['token']);
305-
unset($found[0]['type_hint_token']);
306-
unset($found[0]['type_hint_end_token']);
307-
unset($found[0]['comma_token']);
308-
unset($found[0]['reference_token']);
309-
unset($found[0]['variadic_token']);
310-
unset($found[0]['default_token']);
311-
unset($found[0]['default_equal_token']);
312-
$this->assertSame($expected, $found);
186+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
313187

314188
}//end testSingleDefaultValue()
315189

@@ -341,33 +215,7 @@ public function testDefaultValues()
341215
'nullable_type' => false,
342216
];
343217

344-
$start = (self::$phpcsFile->numTokens - 1);
345-
$function = self::$phpcsFile->findPrevious(
346-
T_COMMENT,
347-
$start,
348-
null,
349-
false,
350-
'/* testDefaultValues */'
351-
);
352-
353-
$found = self::$phpcsFile->getMethodParameters(($function + 2));
354-
unset($found[0]['token']);
355-
unset($found[1]['token']);
356-
unset($found[0]['type_hint_token']);
357-
unset($found[1]['type_hint_token']);
358-
unset($found[0]['type_hint_end_token']);
359-
unset($found[1]['type_hint_end_token']);
360-
unset($found[0]['comma_token']);
361-
unset($found[1]['comma_token']);
362-
unset($found[0]['reference_token']);
363-
unset($found[1]['reference_token']);
364-
unset($found[0]['variadic_token']);
365-
unset($found[1]['variadic_token']);
366-
unset($found[0]['default_token']);
367-
unset($found[1]['default_token']);
368-
unset($found[0]['default_equal_token']);
369-
unset($found[1]['default_equal_token']);
370-
$this->assertSame($expected, $found);
218+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
371219

372220
}//end testDefaultValues()
373221

@@ -390,27 +238,27 @@ public function testBitwiseAndConstantExpressionDefaultValue()
390238
'nullable_type' => false,
391239
];
392240

393-
$start = (self::$phpcsFile->numTokens - 1);
394-
$function = self::$phpcsFile->findPrevious(
395-
T_COMMENT,
396-
$start,
397-
null,
398-
false,
399-
'/* testBitwiseAndConstantExpressionDefaultValue */'
400-
);
401-
402-
$found = self::$phpcsFile->getMethodParameters(($function + 2));
403-
unset($found[0]['token']);
404-
unset($found[0]['type_hint_token']);
405-
unset($found[0]['type_hint_end_token']);
406-
unset($found[0]['comma_token']);
407-
unset($found[0]['reference_token']);
408-
unset($found[0]['variadic_token']);
409-
unset($found[0]['default_token']);
410-
unset($found[0]['default_equal_token']);
411-
$this->assertSame($expected, $found);
241+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
412242

413243
}//end testBitwiseAndConstantExpressionDefaultValue()
414244

415245

246+
/**
247+
* Test helper.
248+
*
249+
* @param string $commentString The comment which preceeds the test.
250+
* @param array $expected The expected function output.
251+
*
252+
* @return void
253+
*/
254+
private function getMethodParametersTestHelper($commentString, $expected)
255+
{
256+
$function = $this->getTargetToken($commentString, [T_FUNCTION]);
257+
$found = self::$phpcsFile->getMethodParameters($function);
258+
259+
$this->assertArraySubset($expected, $found, true);
260+
261+
}//end getMethodParametersTestHelper()
262+
263+
416264
}//end class

0 commit comments

Comments
 (0)