55namespace Codeception \Module \Symfony ;
66
77use PHPUnit \Framework \Constraint \Constraint ;
8- use PHPUnit \Framework \Constraint \LogicalAnd ;
98use PHPUnit \Framework \Constraint \LogicalNot ;
109use Symfony \Component \BrowserKit \Test \Constraint \BrowserCookieValueSame ;
1110use Symfony \Component \BrowserKit \Test \Constraint \BrowserHasCookie ;
2524trait BrowserAssertionsTrait
2625{
2726 /**
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.
2928 */
3029 public function assertBrowserCookieValueSame (string $ name , string $ expectedValue , bool $ raw = false , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
3130 {
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 );
3633 }
3734
3835 /**
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.
4038 */
4139 public function assertBrowserHasCookie (string $ name , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
4240 {
4341 $ this ->assertThatForClient (new BrowserHasCookie ($ name , $ path , $ domain ), $ message );
4442 }
4543
4644 /**
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.
4847 */
4948 public function assertBrowserNotHasCookie (string $ name , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
5049 {
5150 $ this ->assertThatForClient (new LogicalNot (new BrowserHasCookie ($ name , $ path , $ domain )), $ message );
5251 }
5352
5453 /**
55- * Asserts the given request attribute is set to the expected value.
54+ * Asserts that the specified request attribute matches the expected value.
5655 */
5756 public function assertRequestAttributeValueSame (string $ name , string $ expectedValue , string $ message = '' ): void
5857 {
5958 $ this ->assertThat ($ this ->getClient ()->getRequest (), new RequestAttributeValueSame ($ name , $ expectedValue ), $ message );
6059 }
6160
6261 /**
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.
6463 */
6564 public function assertResponseCookieValueSame (string $ name , string $ expectedValue , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
6665 {
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 );
7168 }
7269
7370 /**
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. This checks the format returned by the `Response::getFormat()` method.
7572 */
7673 public function assertResponseFormatSame (?string $ expectedFormat , string $ message = '' ): void
7774 {
7875 $ this ->assertThatForResponse (new ResponseFormatSame ($ this ->getClient ()->getRequest (), $ expectedFormat ), $ message );
7976 }
8077
8178 /**
82- * Asserts the given cookie is present in the response (optionally checking for a specific cookie path or domain) .
79+ * Asserts that the specified cookie is present in the response. Optionally, it can check for a specific cookie path or domain.
8380 */
8481 public function assertResponseHasCookie (string $ name , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
8582 {
8683 $ this ->assertThatForResponse (new ResponseHasCookie ($ name , $ path , $ domain ), $ message );
8784 }
8885
8986 /**
90- * Asserts the given header is available on the response, e.g. assertResponseHasHeader('content-type');.
87+ * Asserts that the specified header is available in the response.
88+ * For example, use `assertResponseHasHeader('content-type');`.
9189 */
9290 public function assertResponseHasHeader (string $ headerName , string $ message = '' ): void
9391 {
9492 $ this ->assertThatForResponse (new ResponseHasHeader ($ headerName ), $ message );
9593 }
9694
9795 /**
98- * Asserts the given header does not contain the expected value on the response,
99- * e.g. assertResponseHeaderNotSame('content-type', 'application/octet-stream');.
96+ * Asserts that the specified header does not contain the expected value in the response.
97+ * For example, use ` assertResponseHeaderNotSame('content-type', 'application/octet-stream');` .
10098 */
10199 public function assertResponseHeaderNotSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
102100 {
103101 $ this ->assertThatForResponse (new LogicalNot (new ResponseHeaderSame ($ headerName , $ expectedValue )), $ message );
104102 }
105103
106104 /**
107- * Asserts the given header does contain the expected value on the response,
108- * e.g. assertResponseHeaderSame('content-type', 'application/octet-stream');.
105+ * Asserts that the specified header contains the expected value in the response.
106+ * For example, use ` assertResponseHeaderSame('content-type', 'application/octet-stream');` .
109107 */
110108 public function assertResponseHeaderSame (string $ headerName , string $ expectedValue , string $ message = '' ): void
111109 {
112110 $ this ->assertThatForResponse (new ResponseHeaderSame ($ headerName , $ expectedValue ), $ message );
113111 }
114112
115113 /**
116- * Asserts that the response was successful (HTTP status is 2xx).
114+ * Asserts that the response was successful (HTTP status code is in the 2xx range ).
117115 */
118116 public function assertResponseIsSuccessful (string $ message = '' , bool $ verbose = true ): void
119117 {
120118 $ this ->assertThatForResponse (new ResponseIsSuccessful ($ verbose ), $ message );
121119 }
122120
123121 /**
124- * Asserts the response is unprocessable (HTTP status is 422)
122+ * Asserts that the response is unprocessable (HTTP status code is 422).
125123 */
126124 public function assertResponseIsUnprocessable (string $ message = '' , bool $ verbose = true ): void
127125 {
128126 $ this ->assertThatForResponse (new ResponseIsUnprocessable ($ verbose ), $ message );
129127 }
130128
131129 /**
132- * Asserts the given cookie is not present in the response (optionally checking for a specific cookie path or domain) .
130+ * Asserts that the specified cookie is not present in the response. Optionally, it can check for a specific cookie path or domain.
133131 */
134132 public function assertResponseNotHasCookie (string $ name , string $ path = '/ ' , ?string $ domain = null , string $ message = '' ): void
135133 {
136134 $ this ->assertThatForResponse (new LogicalNot (new ResponseHasCookie ($ name , $ path , $ domain )), $ message );
137135 }
138136
139137 /**
140- * Asserts the given header is not available on the response, e.g. assertResponseNotHasHeader('content-type');.
138+ * Asserts that the specified header is not available in the response.
139+ * For example, use `assertResponseNotHasHeader('content-type');`.
141140 */
142141 public function assertResponseNotHasHeader (string $ headerName , string $ message = '' ): void
143142 {
144143 $ this ->assertThatForResponse (new LogicalNot (new ResponseHasHeader ($ headerName )), $ message );
145144 }
146145
147146 /**
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.
147+ * Asserts that the response is a redirect. Optionally , you can check the target location and status code.
148+ * The expected location can be either an absolute or a relative path.
150149 */
151150 public function assertResponseRedirects (?string $ expectedLocation = null , ?int $ expectedCode = null , string $ message = '' , bool $ verbose = true ): void
152151 {
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- }
152+ $ this ->assertThatForResponse (new ResponseIsRedirected ($ verbose ), $ message );
160153
161- $ constraint = LogicalAnd::fromConstraints ($ constraint , $ locationConstraint );
154+ if ($ expectedLocation ) {
155+ $ constraint = class_exists (ResponseHeaderLocationSame::class)
156+ ? new ResponseHeaderLocationSame ($ this ->getClient ()->getRequest (), $ expectedLocation )
157+ : new ResponseHeaderSame ('Location ' , $ expectedLocation );
158+ $ this ->assertThatForResponse ($ constraint , $ message );
162159 }
160+
163161 if ($ expectedCode ) {
164- $ constraint = LogicalAnd:: fromConstraints ( $ constraint , new ResponseStatusCodeSame ($ expectedCode ));
162+ $ this -> assertThatForResponse ( new ResponseStatusCodeSame ($ expectedCode ), $ message );
165163 }
166-
167- $ this ->assertThatForResponse ($ constraint , $ message );
168164 }
169165
170166 /**
171- * Asserts a specific HTTP status code.
167+ * Asserts that the response status code matches the expected code.
172168 */
173169 public function assertResponseStatusCodeSame (int $ expectedCode , string $ message = '' , bool $ verbose = true ): void
174170 {
@@ -178,23 +174,18 @@ public function assertResponseStatusCodeSame(int $expectedCode, string $message
178174 /**
179175 * Asserts the request matches the given route and optionally route parameters.
180176 */
181- public function assertRouteSame (string $ expectedRoute , array $ parameters = [], string $ message = '' ): void
182- {
183- $ constraint = new RequestAttributeValueSame ('_route ' , $ expectedRoute );
184- $ constraints = [];
177+ public function assertRouteSame (string $ expectedRoute , array $ parameters = [], string $ message = '' ): void {
178+ $ request = $ this -> getClient ()-> getRequest ();
179+ $ this -> assertThat ( $ request , new RequestAttributeValueSame ('_route ' , $ expectedRoute) );
180+
185181 foreach ($ parameters as $ key => $ value ) {
186- $ constraints [] = new RequestAttributeValueSame ($ key , $ value );
187- }
188- if ($ constraints ) {
189- $ constraint = LogicalAnd::fromConstraints ($ constraint , ...$ constraints );
182+ $ this ->assertThat ($ request , new RequestAttributeValueSame ($ key , $ value ), $ message );
190183 }
191-
192- $ this ->assertThat ($ this ->getClient ()->getRequest (), $ constraint , $ message );
193184 }
194185
195186 /**
196- * Reboot client's kernel.
197- * Can be used to manually reboot kernel when 'rebootable_client' => false
187+ * Reboots the client's kernel.
188+ * Can be used to manually reboot the kernel when 'rebootable_client' is set to false.
198189 *
199190 * ```php
200191 * <?php
@@ -214,7 +205,7 @@ public function rebootClientKernel(): void
214205
215206 /**
216207 * Verifies that a page is available.
217- * By default, it checks the current page, specify the `$url` parameter to change it .
208+ * By default, it checks the current page. Specify the `$url` parameter to change the page being checked .
218209 *
219210 * ```php
220211 * <?php
@@ -224,7 +215,7 @@ public function rebootClientKernel(): void
224215 * $I->seePageIsAvailable('/dashboard'); // Same as above
225216 * ```
226217 *
227- * @param string|null $url
218+ * @param string|null $url The URL of the page to check. If null, the current page is checked.
228219 */
229220 public function seePageIsAvailable (?string $ url = null ): void
230221 {
@@ -237,7 +228,7 @@ public function seePageIsAvailable(?string $url = null): void
237228 }
238229
239230 /**
240- * Goes to a page and check that it redirects to another.
231+ * Navigates to a page and verifies that it redirects to another page .
241232 *
242233 * ```php
243234 * <?php
@@ -246,21 +237,24 @@ public function seePageIsAvailable(?string $url = null): void
246237 */
247238 public function seePageRedirectsTo (string $ page , string $ redirectsTo ): void
248239 {
249- $ this ->getClient ()->followRedirects (false );
240+ $ client = $ this ->getClient ();
241+ $ client ->followRedirects (false );
250242 $ this ->amOnPage ($ page );
251- $ response = $ this -> getClient ()-> getResponse ();
243+
252244 $ this ->assertTrue (
253- $ response ->isRedirection ()
245+ $ client ->getResponse ()->isRedirection (),
246+ 'The response is not a redirection. '
254247 );
255- $ this ->getClient ()->followRedirect ();
248+
249+ $ client ->followRedirect ();
256250 $ this ->seeInCurrentUrl ($ redirectsTo );
257251 }
258252
259253 /**
260- * Submit a form specifying the form name only once.
254+ * Submits a form by specifying the form name only once.
261255 *
262256 * 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.
257+ * If you have customized the names of the field selectors, use `$I->submitForm()` for full control.
264258 *
265259 * ```php
266260 * <?php
@@ -270,8 +264,8 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
270264 * ]);
271265 * ```
272266 *
273- * @param string $name The `name` attribute of the `<form>` (you cannot use an array as selector here)
274- * @param string[] $fields
267+ * @param string $name The `name` attribute of the `<form>`. You cannot use an array as a selector here.
268+ * @param array< string, mixed> $fields The form fields to submit.
275269 */
276270 public function submitSymfonyForm (string $ name , array $ fields ): void
277271 {
0 commit comments