Symfony Panther: HttpFoundation Response object is not available when using WebDriver

716 views Asked by At

I just start testing in Symfony. And I am using Symfony/Panther to achieve testing. But I'm facing an error which is :

LogicException: HttpFoundation Response object is not available when using WebDriver.

Here is my test code:

<?php

namespace App\Tests\Controller;

use App\Repository\UserRepository;
use Liip\TestFixturesBundle\Services\DatabaseToolCollection;
use Symfony\Component\Panther\PantherTestCase;
use Symfony\Component\Panther\Client;

class UserManagmentControllerTest extends PantherTestCase
{

    /** @var AbstractDatabaseTool */
    protected $databaseTool;

    private ?Client $client = null;

    public function setUp(): void
    {
        parent::setUp();
        if (null === $this->client) {
            $this->client = static::createPantherClient();
        }
        $this->databaseTool = static::getContainer()->get(DatabaseToolCollection::class)->get();
    }
    
    public function testShouldLoadAllUsersList()
    {
        $userRepository = static::getContainer()->get(UserRepository::class);
    
        // retrieve the test user
        $testUser = $userRepository->findOneByEmail('[email protected]');
    
        // simulate $testUser being logged in
        // $this->client->loginUser($testUser->setRoles("[\"ROLE_ADMIN\"]"));
        $this->client->request('GET', '/admin/liste-des-utilisateurs');
        // $this->assertResponseIsSuccessful();
        // $this->client->getWebDriver()->findElement(WebDriverBy::name('rgpd'))->click();
        $this->assertResponseRedirects();
    }
    
    protected function tearDown(): void
    {
        parent::tearDown();
        $this->client->close();
        unset($this->databaseTool);
    }
}

In Panther documentation on github it is said that we can Use any PHPUnit assertion, including the ones provided by Symfony.

So why do I get this error ?

Thank you in advance

Php version: 8.1.7 Symfony version: 6.1.3 Panther version: 2.0

1

There are 1 answers

0
Jaime Díaz On

It's seems it is currently not possible if you use Panther. For simple HTTP requests they recommend to use WebTestCase. Something like this:

use App\Entity\Event;
use App\Repository\EventRepository;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class EventControllerTest extends WebTestCase
{
    private KernelBrowser $client;
    private EventRepository $repository;
    private string $path = '/tester/';

    protected function setUp(): void
    {
        $this->client = static::createClient();
    }

    public function testIndex(): void
    {
        $crawler = $this->client->request('GET', $this->path);

        self::assertResponseStatusCodeSame(200);
        self::assertPageTitleContains('Event index');

        // Use the $crawler to perform additional assertions e.g.
        // self::assertSame('Some text on the page', $crawler->filter('.p')->first());
    }