-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathPropertyCreateMockToCreateStubRector.php
More file actions
172 lines (143 loc) · 5.03 KB
/
PropertyCreateMockToCreateStubRector.php
File metadata and controls
172 lines (143 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\PHPUnit120\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\IntersectionType;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectExprDetector;
use Rector\PHPUnit\CodeQuality\NodeAnalyser\MockObjectPropertyDetector;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector\PropertyCreateMockToCreateStubRectorTest
*
* @see https://github.com/sebastianbergmann/phpunit/commit/24c208d6a340c3071f28a9b5cce02b9377adfd43
*/
final class PropertyCreateMockToCreateStubRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly MockObjectExprDetector $mockObjectExprDetector,
private readonly MockObjectPropertyDetector $mockObjectPropertyDetector,
) {
}
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
if ($this->shouldSkipClass($node)) {
return null;
}
/** @var ClassMethod $setUpClassMethod */
$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
$propertyNamesToCreateMockMethodCalls = $this->mockObjectPropertyDetector->collectFromClassMethod(
$setUpClassMethod
);
if ($propertyNamesToCreateMockMethodCalls === []) {
return null;
}
$hasChanged = false;
// find property fetch usage, is it exnted with method expectaitions?
foreach ($propertyNamesToCreateMockMethodCalls as $propertyName => $createMockMethodCall) {
if ($this->mockObjectExprDetector->isPropertyUsedForMocking($node, $propertyName)) {
continue;
}
$createMockMethodCall->name = new Identifier('createStub');
$hasChanged = true;
// update property type
$property = $node->getProperty($propertyName);
/** @var Property $property */
$property->type = $this->updatePropertyType($property->type);
}
if (! $hasChanged) {
return null;
}
return $node;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change mock object property that is never mocked to createStub()',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;
protected function setUp(): void
{
$this->someServiceMock = $this->createMock(SomeService::class);
}
public function testOne(): void
{
$someObject = new SomeClass($this->someServiceMock);
}
public function testTwo(): void
{
$someObject = new AnotherClass($this->someServiceMock);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
private \PHPUnit\Framework\MockObject\Stub\Stub $someServiceMock;
protected function setUp(): void
{
$this->someServiceMock = $this->createStub(SomeService::class);
}
public function testOne(): void
{
$someObject = new SomeClass($this->someServiceMock);
}
public function testTwo(): void
{
$someObject = new AnotherClass($this->someServiceMock);
}
}
CODE_SAMPLE
),
]
);
}
private function shouldSkipClass(Class_ $class): bool
{
if (! $this->testsNodeAnalyzer->isInTestClass($class)) {
return true;
}
$setUpClassMethod = $class->getMethod(MethodName::SET_UP);
// the setup class method must be here, so we have a place where the createMock() is used
return ! $setUpClassMethod instanceof ClassMethod;
}
private function updatePropertyType(?Node $node): IntersectionType|FullyQualified
{
if ($node instanceof IntersectionType) {
$newTypes = [];
foreach ($node->types as $innerType) {
if ($innerType instanceof FullyQualified && $innerType->toString() === PHPUnitClassName::MOCK_OBJECT) {
$newTypes[] = new FullyQualified(PHPUnitClassName::STUB);
} else {
$newTypes[] = $innerType;
}
}
return new IntersectionType($newTypes);
}
return new FullyQualified(PHPUnitClassName::STUB);
}
}