5
5
namespace Codeception \Module \Symfony ;
6
6
7
7
use PHPUnit \Framework \Constraint \Constraint ;
8
- use PHPUnit \Framework \Constraint \LogicalAnd ;
9
8
use PHPUnit \Framework \Constraint \LogicalNot ;
10
9
use Symfony \Component \BrowserKit \Test \Constraint \BrowserCookieValueSame ;
11
10
use Symfony \Component \BrowserKit \Test \Constraint \BrowserHasCookie ;
25
24
trait BrowserAssertionsTrait
26
25
{
27
26
/**
28
- * Asserts the given cookie in the test Client is set to the expected value.
27
+ * Asserts that the given cookie in the test client is set to the expected value.
29
28
*/
30
29
public function assertBrowserCookieValueSame (string $ name , string $ expectedValue , bool $ raw = false , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
31
30
{
32
- $ this ->assertThatForClient (LogicalAnd::fromConstraints (
33
- new BrowserHasCookie ($ name , $ path , $ domain ),
34
- new BrowserCookieValueSame ($ name , $ expectedValue , $ raw , $ path , $ domain )
35
- ), $ message );
31
+ $ this ->assertThatForClient (new BrowserHasCookie ($ name , $ path , $ domain ), $ message );
32
+ $ this ->assertThatForClient (new BrowserCookieValueSame ($ name , $ expectedValue , $ raw , $ path , $ domain ), $ message );
36
33
}
37
34
38
35
/**
39
- * Asserts that the test Client does have the given cookie set (meaning, the cookie was set by any response in the test).
36
+ * Asserts that the test client has the specified cookie set.
37
+ * This indicates that the cookie was set by any response during the test.
40
38
*/
41
39
public function assertBrowserHasCookie (string $ name , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
42
40
{
43
41
$ this ->assertThatForClient (new BrowserHasCookie ($ name , $ path , $ domain ), $ message );
44
42
}
45
43
46
44
/**
47
- * Asserts that the test Client does not have the given cookie set (meaning, the cookie was set by any response in the test).
45
+ * Asserts that the test client does not have the specified cookie set.
46
+ * This indicates that the cookie was not set by any response during the test.
48
47
*/
49
48
public function assertBrowserNotHasCookie (string $ name , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
50
49
{
51
50
$ this ->assertThatForClient (new LogicalNot (new BrowserHasCookie ($ name , $ path , $ domain )), $ message );
52
51
}
53
52
54
53
/**
55
- * Asserts the given request attribute is set to the expected value.
54
+ * Asserts that the specified request attribute matches the expected value.
56
55
*/
57
56
public function assertRequestAttributeValueSame (string $ name , string $ expectedValue , string $ message = '' ): void
58
57
{
59
58
$ this ->assertThat ($ this ->getClient ()->getRequest (), new RequestAttributeValueSame ($ name , $ expectedValue ), $ message );
60
59
}
61
60
62
61
/**
63
- * Asserts the given cookie is present and set to the expected value.
62
+ * Asserts that the specified response cookie is present and matches the expected value.
64
63
*/
65
64
public function assertResponseCookieValueSame (string $ name , string $ expectedValue , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
66
65
{
67
- $ this ->assertThatForResponse (LogicalAnd::fromConstraints (
68
- new ResponseHasCookie ($ name , $ path , $ domain ),
69
- new ResponseCookieValueSame ($ name , $ expectedValue , $ path , $ domain )
70
- ), $ message );
66
+ $ this ->assertThatForResponse (new ResponseHasCookie ($ name , $ path , $ domain ), $ message );
67
+ $ this ->assertThatForResponse (new ResponseCookieValueSame ($ name , $ expectedValue , $ path , $ domain ), $ message );
71
68
}
72
69
73
70
/**
74
- * Asserts the response format returned by the `Response::getFormat()` method is the same as the expected value.
71
+ * Asserts that the response format matches the expected format.
72
+ * This checks the format returned by the `Response::getFormat()` method.
75
73
*/
76
74
public function assertResponseFormatSame (?string $ expectedFormat , string $ message = '' ): void
77
75
{
78
76
$ this ->assertThatForResponse (new ResponseFormatSame ($ this ->getClient ()->getRequest (), $ expectedFormat ), $ message );
79
77
}
80
78
81
79
/**
82
- * Asserts the given cookie is present in the response (optionally checking for a specific cookie path or domain).
80
+ * Asserts that the specified cookie is present in the response.
81
+ * Optionally, it can check for a specific cookie path or domain.
83
82
*/
84
83
public function assertResponseHasCookie (string $ name , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
85
84
{
86
85
$ this ->assertThatForResponse (new ResponseHasCookie ($ name , $ path , $ domain ), $ message );
87
86
}
88
87
89
88
/**
90
- * Asserts the given header is available on the response, e.g. assertResponseHasHeader('content-type');.
89
+ * Asserts that the specified header is available in the response.
90
+ * For example, use `assertResponseHasHeader('content-type');`.
91
91
*/
92
92
public function assertResponseHasHeader (string $ headerName , string $ message = '' ): void
93
93
{
94
94
$ this ->assertThatForResponse (new ResponseHasHeader ($ headerName ), $ message );
95
95
}
96
96
97
97
/**
98
- * Asserts the given header does not contain the expected value on the response,
99
- * e.g. assertResponseHeaderNotSame('content-type', 'application/octet-stream');.
98
+ * Asserts that the specified header does not contain the expected value in the response.
99
+ * For example, use ` assertResponseHeaderNotSame('content-type', 'application/octet-stream');` .
100
100
*/
101
101
public function assertResponseHeaderNotSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
102
102
{
103
103
$ this ->assertThatForResponse (new LogicalNot (new ResponseHeaderSame ($ headerName , $ expectedValue )), $ message );
104
104
}
105
105
106
106
/**
107
- * Asserts the given header does contain the expected value on the response,
108
- * e.g. assertResponseHeaderSame('content-type', 'application/octet-stream');.
107
+ * Asserts that the specified header contains the expected value in the response.
108
+ * For example, use ` assertResponseHeaderSame('content-type', 'application/octet-stream');` .
109
109
*/
110
110
public function assertResponseHeaderSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
111
111
{
112
112
$ this ->assertThatForResponse (new ResponseHeaderSame ($ headerName , $ expectedValue ), $ message );
113
113
}
114
114
115
115
/**
116
- * Asserts that the response was successful (HTTP status is 2xx).
116
+ * Asserts that the response was successful (HTTP status code is in the 2xx range ).
117
117
*/
118
118
public function assertResponseIsSuccessful (string $ message = '' , bool $ verbose = true ): void
119
119
{
120
120
$ this ->assertThatForResponse (new ResponseIsSuccessful ($ verbose ), $ message );
121
121
}
122
122
123
123
/**
124
- * Asserts the response is unprocessable (HTTP status is 422)
124
+ * Asserts that the response is unprocessable (HTTP status code is 422).
125
125
*/
126
126
public function assertResponseIsUnprocessable (string $ message = '' , bool $ verbose = true ): void
127
127
{
128
128
$ this ->assertThatForResponse (new ResponseIsUnprocessable ($ verbose ), $ message );
129
129
}
130
130
131
131
/**
132
- * Asserts the given cookie is not present in the response (optionally checking for a specific cookie path or domain).
132
+ * Asserts that the specified cookie is not present in the response.
133
+ * Optionally, it can check for a specific cookie path or domain.
133
134
*/
134
135
public function assertResponseNotHasCookie (string $ name , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
135
136
{
136
137
$ this ->assertThatForResponse (new LogicalNot (new ResponseHasCookie ($ name , $ path , $ domain )), $ message );
137
138
}
138
139
139
140
/**
140
- * Asserts the given header is not available on the response, e.g. assertResponseNotHasHeader('content-type');.
141
+ * Asserts that the specified header is not available in the response.
142
+ * For example, use `assertResponseNotHasHeader('content-type');`.
141
143
*/
142
144
public function assertResponseNotHasHeader (string $ headerName , string $ message = '' ): void
143
145
{
144
146
$ this ->assertThatForResponse (new LogicalNot (new ResponseHasHeader ($ headerName )), $ message );
145
147
}
146
148
147
149
/**
148
- * Asserts the response is a redirect response (optionally, you can check the target location and status code).
149
- * The excepted location can be either an absolute or a relative path.
150
+ * Asserts that the response is a redirect.
151
+ * Optionally, you can check the target location and status code.
152
+ * The expected location can be either an absolute or a relative path.
150
153
*/
151
154
public function assertResponseRedirects (?string $ expectedLocation = null , ?int $ expectedCode = null , string $ message = '' , bool $ verbose = true ): void
152
155
{
153
- $ constraint = new ResponseIsRedirected ($ verbose );
154
- if ($ expectedLocation ) {
155
- if (class_exists (ResponseHeaderLocationSame::class)) {
156
- $ locationConstraint = new ResponseHeaderLocationSame ($ this ->getClient ()->getRequest (), $ expectedLocation );
157
- } else {
158
- $ locationConstraint = new ResponseHeaderSame ('Location ' , $ expectedLocation );
159
- }
156
+ $ this ->assertThatForResponse (new ResponseIsRedirected ($ verbose ), $ message );
160
157
161
- $ constraint = LogicalAnd::fromConstraints ($ constraint , $ locationConstraint );
158
+ if ($ expectedLocation ) {
159
+ $ constraint = class_exists (ResponseHeaderLocationSame::class)
160
+ ? new ResponseHeaderLocationSame ($ this ->getClient ()->getRequest (), $ expectedLocation )
161
+ : new ResponseHeaderSame ('Location ' , $ expectedLocation );
162
+ $ this ->assertThatForResponse ($ constraint , $ message );
162
163
}
164
+
163
165
if ($ expectedCode ) {
164
- $ constraint = LogicalAnd:: fromConstraints ( $ constraint , new ResponseStatusCodeSame ($ expectedCode ));
166
+ $ this -> assertThatForResponse ( new ResponseStatusCodeSame ($ expectedCode ), $ message );
165
167
}
166
-
167
- $ this ->assertThatForResponse ($ constraint , $ message );
168
168
}
169
169
170
170
/**
171
- * Asserts a specific HTTP status code.
171
+ * Asserts that the response status code matches the expected code.
172
172
*/
173
173
public function assertResponseStatusCodeSame (int $ expectedCode , string $ message = '' , bool $ verbose = true ): void
174
174
{
@@ -178,23 +178,18 @@ public function assertResponseStatusCodeSame(int $expectedCode, string $message
178
178
/**
179
179
* Asserts the request matches the given route and optionally route parameters.
180
180
*/
181
- public function assertRouteSame (string $ expectedRoute , array $ parameters = [], string $ message = '' ): void
182
- {
183
- $ constraint = new RequestAttributeValueSame ('_route ' , $ expectedRoute );
184
- $ constraints = [];
181
+ public function assertRouteSame (string $ expectedRoute , array $ parameters = [], string $ message = '' ): void {
182
+ $ request = $ this -> getClient ()-> getRequest ();
183
+ $ this -> assertThat ( $ request , new RequestAttributeValueSame ('_route ' , $ expectedRoute) );
184
+
185
185
foreach ($ parameters as $ key => $ value ) {
186
- $ constraints [] = new RequestAttributeValueSame ($ key , $ value );
187
- }
188
- if ($ constraints ) {
189
- $ constraint = LogicalAnd::fromConstraints ($ constraint , ...$ constraints );
186
+ $ this ->assertThat ($ request , new RequestAttributeValueSame ($ key , $ value ), $ message );
190
187
}
191
-
192
- $ this ->assertThat ($ this ->getClient ()->getRequest (), $ constraint , $ message );
193
188
}
194
189
195
190
/**
196
- * Reboot client's kernel.
197
- * Can be used to manually reboot kernel when 'rebootable_client' => false
191
+ * Reboots the client's kernel.
192
+ * Can be used to manually reboot the kernel when 'rebootable_client' is set to false.
198
193
*
199
194
* ```php
200
195
* <?php
@@ -214,7 +209,7 @@ public function rebootClientKernel(): void
214
209
215
210
/**
216
211
* Verifies that a page is available.
217
- * By default, it checks the current page, specify the `$url` parameter to change it .
212
+ * By default, it checks the current page. Specify the `$url` parameter to change the page being checked .
218
213
*
219
214
* ```php
220
215
* <?php
@@ -224,7 +219,7 @@ public function rebootClientKernel(): void
224
219
* $I->seePageIsAvailable('/dashboard'); // Same as above
225
220
* ```
226
221
*
227
- * @param string|null $url
222
+ * @param string|null $url The URL of the page to check. If null, the current page is checked.
228
223
*/
229
224
public function seePageIsAvailable (?string $ url = null ): void
230
225
{
@@ -237,7 +232,7 @@ public function seePageIsAvailable(?string $url = null): void
237
232
}
238
233
239
234
/**
240
- * Goes to a page and check that it redirects to another.
235
+ * Navigates to a page and verifies that it redirects to another page .
241
236
*
242
237
* ```php
243
238
* <?php
@@ -246,21 +241,24 @@ public function seePageIsAvailable(?string $url = null): void
246
241
*/
247
242
public function seePageRedirectsTo (string $ page , string $ redirectsTo ): void
248
243
{
249
- $ this ->getClient ()->followRedirects (false );
244
+ $ client = $ this ->getClient ();
245
+ $ client ->followRedirects (false );
250
246
$ this ->amOnPage ($ page );
251
- $ response = $ this -> getClient ()-> getResponse ();
247
+
252
248
$ this ->assertTrue (
253
- $ response ->isRedirection ()
249
+ $ client ->getResponse ()->isRedirection (),
250
+ 'The response is not a redirection. '
254
251
);
255
- $ this ->getClient ()->followRedirect ();
252
+
253
+ $ client ->followRedirect ();
256
254
$ this ->seeInCurrentUrl ($ redirectsTo );
257
255
}
258
256
259
257
/**
260
- * Submit a form specifying the form name only once.
258
+ * Submits a form by specifying the form name only once.
261
259
*
262
260
* Use this function instead of [`$I->submitForm()`](#submitForm) to avoid repeating the form name in the field selectors.
263
- * If you customized the names of the field selectors use `$I->submitForm()` for full control.
261
+ * If you have customized the names of the field selectors, use `$I->submitForm()` for full control.
264
262
*
265
263
* ```php
266
264
* <?php
@@ -270,8 +268,8 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
270
268
* ]);
271
269
* ```
272
270
*
273
- * @param string $name The `name` attribute of the `<form>` (you cannot use an array as selector here)
274
- * @param string[] $fields
271
+ * @param string $name The `name` attribute of the `<form>`. You cannot use an array as a selector here.
272
+ * @param array< string, mixed> $fields The form fields to submit.
275
273
*/
276
274
public function submitSymfonyForm (string $ name , array $ fields ): void
277
275
{
0 commit comments