Skip to content

Commit 4515e65

Browse files
committed
minor #4532 DX: ensure data providers are used (kubawerlos)
This PR was squashed before being merged into the 2.15 branch (closes #4532). Discussion ---------- DX: ensure data providers are used Commits ------- 9cb7bf2 DX: ensure data providers are used
2 parents 04b2004 + 9cb7bf2 commit 4515e65

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

tests/AutoReview/ProjectCodeTest.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ static function (\ReflectionMethod $reflectionMethod) use ($reflectionClass) {
275275
*/
276276
public function testThatDataProvidersAreCorrectlyNamed($testClassName)
277277
{
278-
$dataProviderMethodNames = $this->getDataProviderMethodNames($testClassName);
278+
$usedDataProviderMethodNames = $this->getUsedDataProviderMethodNames($testClassName);
279279

280280
if (empty($dataProviderMethodNames)) {
281281
$this->addToAssertionCount(1); // no data providers to test, all good!
282282
}
283283

284-
foreach ($dataProviderMethodNames as $dataProviderMethodName) {
284+
foreach ($usedDataProviderMethodNames as $dataProviderMethodName) {
285285
static::assertRegExp('/^provide[A-Z]\S+Cases$/', $dataProviderMethodName, sprintf(
286286
'Data provider in "%s" with name "%s" is not correctly named.',
287287
$testClassName,
@@ -290,6 +290,38 @@ public function testThatDataProvidersAreCorrectlyNamed($testClassName)
290290
}
291291
}
292292

293+
/**
294+
* @dataProvider provideTestClassCases
295+
*
296+
* @param string $testClassName
297+
*/
298+
public function testThatDataProvidersAreUsed($testClassName)
299+
{
300+
$reflectionClass = new \ReflectionClass($testClassName);
301+
302+
$definedDataProviders = array_filter(
303+
$reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC),
304+
static function (\ReflectionMethod $reflectionMethod) use ($reflectionClass) {
305+
return $reflectionMethod->getDeclaringClass()->getName() === $reflectionClass->getName()
306+
&& 'provide' === substr($reflectionMethod->getName(), 0, 7);
307+
}
308+
);
309+
310+
if ($definedDataProviders === []) {
311+
$this->addToAssertionCount(1); // no methods to test, all good!
312+
}
313+
314+
$usedDataProviderMethodNames = $this->getUsedDataProviderMethodNames($testClassName);
315+
316+
foreach ($definedDataProviders as $definedDataProvider) {
317+
static::assertContains(
318+
$definedDataProvider->getName(),
319+
$usedDataProviderMethodNames,
320+
sprintf('Data provider in "%s" with name "%s" is not used.', $definedDataProvider->getDeclaringClass()->getName(), $definedDataProvider->getName())
321+
);
322+
}
323+
}
324+
293325
/**
294326
* @dataProvider provideClassesWherePregFunctionsAreForbiddenCases
295327
*
@@ -406,7 +438,7 @@ static function ($className) {
406438
);
407439
}
408440

409-
private function getDataProviderMethodNames($testClassName)
441+
private function getUsedDataProviderMethodNames($testClassName)
410442
{
411443
$dataProviderMethodNames = [];
412444
$tokens = Tokens::fromCode(file_get_contents(

tests/Fixer/ConstantNotation/NativeConstantInvocationFixerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public function provideFix70WithDefaultConfigurationCases()
222222
}
223223

224224
/**
225-
* @dataProvider provideFix70WithDefaultConfigurationCases
225+
* @dataProvider provideFix71WithDefaultConfigurationCases
226226
*
227227
* @param string $expected
228228
* @param null|string $input

tests/Fixer/FunctionNotation/NoUnreachableDefaultArgumentValueFixerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function testFix56($expected, $input = null)
181181
/**
182182
* @return array
183183
*/
184-
public function provideFix56cases()
184+
public function provideFix56Cases()
185185
{
186186
return [
187187
[

0 commit comments

Comments
 (0)