8000 Add docs of new WebTestCase assertions · symfony/symfony-docs@3b1a1a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3b1a1a1

Browse files
committed
Add docs of new WebTestCase assertions
1 parent d403f97 commit 3b1a1a1

File tree

2 files changed

+105
-5
lines changed

2 files changed

+105
-5
lines changed

testing.rst

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,29 @@ component, run:
204204
$ composer require --dev symfony/css-selector
205205
206206
Now you can use CSS selectors with the crawler. To assert that the phrase
207-
"Hello World" is on the page at least once, you can use this assertion::
207+
"Hello World" is present in the page's main title, you can use this assertion::
208208

209-
$this->assertGreaterThan(
210-
0,
211-
$crawler->filter('html:contains("Hello World")')->count()
212-
);
209+
$this->assertSelectorTextContains('html h1.title', 'Hello World');
210+
211+
This assertion will internally call ``$crawler->filter('html h1.title')``, which allows
212+
you to use CSS selectors to filter any HTML element in the page and check for
213+
its existence, attributes, text, etc.
214+
215+
The ``assertSelectorTextContains`` method is not a native PHPUnit assertion and is
216+
available thanks to the ``WebTestCase`` class.
217+
218+
.. versionadded:: 4.3
219+
220+
The ``WebTestCase`` assertions were introduced in Symfony 4.3
221+
222+
.. seealso::
223+
224+
Using native PHPUnit methods, the same assertion would look like this::
225+
226+
$this->assertGreaterThan(
227+
0,
228+
$crawler->filter('html h1.title:contains("Hello World")')->count()
229+
);
213230

214231
The crawler can also be used to interact with the page. Click on a link by first
215232
selecting it with the crawler using either an XPath expression or a CSS selector,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
.. index::
2+
single: Tests; Assertions
3+
4+
Functional Test specific Assertions
5+
===================================
6+
7+
.. versionadded:: 4.3
8+
9+
The shortcut methods for assertions using ``WebTestCase`` were introduced
10+
in Symfony 4.3.
11+
12+
When doing functional tests, sometimes you need to make complex assertions in
13+
order to check whether the ``Request``, the ``Response`` or the ``Crawler``
14+
contain the expected information to make your test succeed.
15+
16+
Here is an example with plain PHPUnit::
17+
18+
$this->assertGreaterThan(
19+
0,
20+
$crawler->filter('html:contains("Hello World")')->count()
21+
);
22+
23+
Now here is the example with the assertions specific to Symfony::
24+
25+
$this->assertSelectorTextContains('html', 'Hello World');
26+
27+
.. note::
28+
29+
These assertions only work if a request has been made with the ``Client``
30+
in a test case extending the ``WebTestCase`` class.
31+
32+
Assertions Reference
33+
---------------------
34+
35+
Response
36+
~~~~~~~~
37+
38+
- ``assertResponseIsSuccessful()``
39+
- ``assertResponseStatusCodeSame()``
40+
- ``assertResponseRedirects()``
41+
- ``assertResponseHasHeader()``
42+
- ``assertResponseNotHasHeader()``
43+
- ``assertResponseHeaderSame()``
44+
- ``assertResponseHeaderNotSame()``
45+
- ``assertResponseHasCookie()``
46+
- ``assertResponseNotHasCookie()``
47+
- ``assertResponseCookieValueSame()``
48+
49+
Request
50+
~~~~~~~
51+
52+
- ``assertRequestAttributeValueSame()``
53+
- ``assertRouteSame()``
54+
55+
Browser
56+
~~~~~~~
57+
58+
- ``assertBrowserHasCookie()``
59+
- ``assertBrowserNotHasCookie()``
60+
- ``assertBrowserCookieValueSame()``
61+
62+
Crawler
63+
~~~~~~~
64+
65+
- ``assertSelectorExists()``
66+
- ``assertSelectorNotExists()``
67+
- ``assertSelectorTextContains()``
68+
- ``assertSelectorTextSame()``
69+
- ``assertSelectorTextNotContains()``
70+
- ``assertPageTitleSame()``
71+
- ``assertPageTitleContains()``
72+
- ``assertInputValueSame()``
73+
- ``assertInputValueNotSame()``
74+
75+
Troubleshooting
76+
---------------
77+
78+
These assertions will not work with `symfony/panther`_ as they use the
79+
``Request`` and ``Response`` objects from the ``HttpFoundation``
80+
component, and the ``KernelBrowser`` from the ``FrameworkBundle``.
81+
Panther only uses the ``BrowserKit`` component.
82+
83+
.. _`symfony/panther`: https://github.com/symfony/panther

0 commit comments

Comments
 (0)
0