Skip to content

Commit

Permalink
[CodeQuality] Add KernelBrowser support on LiteralGetToRequestClassCo…
Browse files Browse the repository at this point in the history
…nstantRector (#601)
  • Loading branch information
samsonasik committed May 12, 2024
1 parent 040014c commit 05297e1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\LiteralGetToRequestClassConstantRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class FromKernelBrowser extends WebTestCase
{
public function testSomething(): void
{
// This calls KernelTestCase::bootKernel(), and creates a
// "client" that is acting as the browser
$client = static::createClient();

// Request a specific page
$crawler = $client->request('GET', '/');

// Validate a successful response and some content
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Hello World');
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\LiteralGetToRequestClassConstantRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class FromKernelBrowser extends WebTestCase
{
public function testSomething(): void
{
// This calls KernelTestCase::bootKernel(), and creates a
// "client" that is acting as the browser
$client = static::createClient();

// Request a specific page
$crawler = $client->request(\Symfony\Component\HttpFoundation\Request::METHOD_GET, '/');

// Validate a successful response and some content
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Hello World');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ public function refactor(Node $node): ?Node
// in most cases that should be skipped, @changelog https://github.com/rectorphp/rector/issues/7135
if (
$this->reflectionProvider->hasClass('Symfony\Component\BrowserKit\AbstractBrowser') &&
$this->isObjectType($node->var, new ObjectType('Symfony\Component\HttpKernel\Client'))
(
$this->isObjectType($node->var, new ObjectType('Symfony\Component\HttpKernel\Client'))
||
$this->isObjectType($node->var, new ObjectType('Symfony\Bundle\FrameworkBundle\KernelBrowser'))
)
) {
return $this->refactorClientMethodCall($node);
}
Expand Down
13 changes: 13 additions & 0 deletions stubs/Symfony/Bundle/FrameworkBundle/KernelBrowser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Symfony\Bundle\FrameworkBundle;

if (class_exists('Symfony\Bundle\FrameworkBundle\KernelBrowser')) {
return;
}

final class KernelBrowser
{
}
10 changes: 9 additions & 1 deletion stubs/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@
}

use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;

abstract class WebTestCase extends KernelTestCase
{
/**
* @var Client
* @var Client|KernelBrowser
*/
protected static $client;

public static function getClient(): Client
{
return self::$client;
}

// support both Client and KernelBrowser transformation
// @see https://github.com/rectorphp/rector/issues/8613
public static function createClient(array $options = [], array $server = []): KernelBrowser
{
return self::$client;
}
}

0 comments on commit 05297e1

Please sign in to comment.