Skip to content

Commit f0a8d28

Browse files
committed
[phpunit 12] Add PropertyCreateMockToCreateStubRector
1 parent 9f5f19d commit f0a8d28

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SomeTest extends TestCase
8+
{
9+
private \PHPUnit\Framework\MockObject\MockObject $someMock;
10+
11+
protected function setUp(): void
12+
{
13+
$this->someMock = $this->createMock(\stdClass::class);
14+
}
15+
16+
public function testThis()
17+
{
18+
$this->assertSame('...', $this->someMock);
19+
}
20+
21+
public function testThat()
22+
{
23+
$this->assertSame('...', $this->someMock);
24+
}
25+
}
26+
27+
?>
28+
-----
29+
<?php
30+
31+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector\Fixture;
32+
33+
use PHPUnit\Framework\TestCase;
34+
35+
final class SomeTest extends TestCase
36+
{
37+
private \PHPUnit\Framework\MockObject\Stub $someMock;
38+
39+
protected function setUp(): void
40+
{
41+
$this->someMock = $this->createStub(\stdClass::class);
42+
}
43+
44+
public function testThis()
45+
{
46+
$this->assertSame('...', $this->someMock);
47+
}
48+
49+
public function testThat()
50+
{
51+
$this->assertSame('...', $this->someMock);
52+
}
53+
}
54+
55+
56+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class PropertyCreateMockToCreateStubRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector\Source;
4+
5+
final class ClassWithDependency
6+
{
7+
public function __construct(
8+
private $dependency,
9+
) {
10+
}
11+
12+
public function getDependency()
13+
{
14+
return $this->dependency;
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\PHPUnit120\Rector\ClassMethod\ExpressionCreateMockToCreateStubRector;
7+
8+
return RectorConfig::configure()
9+
->withRules(rules: [ExpressionCreateMockToCreateStubRector::class]);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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\Stmt\Class_;
9+
use PHPStan\Reflection\ReflectionProvider;
10+
use Rector\Doctrine\NodeAnalyzer\AttributeFinder;
11+
use Rector\PhpParser\Node\BetterNodeFinder;
12+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
13+
use Rector\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
17+
/**
18+
* @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\PropertyCreateMockToCreateStubRector\PropertyCreateMockToCreateStubRectorTest
19+
*
20+
* @see https://github.com/sebastianbergmann/phpunit/commit/24c208d6a340c3071f28a9b5cce02b9377adfd43
21+
*/
22+
final class PropertyCreateMockToCreateStubRector extends AbstractRector
23+
{
24+
public function __construct(
25+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
26+
private readonly AttributeFinder $attributeFinder,
27+
private readonly ReflectionProvider $reflectionProvider,
28+
private readonly BetterNodeFinder $betterNodeFinder,
29+
) {
30+
}
31+
32+
public function getNodeTypes(): array
33+
{
34+
return [Class_::class];
35+
}
36+
37+
/**
38+
* @param Class_ $node
39+
*/
40+
public function refactor(Node $node): ?Class_
41+
{
42+
if ($this->shouldSkipClass($node)) {
43+
return null;
44+
}
45+
46+
return $node;
47+
}
48+
49+
public function getRuleDefinition(): RuleDefinition
50+
{
51+
return new RuleDefinition(
52+
'Change mock object property that is never mocked to createStub()',
53+
[
54+
new CodeSample(
55+
<<<'CODE_SAMPLE'
56+
use PHPUnit\Framework\TestCase;
57+
58+
final class SomeTest extends TestCase
59+
{
60+
private \PHPUnit\Framework\MockObject\MockObject $someServiceMock;
61+
62+
protected function setUp(): void
63+
{
64+
$this->someServiceMock = $this->createMock(SomeService::class);
65+
}
66+
67+
public function testOne(): void
68+
{
69+
$someObject = new SomeClass($this->someSevriceMock);
70+
}
71+
72+
public function testTwo(): void
73+
{
74+
$someObject = new AnotherClass($this->someSevriceMock);
75+
}
76+
}
77+
CODE_SAMPLE
78+
,
79+
<<<'CODE_SAMPLE'
80+
use PHPUnit\Framework\TestCase;
81+
82+
final class SomeTest extends TestCase
83+
{
84+
private \PHPUnit\Framework\MockObject\Stub\Stub $someServiceMock;
85+
86+
protected function setUp(): void
87+
{
88+
$this->someServiceMock = $this->createStub(SomeService::class);
89+
}
90+
91+
public function testOne(): void
92+
{
93+
$someObject = new SomeClass($this->someSevriceMock);
94+
}
95+
96+
public function testTwo(): void
97+
{
98+
$someObject = new AnotherClass($this->someSevriceMock);
99+
}
100+
}
101+
CODE_SAMPLE
102+
),
103+
]
104+
);
105+
}
106+
}

0 commit comments

Comments
 (0)