-
-
Notifications
You must be signed in to change notification settings - Fork 26
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
1 parent
b596dde
commit c68982f
Showing
10 changed files
with
233 additions
and
7 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,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Rules\PHPUnit; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Type\ObjectType; | ||
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @implements Rule<MethodCall> | ||
*/ | ||
final class NoTestMocksRule implements Rule, DocumentedRuleInterface | ||
{ | ||
/** | ||
* @api | ||
* @var string | ||
*/ | ||
public const ERROR_MESSAGE = 'Mocking "%s" class is forbidden. Use direct/anonymous class instead for better static analysis'; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private const MOCKING_METHOD_NAMES = ['createMock', 'createPartialMock', 'createConfiguredMock', 'createStub']; | ||
|
||
/** | ||
* @param string[] $allowedTypes | ||
*/ | ||
public function __construct( | ||
private array $allowedTypes = [] | ||
) { | ||
} | ||
|
||
/** | ||
* @return class-string<Node> | ||
*/ | ||
public function getNodeType(): string | ||
{ | ||
return MethodCall::class; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (! $node->name instanceof Identifier) { | ||
return []; | ||
} | ||
|
||
$methodName = $node->name->toString(); | ||
if (! in_array($methodName, self::MOCKING_METHOD_NAMES, true)) { | ||
return []; | ||
} | ||
|
||
$mockedObjectType = $this->resolveMockedObjectType($node, $scope); | ||
if (! $mockedObjectType instanceof ObjectType) { | ||
return []; | ||
} | ||
|
||
if ($this->isAllowedType($mockedObjectType)) { | ||
return []; | ||
} | ||
|
||
$errorMessage = sprintf(self::ERROR_MESSAGE, $mockedObjectType->getClassName()); | ||
|
||
return [$errorMessage]; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition(self::ERROR_MESSAGE, [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SkipApiMock extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$someTypeMock = $this->createMock(SomeType::class); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SkipApiMock extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$someTypeMock = new class() implements SomeType {}; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
private function resolveMockedObjectType(MethodCall $methodCall, Scope $scope): ?ObjectType | ||
{ | ||
$args = $methodCall->getArgs(); | ||
|
||
$mockedArgValue = $args[0]->value; | ||
$variableType = $scope->getType($mockedArgValue); | ||
|
||
foreach ($variableType->getConstantStrings() as $constantString) { | ||
return new ObjectType($constantString->getValue()); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function isAllowedType(ObjectType $objectType): bool | ||
{ | ||
foreach ($this->allowedTypes as $allowedType) { | ||
if ($objectType->getClassName() === $allowedType) { | ||
return true; | ||
} | ||
|
||
if ($objectType->isInstanceOf($allowedType)->yes()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
tests/Rules/PHPUnit/NoTestMocksRule/Fixture/SkipApiMock.php
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoTestMocksRule\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symplify\PHPStanRules\Tests\PHPUnit\Rules\NoTestMocksRule\Source\SomeAllowedType; | ||
|
||
final class SkipApiMock extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$someAllowedTypeMock = $this->createMock(SomeAllowedType::class); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Rules/PHPUnit/NoTestMocksRule/Fixture/SomeMocking.php
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\PHPUnit\Rules\NoTestMocksRule\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeMocking extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$someClassMock = $this->createMock('SomeClass'); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
tests/Rules/PHPUnit/NoTestMocksRule/NoTestMocksRuleTest.php
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoTestMocksRule; | ||
|
||
use Iterator; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Symplify\PHPStanRules\Rules\PHPUnit\NoTestMocksRule; | ||
|
||
final class NoTestMocksRuleTest extends RuleTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void | ||
{ | ||
$this->analyse([$filePath], $expectedErrorMessagesWithLines); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
yield [ | ||
__DIR__ . '/Fixture/SomeMocking.php', | ||
[[sprintf(NoTestMocksRule::ERROR_MESSAGE, 'SomeClass'), 13]], | ||
]; | ||
|
||
yield [__DIR__ . '/Fixture/SkipApiMock.php', []]; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/config/configured_rule.neon', | ||
]; | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return self::getContainer()->getByType(NoTestMocksRule::class); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/Rules/PHPUnit/NoTestMocksRule/Source/SomeAllowedType.php
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,7 @@ | ||
<?php | ||
|
||
namespace Symplify\PHPStanRules\Tests\PHPUnit\Rules\NoTestMocksRule\Source; | ||
|
||
class SomeAllowedType | ||
{ | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/Rules/PHPUnit/NoTestMocksRule/config/configured_rule.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,7 @@ | ||
services: | ||
- | ||
class: Symplify\PHPStanRules\Rules\PHPUnit\NoTestMocksRule | ||
tags: [phpstan.rules.rule] | ||
arguments: | ||
allowedTypes: | ||
- Symplify\PHPStanRules\Tests\PHPUnit\Rules\NoTestMocksRule\Source\SomeAllowedType |