Skip to content

Commit 462d1a9

Browse files
authored
Detect Test Attribute in TestsNodeAnalyzer::isTestClassMethod (#486)
* detect Test Attribute in TestsNodeAnalyzer::isTestClassMethod * swap `array_any` to traditional foreach * add tests for AddTestsVoidReturnTypeWhereNoReturnRector * fix style * run rector * also check protected method while we're at it * add isTest tests with php attributes for existing rector rules * fix classname for test fixtures
1 parent 96b3565 commit 462d1a9

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SomeFixtureWithTestAttribute extends TestCase
8+
{
9+
/**
10+
* @testWith ["foo"]
11+
* ["bar"]
12+
*/
13+
#[\PHPUnit\Framework\Attributes\Test]
14+
public function shouldDoSomething(): void
15+
{
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector\Fixture;
24+
25+
use PHPUnit\Framework\TestCase;
26+
27+
final class SomeFixtureWithTestAttribute extends TestCase
28+
{
29+
#[\PHPUnit\Framework\Attributes\Test]
30+
#[\PHPUnit\Framework\Attributes\TestWith(['foo'])]
31+
#[\PHPUnit\Framework\Attributes\TestWith(['bar'])]
32+
public function shouldDoSomething(): void
33+
{
34+
}
35+
}
36+
37+
?>

rules-tests/PHPUnit60/Rector/ClassMethod/AddDoesNotPerformAssertionToNonAssertingTestRector/Fixture/test_in_annotation.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
44

5-
class TestInAnnotatoin extends \PHPUnit\Framework\TestCase
5+
class TestInAnnotation extends \PHPUnit\Framework\TestCase
66
{
77
/**
88
* @test
@@ -19,7 +19,7 @@ class TestInAnnotatoin extends \PHPUnit\Framework\TestCase
1919

2020
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
2121

22-
class TestInAnnotatoin extends \PHPUnit\Framework\TestCase
22+
class TestInAnnotation extends \PHPUnit\Framework\TestCase
2323
{
2424
/**
2525
* @test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
4+
5+
class TestInAnnotationWithTestAttribute extends \PHPUnit\Framework\TestCase
6+
{
7+
#[\PHPUnit\Framework\Attributes\Test]
8+
public function thisIsTest()
9+
{
10+
$nothing = 5;
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;
19+
20+
class TestInAnnotationWithTestAttribute extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* @doesNotPerformAssertions
24+
*/
25+
#[\PHPUnit\Framework\Attributes\Test]
26+
public function thisIsTest()
27+
{
28+
$nothing = 5;
29+
}
30+
}
31+
32+
?>

src/NodeAnalyzer/TestsNodeAnalyzer.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public function isTestClassMethod(ClassMethod $classMethod): bool
5858
return true;
5959
}
6060

61+
foreach ($classMethod->getAttrGroups() as $attributeGroup) {
62+
foreach ($attributeGroup->attrs as $attribute) {
63+
if ($attribute->name->toString() === 'PHPUnit\\Framework\\Attributes\\Test') {
64+
return true;
65+
}
66+
}
67+
}
68+
6169
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
6270
return $phpDocInfo->hasByName('test');
6371
}

0 commit comments

Comments
 (0)