-
Notifications
You must be signed in to change notification settings - Fork 48
Add rule to check @dataProvider #150
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PHPUnit; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Type\FileTypeMapper; | ||
use PHPUnit\Framework\TestCase; | ||
use function array_merge; | ||
|
||
/** | ||
* @implements Rule<Node\Stmt\ClassMethod> | ||
*/ | ||
class DataProviderDeclarationRule implements Rule | ||
{ | ||
|
||
/** | ||
* Data provider helper. | ||
* | ||
* @var DataProviderHelper | ||
*/ | ||
private $dataProviderHelper; | ||
|
||
/** | ||
* The file type mapper. | ||
* | ||
* @var FileTypeMapper | ||
*/ | ||
private $fileTypeMapper; | ||
|
||
/** | ||
* When set to true, it reports data provider method with incorrect name case. | ||
* | ||
* @var bool | ||
*/ | ||
private $checkFunctionNameCase; | ||
|
||
public function __construct( | ||
DataProviderHelper $dataProviderHelper, | ||
FileTypeMapper $fileTypeMapper, | ||
bool $checkFunctionNameCase | ||
) | ||
{ | ||
$this->dataProviderHelper = $dataProviderHelper; | ||
$this->fileTypeMapper = $fileTypeMapper; | ||
$this->checkFunctionNameCase = $checkFunctionNameCase; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\ClassMethod::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$classReflection = $scope->getClassReflection(); | ||
|
||
if ($classReflection === null || !$classReflection->isSubclassOf(TestCase::class)) { | ||
return []; | ||
} | ||
|
||
$docComment = $node->getDocComment(); | ||
if ($docComment === null) { | ||
return []; | ||
} | ||
|
||
$methodPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc( | ||
$scope->getFile(), | ||
$classReflection->getName(), | ||
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null, | ||
$node->name->toString(), | ||
$docComment->getText() | ||
); | ||
|
||
$annotations = $this->dataProviderHelper->getDataProviderAnnotations($methodPhpDoc); | ||
|
||
$errors = []; | ||
|
||
foreach ($annotations as $annotation) { | ||
$errors = array_merge( | ||
$errors, | ||
$this->dataProviderHelper->processDataProvider($scope, $annotation, $this->checkFunctionNameCase) | ||
); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\PHPUnit; | ||
|
||
use PHPStan\Analyser\Scope; | ||
use PHPStan\PhpDoc\ResolvedPhpDocBlock; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | ||
use PHPStan\Reflection\MissingMethodFromReflectionException; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function array_merge; | ||
use function preg_match; | ||
use function sprintf; | ||
|
||
class DataProviderHelper | ||
{ | ||
|
||
/** | ||
* @return array<PhpDocTagNode> | ||
*/ | ||
public function getDataProviderAnnotations(?ResolvedPhpDocBlock $phpDoc): array | ||
{ | ||
if ($phpDoc === null) { | ||
return []; | ||
} | ||
|
||
$phpDocNodes = $phpDoc->getPhpDocNodes(); | ||
|
||
$annotations = []; | ||
|
||
foreach ($phpDocNodes as $docNode) { | ||
$annotations = array_merge( | ||
$annotations, | ||
$docNode->getTagsByName('@dataProvider') | ||
); | ||
} | ||
|
||
return $annotations; | ||
} | ||
|
||
/** | ||
* @return RuleError[] errors | ||
*/ | ||
public function processDataProvider( | ||
Scope $scope, | ||
PhpDocTagNode $phpDocTag, | ||
bool $checkFunctionNameCase | ||
): array | ||
{ | ||
$dataProviderName = $this->getDataProviderName($phpDocTag); | ||
if ($dataProviderName === null) { | ||
// Missing name is already handled in NoMissingSpaceInMethodAnnotationRule | ||
return []; | ||
} | ||
|
||
$classReflection = $scope->getClassReflection(); | ||
if ($classReflection === null) { | ||
// Should not happen | ||
return []; | ||
} | ||
|
||
try { | ||
$dataProviderMethodReflection = $classReflection->getNativeMethod($dataProviderName); | ||
} catch (MissingMethodFromReflectionException $missingMethodFromReflectionException) { | ||
$error = RuleErrorBuilder::message(sprintf( | ||
'@dataProvider %s related method not found.', | ||
$dataProviderName | ||
))->build(); | ||
|
||
return [$error]; | ||
} | ||
|
||
$errors = []; | ||
|
||
if ($checkFunctionNameCase && $dataProviderName !== $dataProviderMethodReflection->getName()) { | ||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'@dataProvider %s related method is used with incorrect case: %s.', | ||
villfa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$dataProviderName, | ||
$dataProviderMethodReflection->getName() | ||
))->build(); | ||
} | ||
|
||
if (!$dataProviderMethodReflection->isPublic()) { | ||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'@dataProvider %s related method must be public.', | ||
$dataProviderName | ||
))->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
private function getDataProviderName(PhpDocTagNode $phpDocTag): ?string | ||
{ | ||
if (preg_match('/^[^ \t]+/', (string) $phpDocTag->value, $matches) !== 1) { | ||
return null; | ||
} | ||
|
||
return $matches[0]; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPStan\Rules\PHPUnit; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPStan\Type\FileTypeMapper; | ||
|
||
/** | ||
* @extends RuleTestCase<DataProviderDeclarationRule> | ||
*/ | ||
class DataProviderDeclarationRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new DataProviderDeclarationRule( | ||
new DataProviderHelper(), | ||
self::getContainer()->getByType(FileTypeMapper::class), | ||
true | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/data-provider-declaration.php'], [ | ||
[ | ||
'@dataProvider providebaz related method is used with incorrect case: provideBaz.', | ||
13, | ||
], | ||
[ | ||
'@dataProvider provideQuux related method must be public.', | ||
13, | ||
], | ||
[ | ||
'@dataProvider provideNonExisting related method not found.', | ||
66, | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../../extension.neon', | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace ExampleTestCase; | ||
|
||
class FooTestCase extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @dataProvider provideBar Comment. | ||
* @dataProvider providebaz | ||
* @dataProvider provideQux | ||
* @dataProvider provideQuux | ||
*/ | ||
public function testIsNotFoo(string $subject): void | ||
villfa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
self::assertNotSame('foo', $subject); | ||
} | ||
|
||
public static function provideBar(): iterable | ||
{ | ||
return [ | ||
['bar'], | ||
]; | ||
} | ||
|
||
public static function provideBaz(): iterable | ||
{ | ||
return [ | ||
['baz'], | ||
]; | ||
} | ||
|
||
public function provideQux(): iterable | ||
{ | ||
return [ | ||
['qux'], | ||
]; | ||
} | ||
|
||
protected static function provideQuux(): iterable | ||
{ | ||
|
||
return [ | ||
['quux'], | ||
]; | ||
} | ||
} | ||
|
||
trait BarProvider | ||
{ | ||
public static function provideCorge(): iterable | ||
{ | ||
return [ | ||
['corge'], | ||
]; | ||
} | ||
} | ||
|
||
class BarTestCase extends \PHPUnit\Framework\TestCase | ||
{ | ||
use BarProvider; | ||
|
||
/** | ||
* @dataProvider provideNonExisting | ||
* @dataProvider provideCorge | ||
*/ | ||
public function testIsNotBar(string $subject): void | ||
{ | ||
self::assertNotSame('bar', $subject); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.