Skip to content

Commit a5adef5

Browse files
committed
Add test for NoDocumentMockingRule and skip interface/abstract classes
1 parent d892b6f commit a5adef5

3 files changed

Lines changed: 47 additions & 8 deletions

File tree

src/Rules/Doctrine/NoDocumentMockingRule.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\MethodCall;
99
use PHPStan\Analyser\Scope;
10+
use PHPStan\Reflection\ReflectionProvider;
1011
use PHPStan\Rules\Rule;
1112
use PHPStan\Rules\RuleErrorBuilder;
13+
use PHPStan\Type\Constant\ConstantStringType;
1214
use Symplify\PHPStanRules\Enum\RuleIdentifier\PHPUnitRuleIdentifier;
1315
use Symplify\PHPStanRules\Helper\NamingHelper;
1416

1517
/**
1618
* @implements Rule<MethodCall>
19+
* @see \Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule\NoDocumentMockingRuleTest
1720
*/
18-
final class NoDocumentMockingRule implements Rule
21+
final readonly class NoDocumentMockingRule implements Rule
1922
{
2023
public const string ERROR_MESSAGE = 'Instead of document mocking, create object directly to get better type support';
2124

25+
public function __construct(
26+
private ReflectionProvider $reflectionProvider
27+
) {
28+
}
29+
2230
public function getNodeType(): string
2331
{
2432
return MethodCall::class;
@@ -40,7 +48,11 @@ public function processNode(Node $node, Scope $scope): array
4048
$firstArg = $node->getArgs()[0];
4149
$mockedClassType = $scope->getType($firstArg->value);
4250
foreach ($mockedClassType->getConstantStrings() as $constantStringType) {
43-
if (! str_contains($constantStringType->getValue(), '\\Document\\')) {
51+
if (! str_contains($constantStringType->getValue(), '\\Document\\') && ! str_contains($constantStringType->getValue(), '\\Entity\\')) {
52+
continue;
53+
}
54+
55+
if ($this->shouldSkipDocumentClass($constantStringType)) {
4456
continue;
4557
}
4658

@@ -53,4 +65,20 @@ public function processNode(Node $node, Scope $scope): array
5365

5466
return [];
5567
}
68+
69+
private function shouldSkipDocumentClass(ConstantStringType $constantStringType): bool
70+
{
71+
if ($this->reflectionProvider->hasClass($constantStringType->getValue())) {
72+
$classReflection = $this->reflectionProvider->getClass($constantStringType->getValue());
73+
if ($classReflection->isAbstract()) {
74+
return true;
75+
}
76+
77+
if ($classReflection->isInterface()) {
78+
return true;
79+
}
80+
}
81+
82+
return false;
83+
}
5684
}

tests/Rules/Doctrine/NoDocumentMockingRule/NoDocumentMockingRuleTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule;
66

77
use Iterator;
8-
use PHPStan\Reflection\ReflectionProvider;
8+
use Override;
99
use PHPStan\Rules\Rule;
1010
use PHPStan\Testing\RuleTestCase;
1111
use PHPUnit\Framework\Attributes\DataProvider;
1212
use Symplify\PHPStanRules\Rules\Doctrine\NoDocumentMockingRule;
13-
use Symplify\PHPStanRules\Rules\Doctrine\NoGetRepositoryOnServiceRepositoryEntityRule;
14-
use Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOnServiceRepositoryEntityRule\Source\Repository\SomeServiceRepository;
1513

1614
final class NoDocumentMockingRuleTest extends RuleTestCase
1715
{
@@ -29,17 +27,25 @@ public function testRule(string $filePath, array $expectedErrorMessagesWithLines
2927
*/
3028
public static function provideData(): Iterator
3129
{
32-
$errorMessage = sprintf(NoGetRepositoryOnServiceRepositoryEntityRule::ERROR_MESSAGE, 'SomeEntity', SomeServiceRepository::class);
3330
yield [__DIR__ . '/Fixture/SomeEntityMocking.php', [[
34-
$errorMessage,
31+
NoDocumentMockingRule::ERROR_MESSAGE,
3532
14,
3633
]]];
3734

3835
yield [__DIR__ . '/Fixture/SomeAbstractEntityMocking.php', []];
3936
}
4037

38+
/**
39+
* @return string[]
40+
*/
41+
#[Override]
42+
public static function getAdditionalConfigFiles(): array
43+
{
44+
return [__DIR__ . '/config/configured_rule.neon'];
45+
}
46+
4147
protected function getRule(): Rule
4248
{
43-
return new NoDocumentMockingRule();
49+
return self::getContainer()->getByType(NoDocumentMockingRule::class);
4450
}
4551
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
includes:
2+
- ../../../../config/included_services.neon
3+
4+
rules:
5+
- Symplify\PHPStanRules\Rules\Doctrine\NoDocumentMockingRule

0 commit comments

Comments
 (0)