Skip to content

Commit c1e929f

Browse files
committed
[PHPUnit] Replace delited PHPUnit methods: assertClassHasStaticAttribute, classHasStaticAttribute and assertClassNotHasStaticAttribute by property_exists()
sebastianbergmann/phpunit#4601
1 parent 6b0e4f0 commit c1e929f

5 files changed

Lines changed: 195 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertPropertyExistsRector\Fixture;
4+
5+
final class MyTest2 extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test()
8+
{
9+
$response = new \Namespaced\Response();
10+
$this->assertFalse(property_exists($response, 'property'));
11+
}
12+
}
13+
14+
?>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MyPropertyExistsWithoutAssertRectorTest extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertClassHasStaticAttribute('property', 'stdClass');
12+
$this->assertClassHasStaticAttribute('property', stdClass::class);
13+
$this->classHasStaticAttribute('property', stdClass::class, 'message');
14+
$this->assertClassNotHasStaticAttribute('property', 'Namespaced\stdClass', 'message');
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector\Fixture;
23+
24+
use PHPUnit\Framework\TestCase;
25+
26+
final class MyPropertyExistsWithoutAssertRectorTest extends TestCase
27+
{
28+
public function test()
29+
{
30+
$this->assertTrue(property_exists('stdClass', 'property'));
31+
$this->assertTrue(property_exists(stdClass::class, 'property'));
32+
$this->assertTrue(property_exists(stdClass::class, 'property'), 'message');
33+
$this->assertFalse(property_exists('Namespaced\stdClass', 'property'), 'message');
34+
}
35+
}
36+
37+
?>
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\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class PropertyExistsWithoutAssertRectorTest 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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(PropertyExistsWithoutAssertRector::class);
10+
};
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\PHPUnit100\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\FuncCall;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Scalar\String_;
11+
use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator;
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+
* @url https://github.com/sebastianbergmann/phpunit/issues/4601
19+
*
20+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\PropertyExistsWithoutAssertRector\PropertyExistsWithoutAssertRectorTest
21+
*/
22+
final class PropertyExistsWithoutAssertRector extends AbstractRector
23+
{
24+
25+
/**
26+
* @var array<string, string>
27+
*/
28+
private const RENAME_METHODS_WITH_OBJECT_MAP = [
29+
'assertClassHasStaticAttribute' => 'assertTrue',
30+
'classHasStaticAttribute' => 'assertTrue',
31+
'assertClassNotHasStaticAttribute' => 'assertFalse',
32+
];
33+
34+
public function __construct(
35+
private readonly IdentifierManipulator $identifierManipulator,
36+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
37+
) {
38+
}
39+
40+
public function getRuleDefinition(): RuleDefinition
41+
{
42+
return new RuleDefinition(
43+
'Replace delited PHPUnit methods: assertClassHasStaticAttribute, classHasStaticAttribute and assertClassNotHasStaticAttribute by property_exists()',
44+
[
45+
new CodeSample(
46+
<<<'CODE_SAMPLE'
47+
$this->assertClassHasStaticAttribute("Class", "property");
48+
$this->classHasStaticAttribute("Class", "property");
49+
$this->assertClassNotHasStaticAttribute("Class", "property");
50+
CODE_SAMPLE
51+
,
52+
<<<'CODE_SAMPLE'
53+
$this->assertTrue(property_exists("Class", "property"));
54+
$this->assertTrue(property_exists("Class", "property"));
55+
$this->assertFalse(property_exists("Class", "property"));
56+
CODE_SAMPLE
57+
),
58+
]
59+
);
60+
}
61+
62+
/**
63+
* @return array<class-string<Node>>
64+
*/
65+
public function getNodeTypes(): array
66+
{
67+
return [MethodCall::class];
68+
}
69+
70+
/**
71+
* @param MethodCall $node
72+
*/
73+
public function refactor(Node $node): ?Node
74+
{
75+
$map = self::RENAME_METHODS_WITH_OBJECT_MAP;
76+
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, array_keys($map))) {
77+
return null;
78+
}
79+
80+
if ($node->isFirstClassCallable()) {
81+
return null;
82+
}
83+
84+
$firstNode = new Node\Arg($node->getArgs()[0]->value);
85+
86+
if ($node->getArgs()[1]->value instanceof Node\Expr\ClassConstFetch) {
87+
$secondNode = $node->getArgs()[1];
88+
} else {
89+
$secondNode = new Node\Arg($node->getArgs()[1]->value);
90+
}
91+
92+
$propertyExists = new FuncCall(new Node\Name('property_exists'), [
93+
$secondNode,
94+
$firstNode,
95+
]);
96+
97+
$newArgs = $this->nodeFactory->createArgs([$propertyExists]);
98+
99+
unset($node->args[0], $node->args[1]);
100+
$node->args = array_merge($newArgs, $node->getArgs());
101+
102+
$this->identifierManipulator->renameNodeWithMap($node, $map);
103+
104+
return $node;
105+
}
106+
}

0 commit comments

Comments
 (0)