@@ -292,54 +292,55 @@ document::
292
292
293
293
// ...
294
294
295
- // asserts that there is at least one h2 tag
296
- // with the class "subtitle"
297
- $this->assertGreaterThan(
298
- 0,
299
- $crawler->filter('h2.subtitle')->count()
295
+ // asserts that there is at least one h2 tag with the class "subtitle"
296
+ // the third argument is an optional message shown on failed tests
297
+ $this->assertGreaterThan(0, $crawler->filter('h2.subtitle')->count(),
298
+ 'There is at least one subtitle'
300
299
);
301
300
302
301
// asserts that there are exactly 4 h2 tags on the page
303
302
$this->assertCount(4, $crawler->filter('h2'));
304
303
305
304
// asserts that the "Content-Type" header is "application/json"
306
- $this->assertTrue(
307
- $client->getResponse()->headers->contains(
308
- 'Content-Type',
309
- 'application/json'
310
- ),
311
- 'the "Content-Type" header is "application/json"' // optional message shown on failure
312
- );
313
- //or
314
- $this->assertResponseHeaderSame('Content-Type', 'application/json', 'the "Content-Type" header is "application/json"');
305
+ $this->assertResponseHeaderSame('Content-Type', 'application/json');
306
+ // equivalent to:
307
+ $this->assertTrue($client->getResponse()->headers->contains(
308
+ 'Content-Type', 'application/json'
309
+ ));
310
+
315
311
// asserts that the response content contains a string
316
312
$this->assertContains('foo', $client->getResponse()->getContent());
317
313
// ...or matches a regex
318
314
$this->assertRegExp('/foo(bar)?/', $client->getResponse()->getContent());
319
315
320
316
// asserts that the response status code is 2xx
321
- $this->assertTrue($client->getResponse()->isSuccessful(), 'response status is 2xx');
322
- // ...or simply
323
- $this->assertResponseIsSuccessful('response status is 2xx');
324
- // asserts that the response status code is 404
317
+ $this->assertResponseIsSuccessful();
318
+ // equivalent to:
319
+ $this->assertTrue($client->getResponse()->isSuccessful());
320
+
321
+ // asserts that the response status code is 404 Not Found
325
322
$this->assertTrue($client->getResponse()->isNotFound());
326
- // asserts a specific 200 status code
327
- $this->assertEquals(
328
- 200, // or Symfony\Component\HttpFoundation\Response::HTTP_OK
329
- $client->getResponse()->getStatusCode()
330
- );
331
- //...or simply
332
- $this->assertResponseStatusCodeSame(200);
323
+
324
+ // asserts a specific status code
325
+ $this->assertResponseStatusCodeSame(201);
326
+ // HTTP status numbers are available as constants too:
327
+ // e.g. 201 === Symfony\Component\HttpFoundation\Response::HTTP_CREATED
328
+ // equivalent to:
329
+ $this->assertEquals(201, $client->getResponse()->getStatusCode());
330
+
333
331
// asserts that the response is a redirect to /demo/contact
334
- $this->assertTrue(
335
- $client->getResponse()->isRedirect('/demo/contact')
336
- // if the redirection URL was generated as an absolute URL
337
- // $client->getResponse()->isRedirect('http://localhost/demo/contact')
338
- );
339
- // ...or simply check that the response is a redirect to any URL
340
- $this->assertTrue($client->getResponse()->isRedirect());
341
- // ...or simply
332
+ $this->assertResponseRedirects('/demo/contact');
333
+ // equivalent to:
334
+ $this->assertTrue($client->getResponse()->isRedirect('/demo/contact'));
335
+ // ...or check that the response is a redirect to any URL
342
336
$this->assertResponseRedirects();
337
+
338
+ .. versionadded :: 4.3
339
+
340
+ The ``assertResponseHeaderSame() ``, ``assertResponseIsSuccessful() ``,
341
+ ``assertResponseStatusCodeSame() ``, ``assertResponseRedirects() `` and other
342
+ related methods were introduced in Symfony 4.3.
343
+
343
344
.. _testing-data-providers :
344
345
345
346
Testing against Different Sets of Data
0 commit comments