Skip to content

Commit 6b211c4

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

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/Rules/Doctrine/NoDocumentMockingRule.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@
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;
1213
use Symplify\PHPStanRules\Enum\RuleIdentifier\PHPUnitRuleIdentifier;
1314
use Symplify\PHPStanRules\Helper\NamingHelper;
1415

1516
/**
1617
* @implements Rule<MethodCall>
18+
* @see \Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule\NoDocumentMockingRuleTest
1719
*/
18-
final class NoDocumentMockingRule implements Rule
20+
final readonly class NoDocumentMockingRule implements Rule
1921
{
2022
public const string ERROR_MESSAGE = 'Instead of document mocking, create object directly to get better type support';
2123

24+
public function __construct(
25+
private ReflectionProvider $reflectionProvider
26+
) {
27+
}
28+
2229
public function getNodeType(): string
2330
{
2431
return MethodCall::class;
@@ -40,10 +47,20 @@ public function processNode(Node $node, Scope $scope): array
4047
$firstArg = $node->getArgs()[0];
4148
$mockedClassType = $scope->getType($firstArg->value);
4249
foreach ($mockedClassType->getConstantStrings() as $constantStringType) {
43-
if (! str_contains($constantStringType->getValue(), '\\Document\\')) {
50+
if (! str_contains($constantStringType->getValue(), '\\Document\\') && ! str_contains($constantStringType->getValue(), '\\Entity\\')) {
4451
continue;
4552
}
4653

54+
if ($this->reflectionProvider->hasClass($constantStringType->getValue())) {
55+
$classReflection = $this->reflectionProvider->getClass($constantStringType->getValue());
56+
if ($classReflection->isAbstract()) {
57+
continue;
58+
}
59+
if ($classReflection->isInterface()) {
60+
continue;
61+
}
62+
}
63+
4764
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
4865
->identifier(PHPUnitRuleIdentifier::NO_DOCUMENT_MOCKING)
4966
->build();

tests/Rules/Doctrine/NoDocumentMockingRule/NoDocumentMockingRuleTest.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
namespace Symplify\PHPStanRules\Tests\Rules\Doctrine\NoDocumentMockingRule;
66

7+
use Override;
78
use Iterator;
8-
use PHPStan\Reflection\ReflectionProvider;
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)