Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: check to have DataProviders code agnostic of PHP version #7575

Merged
merged 8 commits into from
Dec 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/AutoReview/ProjectCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PhpCsFixer\Tests\TestCase;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\TokensAnalyzer;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

Expand Down Expand Up @@ -471,6 +472,51 @@ public function testExpectedInputOrder(string $testClassName): void
}
}

/**
* @dataProvider provideDataProviderMethodCases
*/
public function testDataProvidersAreNonPhpVersionConditional(string $testClassName, \ReflectionMethod $dataProvider): void
{
$dataProviderName = $dataProvider->getName();
$methodId = $testClassName.'::'.$dataProviderName;

$tokens = $this->createTokensForClass($testClassName);
$tokensAnalyzer = new TokensAnalyzer($tokens);
$dataProviderElements = array_filter($tokensAnalyzer->getClassyElements(), static function (array $v, int $k) use ($tokens, $dataProviderName) {
$nextToken = $tokens[$tokens->getNextMeaningfulToken($k)];

// element is data provider method
return 'method' === $v['type'] && $nextToken->equals([T_STRING, $dataProviderName]);
}, ARRAY_FILTER_USE_BOTH);

if (1 !== \count($dataProviderElements)) {
throw new \UnexpectedValueException(sprintf('DataProvider `%s` should be found exactly once, got %d times.', $methodId, \count($dataProviderElements)));
}

$methodIndex = array_key_first($dataProviderElements);
$startIndex = $tokens->getNextTokenOfKind($methodIndex, ['{']);
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);

$versionTokens = array_filter($tokens->findGivenKind(T_STRING, $startIndex, $endIndex), static function (Token $v): bool {
return $v->equalsAny([
[T_STRING, 'PHP_VERSION_ID'],
[T_STRING, 'PHP_MAJOR_VERSION'],
[T_STRING, 'PHP_MINOR_VERSION'],
[T_STRING, 'PHP_RELEASE_VERSION'],
[T_STRING, 'phpversion'],
], false);
});

self::assertCount(
0,
$versionTokens,
sprintf(
"DataProvider '%s' should not check PHP version and provide different cases depends on it. It leads to situation when DataProvider provides 'sometimes 10, sometimes 11' test cases, depends on PHP version. That makes John Doe to see 'you run 10/10' and thinking all tests are executed, instead of actually seeing 'you run 10/11 and 1 skipped'.",
$methodId,
),
);
}

/**
* @dataProvider provideDataProviderMethodCases
*/
Expand Down