-
Notifications
You must be signed in to change notification settings - Fork 0
DE-151249 Add TestsExtendMockeryTestCaseRule which enforces a Mockery base test case class #112
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
dominikkaluza
merged 3 commits into
master
from
DE-151249-phpstan-mockery-test-case-rule
Dec 17, 2025
Merged
Changes from all commits
Commits
Show all changes
3 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
89 changes: 89 additions & 0 deletions
89
...d/PhpStan/Rules/Mockery/TestsExtendMockeryTestCaseRule/TestsExtendMockeryTestCaseRule.php
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,89 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule; | ||
|
|
||
| use Mockery\Adapter\Phpunit\MockeryTestCase; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use PHPStan\Rules\IdentifierRuleError; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
| use SlevomatCodingStandard\Sniffs\TestCase as SlevomatCodingStandardTestCase; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * @implements Rule<Class_> | ||
| */ | ||
| class TestsExtendMockeryTestCaseRule implements Rule | ||
| { | ||
| public function __construct( | ||
| private readonly ReflectionProvider $reflectionProvider, | ||
| ) { | ||
| } | ||
|
|
||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return Class_::class; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @param Class_ $node | ||
| * | ||
| * @return list<IdentifierRuleError> | ||
| */ | ||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if ($node->name === null || $node->isAbstract()) { | ||
| return []; | ||
| } | ||
|
|
||
| $namespacedName = $node->namespacedName; | ||
|
|
||
| if ($namespacedName === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $className = $scope->resolveName($namespacedName); | ||
|
|
||
| if (!$this->reflectionProvider->hasClass($className)) { | ||
| return []; | ||
| } | ||
|
|
||
| $class = $this->reflectionProvider->getClass($className); | ||
|
|
||
| if (!$class->isSubclassOf(TestCase::class)) { | ||
| return []; | ||
| } | ||
|
|
||
| // Allow these code quality tools test cases as they are not using Mockery | ||
| if ($class->isSubclassOf(SlevomatCodingStandardTestCase::class) | ||
| || $class->isSubclassOf(AbstractRectorTestCase::class) | ||
| || $class->isSubclassOf(RuleTestCase::class)) { | ||
| return []; | ||
| } | ||
|
|
||
| if ($class->isSubclassOf(MockeryTestCase::class)) { | ||
| return []; | ||
| } | ||
|
|
||
| $ruleErrorMessage = sprintf( | ||
| 'PHPUnit test %s must extend %s (directly or indirectly).', | ||
| $class->getName(), | ||
| MockeryTestCase::class, | ||
| ); | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message($ruleErrorMessage) | ||
| ->identifier('mockery.testCase') | ||
| ->nonIgnorable() | ||
| ->build(), | ||
| ]; | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
...pStan/Rules/Mockery/TestsExtendMockeryTestCaseRule/TestsExtendMockeryTestCaseRuleTest.php
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 BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule; | ||
|
|
||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<TestsExtendMockeryTestCaseRule> | ||
| */ | ||
| class TestsExtendMockeryTestCaseRuleTest extends RuleTestCase | ||
| { | ||
| protected function getRule(): Rule | ||
| { | ||
| return new TestsExtendMockeryTestCaseRule($this->createReflectionProvider()); | ||
| } | ||
|
|
||
|
|
||
| public function testExtendsMockeryTestCase(): void | ||
| { | ||
| $this->analyse( | ||
| [__DIR__ . '/__fixtures__/TestExtendsMockeryTestCase.php'], | ||
| [], | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| public function testExtendsClassWhichExtendsMockeryTestCase(): void | ||
| { | ||
| $this->analyse( | ||
| [__DIR__ . '/__fixtures__/TestExtendsClassWhichExtendsMockeryTestCase.php'], | ||
| [], | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| public function testExtendsPhpUnitTestCase(): void | ||
| { | ||
| $this->analyse( | ||
| [__DIR__ . '/__fixtures__/TestExtendsPhpUnitTestCase.php'], | ||
| [ | ||
| [ | ||
| 'PHPUnit test BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\__fixtures__\TestExtendsPhpUnitTestCase must extend Mockery\Adapter\Phpunit\MockeryTestCase (directly or indirectly).', | ||
| 7, | ||
| ], | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| public function testExtendsClassWhichExtendsPhpUnitTestCase(): void | ||
| { | ||
| $this->analyse( | ||
| [__DIR__ . '/__fixtures__/TestExtendsClassWhichExtendsPhpUnitTestCase.php'], | ||
| [ | ||
| [ | ||
| 'PHPUnit test BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\__fixtures__\TestExtendsClassWhichExtendsPhpUnitTestCase must extend Mockery\Adapter\Phpunit\MockeryTestCase (directly or indirectly).', | ||
| 5, | ||
| ], | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| public function testExtendsPhpstanRuleTestCase(): void | ||
| { | ||
| $this->analyse( | ||
| [__DIR__ . '/__fixtures__/TestExtendsPhpstanRuleTestCase.php'], | ||
| [], | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| public function testExtendsRectorRuleTestCase(): void | ||
| { | ||
| $this->analyse( | ||
| [__DIR__ . '/__fixtures__/TestExtendsRectorRuleTestCase.php'], | ||
| [], | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| public function testExtendsSlevomatCodingStandardTestCase(): void | ||
| { | ||
| $this->analyse( | ||
| [__DIR__ . '/__fixtures__/TestExtendsSlevomatCodingStandardTestCase.php'], | ||
| [], | ||
| ); | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
...stsExtendMockeryTestCaseRule/__fixtures__/TestExtendsClassWhichExtendsMockeryTestCase.php
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,7 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\__fixtures__; | ||
|
|
||
| class TestExtendsClassWhichExtendsMockeryTestCase extends TestExtendsMockeryTestCase | ||
| { | ||
| } |
7 changes: 7 additions & 0 deletions
7
...stsExtendMockeryTestCaseRule/__fixtures__/TestExtendsClassWhichExtendsPhpUnitTestCase.php
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,7 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\__fixtures__; | ||
|
|
||
| class TestExtendsClassWhichExtendsPhpUnitTestCase extends TestExtendsPhpUnitTestCase | ||
| { | ||
| } |
9 changes: 9 additions & 0 deletions
9
.../Rules/Mockery/TestsExtendMockeryTestCaseRule/__fixtures__/TestExtendsMockeryTestCase.php
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,9 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\__fixtures__; | ||
|
|
||
| use Mockery\Adapter\Phpunit\MockeryTestCase; | ||
|
|
||
| class TestExtendsMockeryTestCase extends MockeryTestCase | ||
| { | ||
| } |
9 changes: 9 additions & 0 deletions
9
.../Rules/Mockery/TestsExtendMockeryTestCaseRule/__fixtures__/TestExtendsPhpUnitTestCase.php
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,9 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\__fixtures__; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class TestExtendsPhpUnitTestCase extends TestCase | ||
| { | ||
| } |
9 changes: 9 additions & 0 deletions
9
...es/Mockery/TestsExtendMockeryTestCaseRule/__fixtures__/TestExtendsPhpstanRuleTestCase.php
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,9 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\__fixtures__; | ||
|
|
||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| class TestExtendsPhpstanRuleTestCase extends RuleTestCase | ||
| { | ||
| } |
13 changes: 13 additions & 0 deletions
13
...les/Mockery/TestsExtendMockeryTestCaseRule/__fixtures__/TestExtendsRectorRuleTestCase.php
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,13 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\__fixtures__; | ||
|
|
||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| class TestExtendsRectorRuleTestCase extends AbstractRectorTestCase | ||
| { | ||
| public function provideConfigFilePath(): string | ||
| { | ||
| return ''; | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
...TestsExtendMockeryTestCaseRule/__fixtures__/TestExtendsSlevomatCodingStandardTestCase.php
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,9 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\__fixtures__; | ||
|
|
||
| use SlevomatCodingStandard\Sniffs\TestCase; | ||
|
|
||
| class TestExtendsSlevomatCodingStandardTestCase extends TestCase | ||
| { | ||
| } |
Oops, something went wrong.
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.