|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\PHPUnit120\Rector\Class_; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Attribute; |
| 9 | +use PhpParser\Node\AttributeGroup; |
| 10 | +use PhpParser\Node\Name\FullyQualified; |
| 11 | +use PhpParser\Node\Stmt\Class_; |
| 12 | +use PHPStan\Reflection\ReflectionProvider; |
| 13 | +use Rector\Doctrine\NodeAnalyzer\AttributeFinder; |
| 14 | +use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectExprDetector; |
| 15 | +use Rector\PHPUnit\Enum\PHPUnitAttribute; |
| 16 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 17 | +use Rector\Rector\AbstractRector; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 20 | + |
| 21 | +/** |
| 22 | + * @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsForDataProviderRector\AllowMockObjectsForDataProviderRectorTest |
| 23 | + */ |
| 24 | +final class AllowMockObjectsForDataProviderRector extends AbstractRector |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 28 | + private readonly AttributeFinder $attributeFinder, |
| 29 | + private readonly ReflectionProvider $reflectionProvider, |
| 30 | + private readonly MockObjectExprDetector $mockObjectExprDetector, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function getNodeTypes(): array |
| 35 | + { |
| 36 | + return [Class_::class]; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param Class_ $node |
| 41 | + */ |
| 42 | + public function refactor(Node $node): ?Class_ |
| 43 | + { |
| 44 | + if ($this->shouldSkipClass($node)) { |
| 45 | + return null; |
| 46 | + } |
| 47 | + |
| 48 | + if (! $this->hasDataProviderMethodWithExpectLessMethodMock($node)) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + // add attribute |
| 53 | + $node->attrGroups[] = new AttributeGroup([ |
| 54 | + new Attribute(new FullyQualified(PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS)), |
| 55 | + ]); |
| 56 | + |
| 57 | + return $node; |
| 58 | + } |
| 59 | + |
| 60 | + public function getRuleDefinition(): RuleDefinition |
| 61 | + { |
| 62 | + return new RuleDefinition( |
| 63 | + 'Add #[AllowMockObjectsWithoutExpectations] attribute to PHPUnit test classes that have methods with data providers and mock objects without expectations', |
| 64 | + [ |
| 65 | + new CodeSample( |
| 66 | + <<<'CODE_SAMPLE' |
| 67 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 68 | +use PHPUnit\Framework\TestCase; |
| 69 | +
|
| 70 | +final class SomeClass extends TestCase |
| 71 | +{ |
| 72 | + #[DataProvider('someDataProvider')] |
| 73 | + public function test() |
| 74 | + { |
| 75 | + $someMock = $this->createMock(\stdClass::class); |
| 76 | + $someMock->expects('method')->willReturn(true); |
| 77 | + } |
| 78 | +
|
| 79 | + public static function someDataProvider(): iterable |
| 80 | + { |
| 81 | + yield [1]; |
| 82 | + yield [2]; |
| 83 | + yield [3]; |
| 84 | + } |
| 85 | +} |
| 86 | +CODE_SAMPLE |
| 87 | + , |
| 88 | + <<<'CODE_SAMPLE' |
| 89 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 90 | +use PHPUnit\Framework\TestCase; |
| 91 | +
|
| 92 | +#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations] |
| 93 | +final class SomeClass extends TestCase |
| 94 | +{ |
| 95 | + #[DataProvider('someDataProvider')] |
| 96 | + public function test() |
| 97 | + { |
| 98 | + $someMock = $this->createMock(\stdClass::class); |
| 99 | + $someMock->expects('method')->willReturn(true); |
| 100 | + } |
| 101 | +
|
| 102 | + public static function someDataProvider(): iterable |
| 103 | + { |
| 104 | + yield [1]; |
| 105 | + yield [2]; |
| 106 | + yield [3]; |
| 107 | + } |
| 108 | +} |
| 109 | +CODE_SAMPLE |
| 110 | + ), |
| 111 | + ] |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + private function shouldSkipClass(Class_ $class): bool |
| 116 | + { |
| 117 | + if (! $this->testsNodeAnalyzer->isInTestClass($class)) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + // attribute must exist for the rule to work |
| 122 | + if (! $this->reflectionProvider->hasClass(PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS)) { |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + // already filled |
| 127 | + return $this->attributeFinder->hasAttributeByClasses( |
| 128 | + $class, |
| 129 | + [PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS] |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + private function hasDataProviderMethodWithExpectLessMethodMock(Class_ $class): bool |
| 134 | + { |
| 135 | + // has a test method, that contains a ->method() call without expects() |
| 136 | + // and has a data provider attribute? |
| 137 | + foreach ($class->getMethods() as $classMethod) { |
| 138 | + if (! $this->testsNodeAnalyzer->isTestClassMethod($classMethod)) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + if (! $this->attributeFinder->hasAttributeByClasses($classMethod, [PHPUnitAttribute::DATA_PROVIDER])) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + // has expects-less mock objects |
| 147 | + if ($this->mockObjectExprDetector->hasMethodCallWithoutExpects($classMethod)) { |
| 148 | + return true; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return false; |
| 153 | + } |
| 154 | +} |
0 commit comments