Using the Symfony2 WebTestCase, I have the following test :
$client->request('GET', '/');
$this->assertTrue($client->getResponse() instanceof RedirectResponse);
$crawler = $client->followRedirect();
I would like to test the new url, after the redirection ends with /login
but I have found no way to access the new URL.
You can get the current URL with $client->getResponse()->headers->get('location')
, and assert it ends with /login using assertRegExp()
.
$this->assertRegExp('/\/login$/', $client->getResponse()->headers->get('location'));
Just tried Samy Dindane solution without success (maybe something changed from 2.0)
Anyway, in 2.1, I was able to retrieve current URL in test with:
$client->getRequest()->getUri()
Then, you can use assertRegexp().
You can also do something like this (Symfony 2.3)
$client->getHistory()->current()->getUri()
You can add this row $client->getResponse()->getTargetUrl()
before $client->followRedirect()
to get the url where you will be redirected
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With