Skip to content

Commit

Permalink
Add AssertSameResponseCodeWithDebugContentsRector (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Oct 19, 2023
1 parent 873458c commit 5c5530a
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config/sets/symfony/symfony-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\Symfony\CodeQuality\Rector\ClassMethod\ParamTypeFromRouteRequiredRegexRector;
use Rector\Symfony\CodeQuality\Rector\ClassMethod\RemoveUnusedRequestParamRector;
use Rector\Symfony\CodeQuality\Rector\ClassMethod\ResponseReturnTypeControllerActionRector;
use Rector\Symfony\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector;
use Rector\Symfony\CodeQuality\Rector\MethodCall\LiteralGetToRequestClassConstantRector;

return static function (RectorConfig $rectorConfig): void {
Expand All @@ -26,5 +27,8 @@
ParamTypeFromRouteRequiredRegexRector::class,
ActionSuffixRemoverRector::class,
LoadValidatorMetadataToAnnotationRector::class,

// tests
AssertSameResponseCodeWithDebugContentsRector::class,
]);
};
26 changes: 25 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 82 Rules Overview
# 83 Rules Overview

## ActionSuffixRemoverRector

Expand Down Expand Up @@ -84,6 +84,30 @@ Replaces ArgumentValueResolverInterface by ValueResolverInterface

<br>

## AssertSameResponseCodeWithDebugContentsRector

Make assertSame(200, `$response->getStatusCode())` in tests comparing response code to include response contents for faster feedback

- class: [`Rector\Symfony\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector`](../rules/CodeQuality/Rector/MethodCall/AssertSameResponseCodeWithDebugContentsRector.php)

```diff
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());
+ $this->assertSame(200, $response->getStatusCode(), $response->getContents());
}
}
```

<br>

## AuthorizationCheckerIsGrantedExtractorRector

Change `$this->authorizationChecker->isGranted([$a, $b])` to `$this->authorizationChecker->isGranted($a) || $this->authorizationChecker->isGranted($b)`
Expand Down
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';
}
}
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');
}
}
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());
}
}

?>
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);
};
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;
}
}

0 comments on commit 5c5530a

Please sign in to comment.