Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ services:
- Rector\Skipper\Fnmatcher
- Rector\Skipper\RealpathMatcher

-
class: BrandEmbassyCodingStandard\PhpStan\Rules\Mockery\TestsExtendMockeryTestCaseRule\TestsExtendMockeryTestCaseRule
tags:
- phpstan.rules.rule

parameters:
level: max
paths:
Expand Down
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(),
];
}
}
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'],
[],
);
}
}
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
{
}
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
{
}
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
{
}
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
{
}
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
{
}
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 '';
}
}
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
{
}
Loading