Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@
"Rector\\Legacy\\": "packages/Legacy/src",
"Rector\\ElasticSearchDSL\\": "packages/ElasticSearchDSL/src",
"Rector\\SymfonyPHPUnit\\": "packages/SymfonyPHPUnit/src",
"Rector\\Architecture\\": "packages/Architecture/src"
"Rector\\Architecture\\": "packages/Architecture/src",
"Rector\\PHPUnitSymfony\\": "packages/PHPUnitSymfony/src"
}
},
"autoload-dev": {
Expand Down Expand Up @@ -116,7 +117,8 @@
"Rector\\Legacy\\Tests\\": "packages/Legacy/tests",
"Rector\\ElasticSearchDSL\\Tests\\": "packages/ElasticSearchDSL/tests",
"Rector\\SymfonyPHPUnit\\Tests\\": "packages/SymfonyPHPUnit/tests",
"Rector\\Architecture\\Tests\\": "packages/Architecture/tests"
"Rector\\Architecture\\Tests\\": "packages/Architecture/tests",
"Rector\\PHPUnitSymfony\\Tests\\": "packages/PHPUnitSymfony/tests"
},
"classmap": [
"packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function resolve(Node $nameNode): array
return [$fullyQualifiedName];
}

private function resolveFullyQualifiedName(Node $nameNode, string $name): ?string
private function resolveFullyQualifiedName(Node $nameNode, string $name): string
{
if (in_array($name, ['self', 'static', 'this'], true)) {
/** @var string|null $class */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php declare(strict_types=1);

namespace Rector\PHPUnitSymfony\Rector\StaticCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

/**
* @see \Rector\PHPUnitSymfony\Tests\Rector\StaticCall\AddMessageToEqualsResponseCodeRector\AddMessageToEqualsResponseCodeRectorTest
*/
final class AddMessageToEqualsResponseCodeRector extends AbstractRector
{
/**
* @var string
*/
private const RESPONSE_CLASS = 'Symfony\Component\HttpFoundation\Response';

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Add response content to response code assert, so it is easier to debug', [
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;

final class SomeClassTest extends TestCase
{
public function test(Response $response)
{
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$response->getStatusCode()
);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;

final class SomeClassTest extends TestCase
{
public function test(Response $response)
{
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$response->getStatusCode()
$response->getContent()
);
}
}
CODE_SAMPLE
),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [StaticCall::class, MethodCall::class];
}

/**
* @param StaticCall|MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->name, 'assertEquals')) {
return null;
}

// already has 3rd "message" argument
if (isset($node->args[2])) {
return null;
}

if (! $this->isHttpRequestArgument($node->args[0]->value)) {
return null;
}

$parentVariable = $this->getParentOfGetStatusCode($node->args[1]->value);
if ($parentVariable === null) {
return null;
}

$getContentMethodCall = new MethodCall($parentVariable, 'getContent');

$node->args[2] = new Arg($getContentMethodCall);

return $node;
}

/**
* $this->assertX(Response::SOME_STATUS)
*/
private function isHttpRequestArgument(Node $node): bool
{
if (! $node instanceof ClassConstFetch) {
return false;
}

return $this->isType($node->class, self::RESPONSE_CLASS);
}

/**
* @return Variable|MethodCall|Expr|null
*/
private function getParentOfGetStatusCode(Node $node): ?Node
{
$currentNode = $node;
while ($currentNode instanceof MethodCall) {
if ($this->isName($currentNode->name, 'getStatusCode')) {
return $currentNode->var;
}

$currentNode = $currentNode->var;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace Rector\PHPUnitSymfony\Tests\Rector\StaticCall\AddMessageToEqualsResponseCodeRector;

use Rector\PHPUnitSymfony\Rector\StaticCall\AddMessageToEqualsResponseCodeRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddMessageToEqualsResponseCodeRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/method_call.php.inc']);
}

protected function getRectorClass(): string
{
return AddMessageToEqualsResponseCodeRector::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Rector\PHPUnitSymfony\Tests\Rector\StaticCall\AddMessageToEqualsResponseCodeRector\Fixture;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;

final class SomeClassTest extends TestCase
{
public function test(Response $response)
{
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$response->getStatusCode()
);

$anotherName = new Response();
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$anotherName->getStatusCode()
);
}
}

?>
-----
<?php

namespace Rector\PHPUnitSymfony\Tests\Rector\StaticCall\AddMessageToEqualsResponseCodeRector\Fixture;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;

final class SomeClassTest extends TestCase
{
public function test(Response $response)
{
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$response->getStatusCode(),
$response->getContent()
);

$anotherName = new Response();
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$anotherName->getStatusCode(),
$anotherName->getContent()
);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rector\PHPUnitSymfony\Tests\Rector\StaticCall\AddMessageToEqualsResponseCodeRector\Fixture;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;

final class MethodCallTest extends TestCase
{
public function test()
{
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$this->getResponse()->getStatusCode()
);
}

private function getResponse(): Response
{
}
}

?>
-----
<?php

namespace Rector\PHPUnitSymfony\Tests\Rector\StaticCall\AddMessageToEqualsResponseCodeRector\Fixture;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;

final class MethodCallTest extends TestCase
{
public function test()
{
$this->assertEquals(
Response::HTTP_NO_CONTENT,
$this->getResponse()->getStatusCode(),
$this->getResponse()->getContent()
);
}

private function getResponse(): Response
{
}
}

?>