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 ;
@@ -29,10 +28,8 @@ trait BrowserAssertionsTrait
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
/**
@@ -64,10 +61,8 @@ public function assertRequestAttributeValueSame(string $name, string $expectedVa
64
61
*/
65
62
public function assertResponseCookieValueSame (string $ name , string $ expectedValue , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
66
63
{
67
- $ this ->assertThatForResponse (LogicalAnd::fromConstraints (
68
- new ResponseHasCookie ($ name , $ path , $ domain ),
69
- new ResponseCookieValueSame ($ name , $ expectedValue , $ path , $ domain )
70
- ), $ message );
64
+ $ this ->assertThatForResponse (new ResponseHasCookie ($ name , $ path , $ domain ), $ message );
65
+ $ this ->assertThatForResponse (new ResponseCookieValueSame ($ name , $ expectedValue , $ path , $ domain ), $ message );
71
66
}
72
67
73
68
/**
@@ -146,25 +141,22 @@ public function assertResponseNotHasHeader(string $headerName, string $message =
146
141
147
142
/**
148
143
* 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.
144
+ * The expected location can be either an absolute or a relative path.
150
145
*/
151
146
public function assertResponseRedirects (?string $ expectedLocation = null , ?int $ expectedCode = null , string $ message = '' , bool $ verbose = true ): void
152
147
{
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
- }
148
+ $ this ->assertThatForResponse (new ResponseIsRedirected ($ verbose ), $ message );
160
149
161
- $ constraint = LogicalAnd::fromConstraints ($ constraint , $ locationConstraint );
150
+ if ($ expectedLocation ) {
151
+ $ constraint = class_exists (ResponseHeaderLocationSame::class)
152
+ ? new ResponseHeaderLocationSame ($ this ->getClient ()->getRequest (), $ expectedLocation )
153
+ : new ResponseHeaderSame ('Location ' , $ expectedLocation );
154
+ $ this ->assertThatForResponse ($ constraint , $ message );
162
155
}
156
+
163
157
if ($ expectedCode ) {
164
- $ constraint = LogicalAnd:: fromConstraints ( $ constraint , new ResponseStatusCodeSame ($ expectedCode ));
158
+ $ this -> assertThatForResponse ( new ResponseStatusCodeSame ($ expectedCode ), $ message );
165
159
}
166
-
167
- $ this ->assertThatForResponse ($ constraint , $ message );
168
160
}
169
161
170
162
/**
@@ -178,22 +170,17 @@ public function assertResponseStatusCodeSame(int $expectedCode, string $message
178
170
/**
179
171
* Asserts the request matches the given route and optionally route parameters.
180
172
*/
181
- public function assertRouteSame (string $ expectedRoute , array $ parameters = [], string $ message = '' ): void
182
- {
183
- $ constraint = new RequestAttributeValueSame ('_route ' , $ expectedRoute );
184
- $ constraints = [];
173
+ public function assertRouteSame (string $ expectedRoute , array $ parameters = [], string $ message = '' ): void {
174
+ $ request = $ this -> getClient ()-> getRequest ();
175
+ $ this -> assertThat ( $ request , new RequestAttributeValueSame ('_route ' , $ expectedRoute) );
176
+
185
177
foreach ($ parameters as $ key => $ value ) {
186
- $ constraints [] = new RequestAttributeValueSame ($ key , $ value );
187
- }
188
- if ($ constraints ) {
189
- $ constraint = LogicalAnd::fromConstraints ($ constraint , ...$ constraints );
178
+ $ this ->assertThat ($ request , new RequestAttributeValueSame ($ key , $ value ), $ message );
190
179
}
191
-
192
- $ this ->assertThat ($ this ->getClient ()->getRequest (), $ constraint , $ message );
193
180
}
194
181
195
182
/**
196
- * Reboot client's kernel.
183
+ * Reboots the client's kernel.
197
184
* Can be used to manually reboot kernel when 'rebootable_client' => false
198
185
*
199
186
* ```php
@@ -237,7 +224,7 @@ public function seePageIsAvailable(?string $url = null): void
237
224
}
238
225
239
226
/**
240
- * Goes to a page and check that it redirects to another.
227
+ * Navigates to a page and verifies that it redirects to another.
241
228
*
242
229
* ```php
243
230
* <?php
@@ -246,21 +233,23 @@ public function seePageIsAvailable(?string $url = null): void
246
233
*/
247
234
public function seePageRedirectsTo (string $ page , string $ redirectsTo ): void
248
235
{
249
- $ this ->getClient ()->followRedirects (false );
236
+ $ client = $ this ->getClient ();
237
+ $ client ->followRedirects (false );
250
238
$ this ->amOnPage ($ page );
251
- $ response = $ this -> getClient ()-> getResponse ();
239
+
252
240
$ this ->assertTrue (
253
- $ response ->isRedirection ()
241
+ $ client -> getResponse () ->isRedirection ()
254
242
);
255
- $ this ->getClient ()->followRedirect ();
243
+
244
+ $ client ->followRedirect ();
256
245
$ this ->seeInCurrentUrl ($ redirectsTo );
257
246
}
258
247
259
248
/**
260
- * Submit a form specifying the form name only once.
249
+ * Submits a form by specifying the form name only once.
261
250
*
262
251
* 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.
252
+ * If you customized the names of the field selectors, use `$I->submitForm()` for full control.
264
253
*
265
254
* ```php
266
255
* <?php
@@ -270,8 +259,8 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
270
259
* ]);
271
260
* ```
272
261
*
273
- * @param string $name The `name` attribute of the `<form>` (you cannot use an array as selector here)
274
- * @param string[] $ fields
262
+ * @param string $name The `name` attribute of the `<form>` (you cannot use an array as a selector here)
263
+ * @param array< string, mixed> $fields Form fields
275
264
*/
276
265
public function submitSymfonyForm (string $ name , array $ fields ): void
277
266
{
0 commit comments