Skip to content

Commit 4bc7a02

Browse files
authored
Merge pull request #7804 from kenjis/docs-testing-response.rst
docs: improve testing/response.rst
2 parents 0989491 + 71b5a95 commit 4bc7a02

File tree

13 files changed

+115
-36
lines changed

13 files changed

+115
-36
lines changed

user_guide_src/source/testing/response.rst

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from your test cases. Usually a ``TestResponse`` will be provided for you as a r
88
create your own directly using any ``ResponseInterface``:
99

1010
.. literalinclude:: response/001.php
11+
:lines: 2-
1112

1213
.. contents::
1314
:local:
@@ -28,13 +29,15 @@ request()
2829
You can access directly the Request object, if it was set during testing:
2930

3031
.. literalinclude:: response/002.php
32+
:lines: 2-
3133

3234
response()
3335
----------
3436

3537
This allows you direct access to the response object:
3638

3739
.. literalinclude:: response/003.php
40+
:lines: 2-
3841

3942
Checking Response Status
4043
========================
@@ -46,27 +49,31 @@ Returns a boolean true/false based on whether the response is perceived to be "o
4649
a response status code in the 200 or 300's. An empty body is not considered valid, unless in redirects.
4750

4851
.. literalinclude:: response/004.php
52+
:lines: 2-
4953

5054
assertOK()
5155
----------
5256

5357
This assertion simply uses the ``isOK()`` method to test a response. ``assertNotOK()`` is the inverse of this assertion.
5458

5559
.. literalinclude:: response/005.php
60+
:lines: 2-
5661

5762
isRedirect()
5863
------------
5964

6065
Returns a boolean true/false based on whether the response is a redirected response.
6166

6267
.. literalinclude:: response/006.php
68+
:lines: 2-
6369

6470
assertRedirect()
6571
----------------
6672

6773
Asserts that the Response is an instance of RedirectResponse. ``assertNotRedirect()`` is the inverse of this assertion.
6874

6975
.. literalinclude:: response/007.php
76+
:lines: 2-
7077

7178
assertRedirectTo()
7279
------------------
@@ -75,20 +82,23 @@ Asserts that the Response is an instance of RedirectResponse and the destination
7582
matches the uri given.
7683

7784
.. literalinclude:: response/008.php
85+
:lines: 2-
7886

7987
getRedirectUrl()
8088
----------------
8189

8290
Returns the URL set for a RedirectResponse, or null for failure.
8391

8492
.. literalinclude:: response/009.php
93+
:lines: 2-
8594

8695
assertStatus(int $code)
8796
-----------------------
8897

8998
Asserts that the HTTP status code returned matches $code.
9099

91100
.. literalinclude:: response/010.php
101+
:lines: 2-
92102

93103
Session Assertions
94104
==================
@@ -100,13 +110,15 @@ Asserts that a value exists in the resulting session. If $value is passed, will
100110
matches what was specified.
101111

102112
.. literalinclude:: response/011.php
113+
:lines: 2-
103114

104115
assertSessionMissing(string $key)
105116
---------------------------------
106117

107118
Asserts that the resulting session does not include the specified $key.
108119

109120
.. literalinclude:: response/012.php
121+
:lines: 2-
110122

111123
Header Assertions
112124
=================
@@ -118,13 +130,15 @@ Asserts that a header named ``$key`` exists in the response. If ``$value`` is no
118130
the values match.
119131

120132
.. literalinclude:: response/013.php
133+
:lines: 2-
121134

122135
assertHeaderMissing(string $key)
123136
--------------------------------
124137

125138
Asserts that a header name ``$key`` does not exist in the response.
126139

127140
.. literalinclude:: response/014.php
141+
:lines: 2-
128142

129143
Cookie Assertions
130144
=================
@@ -136,13 +150,15 @@ Asserts that a cookie named ``$key`` exists in the response. If ``$value`` is no
136150
the values match. You can set the cookie prefix, if needed, by passing it in as the third parameter.
137151

138152
.. literalinclude:: response/015.php
153+
:lines: 2-
139154

140155
assertCookieMissing(string $key)
141156
--------------------------------
142157

143158
Asserts that a cookie named ``$key`` does not exist in the response.
144159

145160
.. literalinclude:: response/016.php
161+
:lines: 2-
146162

147163
assertCookieExpired(string $key, string $prefix = '')
148164
-----------------------------------------------------
@@ -151,6 +167,7 @@ Asserts that a cookie named ``$key`` exists, but has expired. You can set the co
151167
in as the second parameter.
152168

153169
.. literalinclude:: response/017.php
170+
:lines: 2-
154171

155172
DOM Helpers
156173
===========
@@ -161,14 +178,17 @@ are useful for using within assertions in your tests.
161178
see()
162179
-----
163180

164-
The ``see()`` method checks the text on the page to see if it exists either by itself, or more specifically within
181+
Returns a boolean true/false based on whether the text on the page exists either
182+
by itself, or more specifically within
165183
a tag, as specified by type, class, or id:
166184

167185
.. literalinclude:: response/018.php
186+
:lines: 2-
168187

169188
The ``dontSee()`` method is the exact opposite:
170189

171190
.. literalinclude:: response/019.php
191+
:lines: 2-
172192

173193
seeElement()
174194
------------
@@ -177,27 +197,31 @@ The ``seeElement()`` and ``dontSeeElement()`` are very similar to the previous m
177197
values of the elements. Instead, they simply check that the elements exist on the page:
178198

179199
.. literalinclude:: response/020.php
200+
:lines: 2-
180201

181202
seeLink()
182203
---------
183204

184205
You can use ``seeLink()`` to ensure that a link appears on the page with the specified text:
185206

186207
.. literalinclude:: response/021.php
208+
:lines: 2-
187209

188210
seeInField()
189211
------------
190212

191213
The ``seeInField()`` method checks for any input tags exist with the name and value:
192214

193215
.. literalinclude:: response/022.php
216+
:lines: 2-
194217

195218
seeCheckboxIsChecked()
196219
----------------------
197220

198221
Finally, you can check if a checkbox exists and is checked with the ``seeCheckboxIsChecked()`` method:
199222

200223
.. literalinclude:: response/023.php
224+
:lines: 2-
201225

202226
DOM Assertions
203227
==============
@@ -212,20 +236,23 @@ Asserts that text/HTML is on the page, either by itself or - more specifically -
212236
a tag, as specified by type, class, or id:
213237

214238
.. literalinclude:: response/024.php
239+
:lines: 2-
215240

216241
assertDontSee(string $search = null, string $element = null)
217242
------------------------------------------------------------
218243

219244
Asserts the exact opposite of the ``assertSee()`` method:
220245

221246
.. literalinclude:: response/025.php
247+
:lines: 2-
222248

223249
assertSeeElement(string $search)
224250
--------------------------------
225251

226252
Similar to ``assertSee()``, however this only checks for an existing element. It does not check for specific text:
227253

228254
.. literalinclude:: response/026.php
255+
:lines: 2-
229256

230257
assertDontSeeElement(string $search)
231258
------------------------------------
@@ -234,20 +261,23 @@ Similar to ``assertSee()``, however this only checks for an existing element tha
234261
specific text:
235262

236263
.. literalinclude:: response/027.php
264+
:lines: 2-
237265

238266
assertSeeLink(string $text, string $details = null)
239267
---------------------------------------------------
240268

241269
Asserts that an anchor tag is found with matching ``$text`` as the body of the tag:
242270

243271
.. literalinclude:: response/028.php
272+
:lines: 2-
244273

245274
assertSeeInField(string $field, string $value = null)
246275
-----------------------------------------------------
247276

248277
Asserts that an input tag exists with the name and value:
249278

250279
.. literalinclude:: response/029.php
280+
:lines: 2-
251281

252282
Working with JSON
253283
=================
@@ -261,19 +291,22 @@ getJSON()
261291
This method will return the body of the response as a JSON string:
262292

263293
.. literalinclude:: response/030.php
294+
:lines: 2-
264295

265296
You can use this method to determine if ``$response`` actually holds JSON content:
266297

267298
.. literalinclude:: response/031.php
299+
:lines: 2-
268300

269301
.. note:: Be aware that the JSON string will be pretty-printed in the result.
270302

271303
assertJSONFragment(array $fragment)
272304
-----------------------------------
273305

274-
Asserts that $fragment is found within the JSON response. It does not need to match the entire JSON value.
306+
Asserts that ``$fragment`` is found within the JSON response. It does not need to match the entire JSON value.
275307

276308
.. literalinclude:: response/032.php
309+
:lines: 2-
277310

278311
assertJSONExact($test)
279312
----------------------
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
<?php
22

33
// Check that "Hello World" is on the page
4-
$results->see('Hello World');
4+
if ($results->see('Hello World')) {
5+
// ...
6+
}
7+
58
// Check that "Hello World" is within an h1 tag
6-
$results->see('Hello World', 'h1');
9+
if ($results->see('Hello World', 'h1')) {
10+
// ...
11+
}
12+
713
// Check that "Hello World" is within an element with the "notice" class
8-
$results->see('Hello World', '.notice');
14+
if ($results->see('Hello World', '.notice')) {
15+
// ...
16+
}
17+
918
// Check that "Hello World" is within an element with id of "title"
10-
$results->see('Hello World', '#title');
19+
if ($results->see('Hello World', '#title')) {
20+
// ...
21+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
// Checks that "Hello World" does NOT exist on the page
4-
$results->dontSee('Hello World');
4+
if ($results->dontSee('Hello World')) {
5+
// ...
6+
}
7+
58
// Checks that "Hellow World" does NOT exist within any h1 tag
6-
$results->dontSee('Hello World', 'h1');
9+
if ($results->dontSee('Hello World', 'h1')) {
10+
// ...
11+
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
<?php
22

33
// Check that an element with class 'notice' exists
4-
$results->seeElement('.notice');
4+
if ($results->seeElement('.notice')) {
5+
// ...
6+
}
7+
58
// Check that an element with id 'title' exists
6-
$results->seeElement('#title');
9+
if ($results->seeElement('#title')) {
10+
// ...
11+
}
12+
713
// Verify that an element with id 'title' does NOT exist
8-
$results->dontSeeElement('#title');
14+
if ($results->dontSeeElement('#title')) {
15+
// ...
16+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
// Check that a link exists with 'Upgrade Account' as the text::
4-
$results->seeLink('Upgrade Account');
4+
if ($results->seeLink('Upgrade Account')) {
5+
// ...
6+
}
7+
58
// Check that a link exists with 'Upgrade Account' as the text, AND a class of 'upsell'
6-
$results->seeLink('Upgrade Account', '.upsell');
9+
if ($results->seeLink('Upgrade Account', '.upsell')) {
10+
// ...
11+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
// Check that an input exists named 'user' with the value 'John Snow'
4-
$results->seeInField('user', 'John Snow');
4+
if ($results->seeInField('user', 'John Snow')) {
5+
// ...
6+
}
7+
58
// Check a multi-dimensional input
6-
$results->seeInField('user[name]', 'John Snow');
9+
if ($results->seeInField('user[name]', 'John Snow')) {
10+
// ...
11+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
// Check if checkbox is checked with class of 'foo'
4-
$results->seeCheckboxIsChecked('.foo');
4+
if ($results->seeCheckboxIsChecked('.foo')) {
5+
// ...
6+
}
7+
58
// Check if checkbox with id of 'bar' is checked
6-
$results->seeCheckboxIsChecked('#bar');
9+
if ($results->seeCheckboxIsChecked('#bar')) {
10+
// ...
11+
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<?php
22

3-
// Check that "Hello World" is on the page
3+
// Verify that "Hello World" is on the page
44
$result->assertSee('Hello World');
5-
// Check that "Hello World" is within an h1 tag
5+
6+
// Verify that "Hello World" is within an h1 tag
67
$result->assertSee('Hello World', 'h1');
7-
// Check that "Hello World" is within an element with the "notice" class
8+
9+
// Verify that "Hello World" is within an element with the "notice" class
810
$result->assertSee('Hello World', '.notice');
9-
// Check that "Hello World" is within an element with id of "title"
11+
12+
// Verify that "Hello World" is within an element with id of "title"
1013
$result->assertSee('Hello World', '#title');
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

3-
// Checks that "Hello World" does NOT exist on the page
4-
$results->dontSee('Hello World');
5-
// Checks that "Hello World" does NOT exist within any h1 tag
6-
$results->dontSee('Hello World', 'h1');
3+
// Verify that "Hello World" does NOT exist on the page
4+
$results->assertDontSee('Hello World');
5+
6+
// Verify that "Hello World" does NOT exist within any h1 tag
7+
$results->assertDontSee('Hello World', 'h1');

0 commit comments

Comments
 (0)