-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
321 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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.', | ||
$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 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 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 | ||
{ | ||
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); | ||
} | ||
} |