-
-
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.
[phpunit] Add NoMockObjectAndRealObjectPropertyRule (#170)
- Loading branch information
1 parent
f1fc3d2
commit 97178a0
Showing
10 changed files
with
162 additions
and
0 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
60 changes: 60 additions & 0 deletions
60
src/Rules/PHPUnit/NoMockObjectAndRealObjectPropertyRule.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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Rules\PHPUnit; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\IntersectionType; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Property; | ||
use PhpParser\Node\UnionType; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symplify\PHPStanRules\Enum\PHPUnitRuleIdentifier; | ||
|
||
/** | ||
* @implements Rule<Property> | ||
*/ | ||
final class NoMockObjectAndRealObjectPropertyRule implements Rule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const ERROR_MESSAGE = 'Instead of ambiguous mock + object mix, pick single type that is more relevant'; | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Property::class; | ||
} | ||
|
||
/** | ||
* @param Property $node | ||
* @return RuleError[] | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (! $node->type instanceof IntersectionType && ! $node->type instanceof UnionType) { | ||
return []; | ||
} | ||
|
||
foreach ($node->type->types as $type) { | ||
if (! $type instanceof Name) { | ||
continue; | ||
} | ||
|
||
if ($type->toString() !== MockObject::class) { | ||
continue; | ||
} | ||
|
||
return [RuleErrorBuilder::message(self::ERROR_MESSAGE) | ||
->identifier(PHPUnitRuleIdentifier::NO_MOCK_OBJECT_AND_REAL_OBJECT_PROPERTY) | ||
->build()]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...es/PHPUnit/NoMockObjectAndRealObjectPropertyRule/Fixture/IntersectionMockedProperties.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,12 @@ | ||
<?php | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule\Source\SomeObject; | ||
|
||
final class IntersectionMockedProperties | ||
{ | ||
private MockObject&SomeObject $someObject; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
tests/Rules/PHPUnit/NoMockObjectAndRealObjectPropertyRule/Fixture/SkipNullableObject.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,11 @@ | ||
<?php | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule\Source\SomeObject; | ||
|
||
final class SkipNullableObject | ||
{ | ||
private ?MockObject $someObject; | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/Rules/PHPUnit/NoMockObjectAndRealObjectPropertyRule/Fixture/SkipOneOrTheOther.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,13 @@ | ||
<?php | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule\Source\SomeObject; | ||
|
||
final class SkipOneOrTheOther | ||
{ | ||
private SomeObject $someObject; | ||
|
||
private MockObject $anotherMock; | ||
} |
12 changes: 12 additions & 0 deletions
12
...es/PHPUnit/NoMockObjectAndRealObjectPropertyRule/Fixture/SomeTestWithMockedProperties.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,12 @@ | ||
<?php | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule\Fixture; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule\Source\SomeObject; | ||
|
||
final class SomeTestWithMockedProperties | ||
{ | ||
private MockObject|SomeObject $someObject; | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...PUnit/NoMockObjectAndRealObjectPropertyRule/NoMockObjectAndRealObjectPropertyRuleTest.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,32 @@ | ||
<?php | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule; | ||
|
||
use Iterator; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Symplify\PHPStanRules\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule; | ||
|
||
final class NoMockObjectAndRealObjectPropertyRuleTest extends RuleTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function testRule(string $filePath, array $expectedErrorsWithLines): void | ||
{ | ||
$this->analyse([$filePath], $expectedErrorsWithLines); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
yield [__DIR__ . '/Fixture/SomeTestWithMockedProperties.php', [[NoMockObjectAndRealObjectPropertyRule::ERROR_MESSAGE, 10]]]; | ||
yield [__DIR__ . '/Fixture/IntersectionMockedProperties.php', [[NoMockObjectAndRealObjectPropertyRule::ERROR_MESSAGE, 10]]]; | ||
|
||
yield [__DIR__ . '/Fixture/SkipOneOrTheOther.php', []]; | ||
yield [__DIR__ . '/Fixture/SkipNullableObject.php', []]; | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new NoMockObjectAndRealObjectPropertyRule(); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/Rules/PHPUnit/NoMockObjectAndRealObjectPropertyRule/Source/SomeObject.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\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule\Source; | ||
|
||
final class SomeObject | ||
{ | ||
} |
2 changes: 2 additions & 0 deletions
2
tests/Rules/PHPUnit/NoMockObjectAndRealObjectPropertyRule/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,2 @@ | ||
rules: | ||
- Symplify\PHPStanRules\Rules\PHPUnit\NoMockObjectAndRealObjectPropertyRule |