panther icon indicating copy to clipboard operation
panther copied to clipboard

Error in a `PantherTestCase` when using assertion from `BrowserKitAssertionsTrait` like `assertResponseIsSuccessful`

Open gcollombet opened this issue 2 years ago • 2 comments

In a PantherTestCase

When we use an assertion from the BrowserKitAssertionsTrait, we get a TypeError :

TypeError : Return value of Symfony\Component\Panther\PantherTestCase::getResponse() must be an instance of Symfony\Component\HttpFoundation\Response, instance of Symfony\Component\BrowserKit\Response returned

 /var/www/html/vendor/symfony/framework-bundle/Test/BrowserKitAssertionsTrait.php:148
 /var/www/html/vendor/symfony/framework-bundle/Test/BrowserKitAssertionsTrait.php:36

For exemple I have that error with that two function.

    $this->assertResponseStatusCodeSame(302);
    $this->assertResponseIsSuccessful();

I use BrowserKit (createHttpBrowserClient) as client.

gcollombet avatar Sep 01 '21 10:09 gcollombet

We should return a better error message, but it's not possible to use these assertions with Panther because WebDriver doesn't give access to the HTTP headers.

dunglas avatar Sep 01 '21 11:09 dunglas

Thank you for the fast answer. Is that also the case when we use the HttpBrowserClient ?

Because the following expression work when client is a HttpBrowserClient (created with createHttpBrowserClient() method) :

$this->assertEquals(200, $client->getResponse()->getStatusCode(), $client->getResponse()->getContent());

If some methods of the trait BrowserKitAssertionsTrait should never be used regardless of the client in a PantherTestCase, maybe that methods should be made private when used in trait src/WebTestAssertionsTrait.php ?

  use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait as BaseWebTestAssertionsTrait;
    use BaseWebTestAssertionsTrait {
        assertPageTitleSame as private baseAssertPageTitleSame;
        assertPageTitleContains as private baseAssertPageTitleContains;
    }

gcollombet avatar Sep 01 '21 12:09 gcollombet