|
| 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; |
| 11 | +use PhpParser\Node\Name\FullyQualified; |
| 12 | +use PhpParser\Node\Stmt\Class_; |
| 13 | +use PhpParser\Node\Stmt\ClassMethod; |
| 14 | +use PHPStan\Reflection\ReflectionProvider; |
| 15 | +use Rector\Doctrine\NodeAnalyzer\AttributeFinder; |
| 16 | +use Rector\PHPUnit\Enum\PHPUnitAttribute; |
| 17 | +use Rector\PHPUnit\Enum\PHPUnitClassName; |
| 18 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 19 | +use Rector\Rector\AbstractRector; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 22 | + |
| 23 | +/** |
| 24 | + * @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWithoutExpectationsAttributeRector\AllowMockObjectsWithoutExpectationsAttributeRectorTest |
| 25 | + * |
| 26 | + * @see https://github.com/sebastianbergmann/phpunit/commit/24c208d6a340c3071f28a9b5cce02b9377adfd43 |
| 27 | + */ |
| 28 | +final class AllowMockObjectsWithoutExpectationsAttributeRector extends AbstractRector |
| 29 | +{ |
| 30 | + public function __construct( |
| 31 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 32 | + private readonly AttributeFinder $attributeFinder, |
| 33 | + private readonly ReflectionProvider $reflectionProvider |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + public function getNodeTypes(): array |
| 38 | + { |
| 39 | + return [Class_::class]; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param Class_ $node |
| 44 | + */ |
| 45 | + public function refactor(Node $node): ?Class_ |
| 46 | + { |
| 47 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + // attribute must exist for the rule to work |
| 52 | + if (! $this->reflectionProvider->hasClass(PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS)) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + // already filled |
| 57 | + if ($this->attributeFinder->hasAttributeByClasses( |
| 58 | + $node, |
| 59 | + [PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS] |
| 60 | + )) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + // has mock objects properties and setUp() method? |
| 65 | +
|
| 66 | + if (! $node->getMethod('setUp') instanceof ClassMethod) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + if (! $this->hasMockObjectProperty($node)) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + // @todo add the attribute if has more than 1 public test* method |
| 75 | + $testMethodCount = 0; |
| 76 | + |
| 77 | + foreach ($node->getMethods() as $classMethod) { |
| 78 | + if ($this->testsNodeAnalyzer->isTestClassMethod($classMethod)) { |
| 79 | + ++$testMethodCount; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if ($testMethodCount < 2) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + // add attribute |
| 88 | + $node->attrGroups[] = new AttributeGroup([ |
| 89 | + new Attribute(new FullyQualified(PHPUnitAttribute::ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS)), |
| 90 | + ]); |
| 91 | + |
| 92 | + return $node; |
| 93 | + } |
| 94 | + |
| 95 | + public function getRuleDefinition(): RuleDefinition |
| 96 | + { |
| 97 | + return new RuleDefinition( |
| 98 | + 'Add #[AllowMockObjectsWithoutExpectations] attribute to PHPUnit test classes with mock properties used in multiple methods', |
| 99 | + [ |
| 100 | + new CodeSample( |
| 101 | + <<<'CODE_SAMPLE' |
| 102 | +use PHPUnit\Framework\TestCase; |
| 103 | +final class SomeTest extends TestCase |
| 104 | +{ |
| 105 | + private \PHPUnit\Framework\MockObject\MockObject $someServiceMock; |
| 106 | +
|
| 107 | + protected function setUp(): void |
| 108 | + { |
| 109 | + $this->someServiceMock = $this->createMock(SomeService::class); |
| 110 | + } |
| 111 | +
|
| 112 | + public function testOne(): void |
| 113 | + { |
| 114 | + // use $this->someServiceMock |
| 115 | + } |
| 116 | +
|
| 117 | + public function testTwo(): void |
| 118 | + { |
| 119 | + // use $this->someServiceMock |
| 120 | + } |
| 121 | +} |
| 122 | +CODE_SAMPLE |
| 123 | + , |
| 124 | + <<<'CODE_SAMPLE' |
| 125 | +use PHPUnit\Framework\TestCase; |
| 126 | +use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations; |
| 127 | +
|
| 128 | +#[AllowMockObjectsWithoutExpectations] |
| 129 | +final class SomeTest extends TestCase |
| 130 | +{ |
| 131 | + private \PHPUnit\Framework\MockObject\MockObject $someServiceMock; |
| 132 | +
|
| 133 | + protected function setUp(): void |
| 134 | + { |
| 135 | + $this->someServiceMock = $this->createMock(SomeService::class); |
| 136 | + } |
| 137 | +
|
| 138 | + public function testOne(): void |
| 139 | + { |
| 140 | + // use $this->someServiceMock |
| 141 | + } |
| 142 | +
|
| 143 | + public function testTwo(): void |
| 144 | + { |
| 145 | + // use $this->someServiceMock |
| 146 | + } |
| 147 | +} |
| 148 | +CODE_SAMPLE |
| 149 | + ), |
| 150 | + |
| 151 | + ] |
| 152 | + ); |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + private function hasMockObjectProperty(Class_ $class): bool |
| 157 | + { |
| 158 | + foreach ($class->getProperties() as $property) { |
| 159 | + if (! $property->type instanceof Name) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + |
| 163 | + if ($this->isName($property->type, PHPUnitClassName::MOCK_OBJECT)) { |
| 164 | + return true; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return false; |
| 169 | + } |
| 170 | +} |
0 commit comments