@@ -148,7 +148,7 @@ utilities used in the functional tests:
148
148
Your First Functional Test
149
149
~~~~~~~~~~~~~~~~~~~~~~~~~~
150
150
Functional 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
152
152
``PostController `` class, start by creating a new ``PostControllerTest.php ``
153
153
file that extends a special ``WebTestCase `` class.
154
154
@@ -288,48 +288,54 @@ document::
288
288
289
289
// ...
290
290
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'
296
295
);
297
296
298
297
// asserts that there are exactly 4 h2 tags on the page
299
298
$this->assertCount(4, $crawler->filter('h2'));
300
299
301
300
// 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
+ ));
309
306
310
307
// asserts that the response content contains a string
311
308
$this->assertStringContainsString('foo', $client->getResponse()->getContent());
312
309
// ...or matches a regex
313
310
$this->assertRegExp('/foo(bar)?/', $client->getResponse()->getContent());
314
311
315
312
// 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
318
318
$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());
324
326
325
327
// 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.
333
339
334
340
.. _testing-data-providers :
335
341
@@ -393,13 +399,13 @@ returns a ``Crawler`` instance.
393
399
The full signature of the ``request() `` method is::
394
400
395
401
request(
396
- $method,
397
- $uri,
402
+ string $method,
403
+ string $uri,
398
404
array $parameters = [],
399
405
array $files = [],
400
406
array $server = [],
401
- $content = null,
402
- $changeHistory = true
407
+ string $content = null,
408
+ bool $changeHistory = true
403
409
)
404
410
405
411
The ``server `` array is the raw values that you'd expect to normally
0 commit comments