Skip to content

Commit b9985d8

Browse files
authored
[CodeQuality] Skip TestCase suffix classes in RemoveNeverUsedMockPropertyRector (#709)
* [CodeQuality] Add BareCreateMockAssignToDirectUseRector to phpunit-mock-to-stub set * [CodeQuality] Skip TestCase suffix classes in RemoveNeverUsedMockPropertyRector Abstract *TestCase classes are most likely extended, with mock properties used in child classes. Skip them to avoid removing still-used mocks.
1 parent a741141 commit b9985d8

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveNeverUsedMockPropertyRector\Fixture;
4+
5+
use PHPUnit\Framework\MockObject\MockObject;
6+
use PHPUnit\Framework\TestCase;
7+
8+
abstract class SomeAbstractTestCase extends TestCase
9+
{
10+
private MockObject $mockProperty;
11+
12+
protected function setUp(): void
13+
{
14+
$this->mockProperty = $this->createMock(\stdClass::class);
15+
$this->mockProperty->expects($this->once())
16+
->method('someMethod')
17+
->willReturn('someValue');
18+
}
19+
}

rules/CodeQuality/Rector/Class_/RemoveNeverUsedMockPropertyRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public function refactor(Node $node): ?Node
9292
return null;
9393
}
9494

95+
// skip abstract test case classes, as most likely extended and mock used in child classes
96+
if (str_ends_with((string) $this->getName($node), 'TestCase')) {
97+
return null;
98+
}
99+
95100
$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
96101
if (! $setUpClassMethod instanceof ClassMethod) {
97102
return null;

0 commit comments

Comments
 (0)