@@ -148,7 +148,7 @@ utilities used in the functional tests:
148148 Your First Functional Test
149149~~~~~~~~~~~~~~~~~~~~~~~~~~
150150Functional tests are PHP files that typically live in the ``tests/Controller ``
151- directory for your bundle . If you want to test the pages handled by your
151+ directory of your application . If you want to test the pages handled by your
152152``PostController `` class, start by creating a new ``PostControllerTest.php ``
153153file that extends a special ``WebTestCase `` class.
154154
@@ -288,48 +288,54 @@ document::
288288
289289 // ...
290290
291- // asserts that there is at least one h2 tag
292- // with the class "subtitle"
293- $this->assertGreaterThan(
294- 0,
295- $crawler->filter('h2.subtitle')->count()
291+ // asserts that there is at least one h2 tag with the class "subtitle"
292+ // the third argument is an optional message shown on failed tests
293+ $this->assertGreaterThan(0, $crawler->filter('h2.subtitle')->count(),
294+ 'There is at least one subtitle'
296295 );
297296
298297 // asserts that there are exactly 4 h2 tags on the page
299298 $this->assertCount(4, $crawler->filter('h2'));
300299
301300 // asserts that the "Content-Type" header is "application/json"
302- $this->assertTrue(
303- $client->getResponse()->headers->contains(
304- 'Content-Type',
305- 'application/json'
306- ),
307- 'the "Content-Type" header is "application/json"' // optional message shown on failure
308- );
301+ $this->assertResponseHeaderSame('Content-Type', 'application/json');
302+ // equivalent to:
303+ $this->assertTrue($client->getResponse()->headers->contains(
304+ 'Content-Type', 'application/json'
305+ ));
309306
310307 // asserts that the response content contains a string
311308 $this->assertStringContainsString('foo', $client->getResponse()->getContent());
312309 // ...or matches a regex
313310 $this->assertRegExp('/foo(bar)?/', $client->getResponse()->getContent());
314311
315312 // asserts that the response status code is 2xx
316- $this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
317- // asserts that the response status code is 404
313+ $this->assertResponseIsSuccessful();
314+ // equivalent to:
315+ $this->assertTrue($client->getResponse()->isSuccessful());
316+
317+ // asserts that the response status code is 404 Not Found
318318 $this->assertTrue($client->getResponse()->isNotFound());
319- // asserts a specific 200 status code
320- $this->assertEquals(
321- 200, // or Symfony\Component\HttpFoundation\Response::HTTP_OK
322- $client->getResponse()->getStatusCode()
323- );
319+
320+ // asserts a specific status code
321+ $this->assertResponseStatusCodeSame(201);
322+ // HTTP status numbers are available as constants too:
323+ // e.g. 201 === Symfony\Component\HttpFoundation\Response::HTTP_CREATED
324+ // equivalent to:
325+ $this->assertEquals(201, $client->getResponse()->getStatusCode());
324326
325327 // asserts that the response is a redirect to /demo/contact
326- $this->assertTrue(
327- $client->getResponse()->isRedirect('/demo/contact')
328- // if the redirection URL was generated as an absolute URL
329- // $client->getResponse()->isRedirect('http://localhost/demo/contact')
330- );
331- // ...or simply check that the response is a redirect to any URL
332- $this->assertTrue($client->getResponse()->isRedirect());
328+ $this->assertResponseRedirects('/demo/contact');
329+ // equivalent to:
330+ $this->assertTrue($client->getResponse()->isRedirect('/demo/contact'));
331+ // ...or check that the response is a redirect to any URL
332+ $this->assertResponseRedirects();
333+
334+ .. versionadded :: 4.3
335+
336+ The ``assertResponseHeaderSame() ``, ``assertResponseIsSuccessful() ``,
337+ ``assertResponseStatusCodeSame() ``, ``assertResponseRedirects() `` and other
338+ related methods were introduced in Symfony 4.3.
333339
334340.. _testing-data-providers :
335341
0 commit comments