assertResponseRedirects method with PHPUnit by passing the URI as a parameter

Viewed 554

Can someone explain to me why when I use the assertResponseRedirects method with PHPUnit by passing the URI as a parameter:

class UserControllerTest extends WebTestCase
{
    public function testUnauthenticatedIsRedirected(): void
    {
        $this->client->request('GET', '/account');
        static::assertResponseRedirects('/login');
    }
}

I have this error:

Failed asserting that Symfony\Component\HttpFoundation\RedirectResponse Object &0000000044abc158000000000cda95f8 (
    'targetUrl' => 'http://localhost/login'
...

But if I put the absolute URL, the test works:

public function testUnauthenticatedIsRedirected(): void
{
    $this->client->request('GET', '/account');
    static::assertResponseRedirects('http://localhost/login');
}

Everyone puts the URI and it works, but not me... I'm on Symfony 5.2.1

Thank you in advance

1 Answers
$this->client->request('GET', '/account');
$response = $this->client->getResponse();
$this->assertEquals(Response::HTTP_FOUND, $response->getStatusCode());
$location = $response->headers->get('location');

$this->assertEquals('expected URL here', $location);
Related