In Symfony applications, creating functional tests is simple thanks to the
utilities provided by the WebTestCase
class and PHPUnit's assertions.
However, for common use cases (such as testing that the response was successful
or that it redirected to some URL) the assert___()
methods are too basic
and require writing too much code.
Lots of Symfony developers have created their own utility classes for tests to
solve this issue. That's why in Symfony 4.3 we've decided to add the most common
ones in a new Symfony
which is included by default in WebTestCase
, so you only have to upgrade
your application to Symfony 4.3 to start using them.
Example of checking if the response was successful:
1 2 3 4
// Before
$this->assertSame(200, $client->getResponse()->getStatusCode());
// After
$this->assertResponseIsSuccessful();
Example of checking if the response redirects to some URL:
1 2 3 4 5
// Before
$this->assertSame(301, $client->getResponse()->getStatusCode());
$this->assertSame('https://example.com', $client->getResponse()->headers->get('Location'));
// After
$this->assertResponseRedirects('https://example.com', 301);
Example of checking if some element contains some text:
1 2 3 4
// Before
$this->assertContains('Hello World', $crawler->filter('h1')->text());
// After
$this->assertSelectorTextContains('h1', 'Hello World');
The new assertions will make your tests more readable and will boost your productivity. Here's the full list of assertions available:
assertClientCookieValueEquals()
assertClientHasCookie()
assertClientNotHasCookie()
assertClientRawCookieValueEquals()
assertHttpCodeEquals()
assertInputValueEquals()
assertInputValueNotEquals()
assertPageTitleContains()
assertPageTitleEquals()
assertRequestAttributeValueEquals()
assertResponseCookieValueEquals()
assertResponseCookieValueNotEquals()
assertResponseHasCookie()
assertResponseHasHeader()
assertResponseHeaderEquals()
assertResponseHeaderNotEquals()
assertResponseIsSuccessful()
assertResponseNotHasCookie()
assertResponseNotHasHeader()
assertResponseRedirects()
assertRouteEquals()
assertSelectorContainsText()
assertSelectorExists()
assertSelectorNotContainsText()
assertSelectorNotExists()
Nice enhancement. Well done ;)
Niceeeee
Thanks for this new feature.
I don't really like $this->assertResponseIsSuccessful(), because When It fails it says "Failing asserting false is true". And IMHO, this is not as explicit as "Failing asserting a 403 is 200".
@Greg:
$this->assertResponseIsSuccessful()
will not fail saying "Failing asserting false is true". It will fail saying "Failing asserting that the response is successful".A successful Response is just a Response with a status code in the 2xx range.
If you want to check for a specific status code, use
$this->assertResponseStatusCodeSame(200);
Cool cool
Very nice :) this would be fun to implement in Rector
Nice, thanks!
Looks like some method from the list are missing in Symfony code, e.g assertHttpCodeEquals(). It was in #29990, but not in #30813 that replaced it.