Skip to content

Commit e6b0673

Browse files
committed
GetMethodPropertiesTest: 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.
1 parent a36e967 commit e6b0673

File tree

2 files changed

+35
-181
lines changed

2 files changed

+35
-181
lines changed

tests/Core/File/GetMethodPropertiesTest.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ function myFunction() {}
66
/* testReturnFunction */
77
function myFunction(array ...$arrays): array
88
{
9-
return array_map(/* testNestedClosure */function(array $array): int {
9+
/* testNestedClosure */
10+
return array_map(function(array $array): int {
1011
return array_sum($array);
1112
}, $arrays);
1213
}

tests/Core/File/GetMethodPropertiesTest.php

Lines changed: 33 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,7 @@ public function testBasicFunction()
3333
'has_body' => true,
3434
];
3535

36-
$start = (self::$phpcsFile->numTokens - 1);
37-
$function = self::$phpcsFile->findPrevious(
38-
T_COMMENT,
39-
$start,
40-
null,
41-
false,
42-
'/* testBasicFunction */'
43-
);
44-
45-
$found = self::$phpcsFile->getMethodProperties(($function + 2));
46-
unset($found['return_type_token']);
47-
$this->assertSame($expected, $found);
36+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
4837

4938
}//end testBasicFunction()
5039

@@ -67,18 +56,7 @@ public function testReturnFunction()
6756
'has_body' => true,
6857
];
6958

70-
$start = (self::$phpcsFile->numTokens - 1);
71-
$function = self::$phpcsFile->findPrevious(
72-
T_COMMENT,
73-
$start,
74-
null,
75-
false,
76-
'/* testReturnFunction */'
77-
);
78-
79-
$found = self::$phpcsFile->getMethodProperties(($function + 2));
80-
unset($found['return_type_token']);
81-
$this->assertSame($expected, $found);
59+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
8260

8361
}//end testReturnFunction()
8462

@@ -101,18 +79,7 @@ public function testNestedClosure()
10179
'has_body' => true,
10280
];
10381

104-
$start = (self::$phpcsFile->numTokens - 1);
105-
$function = self::$phpcsFile->findPrevious(
106-
T_COMMENT,
107-
$start,
108-
null,
109-
false,
110-
'/* testNestedClosure */'
111-
);
112-
113-
$found = self::$phpcsFile->getMethodProperties(($function + 1));
114-
unset($found['return_type_token']);
115-
$this->assertSame($expected, $found);
82+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
11683

11784
}//end testNestedClosure()
11885

@@ -135,18 +102,7 @@ public function testBasicMethod()
135102
'has_body' => true,
136103
];
137104

138-
$start = (self::$phpcsFile->numTokens - 1);
139-
$function = self::$phpcsFile->findPrevious(
140-
T_COMMENT,
141-
$start,
142-
null,
143-
false,
144-
'/* testBasicMethod */'
145-
);
146-
147-
$found = self::$phpcsFile->getMethodProperties(($function + 3));
148-
unset($found['return_type_token']);
149-
$this->assertSame($expected, $found);
105+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
150106

151107
}//end testBasicMethod()
152108

@@ -169,18 +125,7 @@ public function testPrivateStaticMethod()
169125
'has_body' => true,
170126
];
171127

172-
$start = (self::$phpcsFile->numTokens - 1);
173-
$function = self::$phpcsFile->findPrevious(
174-
T_COMMENT,
175-
$start,
176-
null,
177-
false,
178-
'/* testPrivateStaticMethod */'
179-
);
180-
181-
$found = self::$phpcsFile->getMethodProperties(($function + 7));
182-
unset($found['return_type_token']);
183-
$this->assertSame($expected, $found);
128+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
184129

185130
}//end testPrivateStaticMethod()
186131

@@ -203,18 +148,7 @@ public function testFinalMethod()
203148
'has_body' => true,
204149
];
205150

206-
$start = (self::$phpcsFile->numTokens - 1);
207-
$function = self::$phpcsFile->findPrevious(
208-
T_COMMENT,
209-
$start,
210-
null,
211-
false,
212-
'/* testFinalMethod */'
213-
);
214-
215-
$found = self::$phpcsFile->getMethodProperties(($function + 7));
216-
unset($found['return_type_token']);
217-
$this->assertSame($expected, $found);
151+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
218152

219153
}//end testFinalMethod()
220154

@@ -237,18 +171,7 @@ public function testProtectedReturnMethod()
237171
'has_body' => true,
238172
];
239173

240-
$start = (self::$phpcsFile->numTokens - 1);
241-
$function = self::$phpcsFile->findPrevious(
242-
T_COMMENT,
243-
$start,
244-
null,
245-
false,
246-
'/* testProtectedReturnMethod */'
247-
);
248-
249-
$found = self::$phpcsFile->getMethodProperties(($function + 5));
250-
unset($found['return_type_token']);
251-
$this->assertSame($expected, $found);
174+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
252175

253176
}//end testProtectedReturnMethod()
254177

@@ -271,18 +194,7 @@ public function testPublicReturnMethod()
271194
'has_body' => true,
272195
];
273196

274-
$start = (self::$phpcsFile->numTokens - 1);
275-
$function = self::$phpcsFile->findPrevious(
276-
T_COMMENT,
277-
$start,
278-
null,
279-
false,
280-
'/* testPublicReturnMethod */'
281-
);
282-
283-
$found = self::$phpcsFile->getMethodProperties(($function + 5));
284-
unset($found['return_type_token']);
285-
$this->assertSame($expected, $found);
197+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
286198

287199
}//end testPublicReturnMethod()
288200

@@ -305,18 +217,7 @@ public function testNullableReturnMethod()
305217
'has_body' => true,
306218
];
307219

308-
$start = (self::$phpcsFile->numTokens - 1);
309-
$function = self::$phpcsFile->findPrevious(
310-
T_COMMENT,
311-
$start,
312-
null,
313-
false,
314-
'/* testNullableReturnMethod */'
315-
);
316-
317-
$found = self::$phpcsFile->getMethodProperties(($function + 5));
318-
unset($found['return_type_token']);
319-
$this->assertSame($expected, $found);
220+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
320221

321222
}//end testNullableReturnMethod()
322223

@@ -339,18 +240,7 @@ public function testMessyNullableReturnMethod()
339240
'has_body' => true,
340241
];
341242

342-
$start = (self::$phpcsFile->numTokens - 1);
343-
$function = self::$phpcsFile->findPrevious(
344-
T_COMMENT,
345-
$start,
346-
null,
347-
false,
348-
'/* testMessyNullableReturnMethod */'
349-
);
350-
351-
$found = self::$phpcsFile->getMethodProperties(($function + 5));
352-
unset($found['return_type_token']);
353-
$this->assertSame($expected, $found);
243+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
354244

355245
}//end testMessyNullableReturnMethod()
356246

@@ -373,18 +263,7 @@ public function testReturnNamespace()
373263
'has_body' => true,
374264
];
375265

376-
$start = (self::$phpcsFile->numTokens - 1);
377-
$function = self::$phpcsFile->findPrevious(
378-
T_COMMENT,
379-
$start,
380-
null,
381-
false,
382-
'/* testReturnNamespace */'
383-
);
384-
385-
$found = self::$phpcsFile->getMethodProperties(($function + 3));
386-
unset($found['return_type_token']);
387-
$this->assertSame($expected, $found);
266+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
388267

389268
}//end testReturnNamespace()
390269

@@ -407,18 +286,7 @@ public function testReturnMultilineNamespace()
407286
'has_body' => true,
408287
];
409288

410-
$start = (self::$phpcsFile->numTokens - 1);
411-
$function = self::$phpcsFile->findPrevious(
412-
T_COMMENT,
413-
$start,
414-
null,
415-
false,
416-
'/* testReturnMultilineNamespace */'
417-
);
418-
419-
$found = self::$phpcsFile->getMethodProperties(($function + 3));
420-
unset($found['return_type_token']);
421-
$this->assertSame($expected, $found);
289+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
422290

423291
}//end testReturnMultilineNamespace()
424292

@@ -441,18 +309,7 @@ public function testAbstractMethod()
441309
'has_body' => false,
442310
];
443311

444-
$start = (self::$phpcsFile->numTokens - 1);
445-
$function = self::$phpcsFile->findPrevious(
446-
T_COMMENT,
447-
$start,
448-
null,
449-
false,
450-
'/* testAbstractMethod */'
451-
);
452-
453-
$found = self::$phpcsFile->getMethodProperties(($function + 5));
454-
unset($found['return_type_token']);
455-
$this->assertSame($expected, $found);
312+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
456313

457314
}//end testAbstractMethod()
458315

@@ -475,18 +332,7 @@ public function testAbstractReturnMethod()
475332
'has_body' => false,
476333
];
477334

478-
$start = (self::$phpcsFile->numTokens - 1);
479-
$function = self::$phpcsFile->findPrevious(
480-
T_COMMENT,
481-
$start,
482-
null,
483-
false,
484-
'/* testAbstractReturnMethod */'
485-
);
486-
487-
$found = self::$phpcsFile->getMethodProperties(($function + 7));
488-
unset($found['return_type_token']);
489-
$this->assertSame($expected, $found);
335+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
490336

491337
}//end testAbstractReturnMethod()
492338

@@ -509,20 +355,27 @@ public function testInterfaceMethod()
509355
'has_body' => false,
510356
];
511357

512-
$start = (self::$phpcsFile->numTokens - 1);
513-
$function = self::$phpcsFile->findPrevious(
514-
T_COMMENT,
515-
$start,
516-
null,
517-
false,
518-
'/* testInterfaceMethod */'
519-
);
520-
521-
$found = self::$phpcsFile->getMethodProperties(($function + 3));
522-
unset($found['return_type_token']);
523-
$this->assertSame($expected, $found);
358+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
524359

525360
}//end testInterfaceMethod()
526361

527362

363+
/**
364+
* Test helper.
365+
*
366+
* @param string $commentString The comment which preceeds the test.
367+
* @param array $expected The expected function output.
368+
*
369+
* @return void
370+
*/
371+
private function getMethodPropertiesTestHelper($commentString, $expected)
372+
{
373+
$function = $this->getTargetToken($commentString, [T_FUNCTION, T_CLOSURE]);
374+
$found = self::$phpcsFile->getMethodProperties($function);
375+
376+
$this->assertArraySubset($expected, $found, true);
377+
378+
}//end getMethodPropertiesTestHelper()
379+
380+
528381
}//end class

0 commit comments

Comments
 (0)