-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AssertSameResponseCodeWithDebugContentsRector (#536)
- Loading branch information
1 parent
873458c
commit 5c5530a
Showing
7 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ResponseCodeWithDebugContentsRector/AssertSameResponseCodeWithDebugContentsRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AssertSameResponseCodeWithDebugContentsRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...hodCall/AssertSameResponseCodeWithDebugContentsRector/Fixture/skip_custom_message.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SkipCustomMessage extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
/** @var \Symfony\Component\HttpFoundation\Response $response */ | ||
$response = $this->processResult(); | ||
|
||
$this->assertSame(200, $response->getStatusCode(), 'custom message'); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...ector/MethodCall/AssertSameResponseCodeWithDebugContentsRector/Fixture/some_class.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeClass extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
/** @var \Symfony\Component\HttpFoundation\Response $response */ | ||
$response = $this->processResult(); | ||
|
||
$this->assertSame(200, $response->getStatusCode()); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeClass extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
/** @var \Symfony\Component\HttpFoundation\Response $response */ | ||
$response = $this->processResult(); | ||
|
||
$this->assertSame(200, $response->getStatusCode(), $response->getContents()); | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...ector/MethodCall/AssertSameResponseCodeWithDebugContentsRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
use Rector\Symfony\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(AssertSameResponseCodeWithDebugContentsRector::class); | ||
}; |
134 changes: 134 additions & 0 deletions
134
rules/CodeQuality/Rector/MethodCall/AssertSameResponseCodeWithDebugContentsRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Symfony\CodeQuality\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector\AssertSameResponseCodeWithDebugContentsRectorTest | ||
*/ | ||
final class AssertSameResponseCodeWithDebugContentsRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Make assertSame(200, $response->getStatusCode()) in tests comparing response code to include response contents for faster feedback', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
class SomeClass extends TestCase | ||
{ | ||
public function run() | ||
{ | ||
/** @var \Symfony\Component\HttpFoundation\Response $response */ | ||
$response = $this->processResult(); | ||
$this->assertSame(200, $response->getStatusCode()); | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
class SomeClass extends TestCase | ||
{ | ||
public function run() | ||
{ | ||
/** @var \Symfony\Component\HttpFoundation\Response $response */ | ||
$response = $this->processResult(); | ||
$this->assertSame(200, $response->getStatusCode(), $response->getContents()); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
|
||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->testsNodeAnalyzer->isInTestClass($node)) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->name, 'assertSame')) { | ||
return null; | ||
} | ||
|
||
// there cannot be any custom message | ||
$args = $node->getArgs(); | ||
if (count($args) !== 2) { | ||
return null; | ||
} | ||
|
||
$firstArg = $args[0]; | ||
$comparedValueType = $this->getType($firstArg->value); | ||
|
||
// must be number | ||
if (! $comparedValueType instanceof ConstantIntegerType) { | ||
return null; | ||
} | ||
|
||
$responseExpr = $this->matchResponseExpr($args[1]->value); | ||
if (! $responseExpr instanceof Expr) { | ||
return null; | ||
} | ||
|
||
$getContentsMethodCall = new MethodCall($responseExpr, 'getContents'); | ||
$node->args[2] = new Arg($getContentsMethodCall); | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* We look for $response->getStatusCode() | ||
* $client->getResponse()->getStatusCode() | ||
* | ||
* etc. | ||
*/ | ||
private function matchResponseExpr(Expr $expr): ?Expr | ||
{ | ||
if (! $expr instanceof MethodCall) { | ||
return null; | ||
} | ||
|
||
// must be status method call | ||
if (! $this->isName($expr->name, 'getStatusCode')) { | ||
return null; | ||
} | ||
|
||
return $expr->var; | ||
} | ||
} |