Skip to content

Commit 9044f2e

Browse files
committed
[PHPUnit60] Skip tests in classes with asserting parent lifecycle methods in AddDoesNotPerformAssertionToNonAssertingTestRector
1 parent 6d45a51 commit 9044f2e

11 files changed

Lines changed: 340 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithAssertingTearDown;
6+
7+
class OverrideAssertingParentTearDown extends AbstractClassWithAssertingTearDown
8+
{
9+
protected function tearDown(): void
10+
{
11+
$this->value = null;
12+
}
13+
14+
public function testSomething()
15+
{
16+
$value = 5;
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
25+
26+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithAssertingTearDown;
27+
28+
class OverrideAssertingParentTearDown extends AbstractClassWithAssertingTearDown
29+
{
30+
protected function tearDown(): void
31+
{
32+
$this->value = null;
33+
}
34+
35+
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
36+
public function testSomething()
37+
{
38+
$value = 5;
39+
}
40+
}
41+
42+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class Deep0 extends TestCase { protected function setUp(): void { $this->assertTrue(true); } }
8+
abstract class Deep1 extends Deep0 { protected function setUp(): void { parent::setUp(); } }
9+
abstract class Deep2 extends Deep1 { protected function setUp(): void { parent::setUp(); } }
10+
11+
final class DeepTest extends Deep2
12+
{
13+
public function testSomething()
14+
{
15+
$value = 1;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class DeepC0 extends TestCase { protected function setUp(): void { $this->assertTrue(true); } }
8+
abstract class DeepC1 extends DeepC0 { protected function setUp(): void { parent::setUp(); } }
9+
abstract class DeepC2 extends DeepC1 { protected function setUp(): void { parent::setUp(); } }
10+
11+
final class DeepCTest extends DeepC2
12+
{
13+
public function testSomething()
14+
{
15+
$value = 1;
16+
}
17+
}
18+
19+
final class DeepCShallowTest extends DeepC1
20+
{
21+
public function testOther()
22+
{
23+
$other = 2;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithTraitAssertingSetUp;
6+
7+
class SkipAssertingSetUpViaParentTrait extends AbstractClassWithTraitAssertingSetUp
8+
{
9+
public function testSomething()
10+
{
11+
$value = 5;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithAssertingTearDown;
6+
7+
class SkipAssertingTearDownInParent extends AbstractClassWithAssertingTearDown
8+
{
9+
public function testSomething()
10+
{
11+
$value = 5;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
abstract class AbstractClassWithAssertingTearDown extends TestCase
10+
{
11+
protected function tearDown(): void
12+
{
13+
$this->assertTrue(true);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
abstract class AbstractClassWithTraitAssertingSetUp extends TestCase
10+
{
11+
use AssertingHelperTrait;
12+
13+
protected function setUp(): void
14+
{
15+
$this->verifyReady();
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source;
6+
7+
trait AssertingHelperTrait
8+
{
9+
protected function verifyReady(): void
10+
{
11+
$this->assertTrue(true);
12+
}
13+
}

‎rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function refactor(Node $node): ?Node
107107
// find array in data provider - must contain a return node
108108
/** @var Return_[] $returns */
109109
$returns = $this->betterNodeFinder->findInstanceOf((array) $node->stmts, Return_::class);
110-
$hasChanged = \false;
110+
$hasChanged = false;
111111
foreach ($returns as $return) {
112112
if (! $return->expr instanceof Array_) {
113113
continue;
@@ -123,10 +123,10 @@ public function refactor(Node $node): ?Node
123123
}
124124

125125
// ensure newlined printed
126-
$array->setAttribute(AttributeKey::NEWLINED_ARRAY_PRINT, \true);
126+
$array->setAttribute(AttributeKey::NEWLINED_ARRAY_PRINT, true);
127127
// invoke reprint
128128
$array->setAttribute(AttributeKey::ORIGINAL_NODE, null);
129-
$hasChanged = \true;
129+
$hasChanged = true;
130130
}
131131

132132
if ($hasChanged) {

‎rules/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function refactor(Node $node): ?Node
128128
}
129129

130130
// assertions in lifecycle methods count toward every test of the class
131-
if ($this->lifecycleMethodAssertAnalyzer->hasAssertingLifecycleMethod($node)) {
131+
if ($this->lifecycleMethodAssertAnalyzer->hasAssertingLifecycleMethod($node, $scope)) {
132132
return null;
133133
}
134134

0 commit comments

Comments
 (0)