Skip to content

Commit 30f7784

Browse files
committed
Symfony assertion refinement
1 parent 72cf4d1 commit 30f7784

File tree

3 files changed

+90
-148
lines changed

3 files changed

+90
-148
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"symfony/http-kernel": "^5.4 | ^6.4 | ^7.0",
4444
"symfony/mailer": "^5.4 | ^6.4 | ^7.0",
4545
"symfony/mime": "^5.4 | ^6.4 | ^7.0",
46-
"symfony/notifier": "5.4 | ^6.4 | ^7.0",
46+
"symfony/notifier": "^5.4 | ^6.4 | ^7.0",
4747
"symfony/options-resolver": "^5.4 | ^6.4 | ^7.0",
4848
"symfony/property-access": "^5.4 | ^6.4 | ^7.0",
4949
"symfony/property-info": "^5.4 | ^6.4 | ^7.0",

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Codeception\Module\Symfony;
66

77
use PHPUnit\Framework\Constraint\Constraint;
8-
use PHPUnit\Framework\Constraint\LogicalAnd;
98
use PHPUnit\Framework\Constraint\LogicalNot;
109
use Symfony\Component\BrowserKit\Test\Constraint\BrowserCookieValueSame;
1110
use Symfony\Component\BrowserKit\Test\Constraint\BrowserHasCookie;
@@ -25,150 +24,151 @@
2524
trait 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.
72+
* This checks the format returned by the `Response::getFormat()` method.
7573
*/
7674
public function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void
7775
{
7876
$this->assertThatForResponse(new ResponseFormatSame($this->getClient()->getRequest(), $expectedFormat), $message);
7977
}
8078

8179
/**
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.
8382
*/
8483
public function assertResponseHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
8584
{
8685
$this->assertThatForResponse(new ResponseHasCookie($name, $path, $domain), $message);
8786
}
8887

8988
/**
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');`.
9191
*/
9292
public function assertResponseHasHeader(string $headerName, string $message = ''): void
9393
{
9494
$this->assertThatForResponse(new ResponseHasHeader($headerName), $message);
9595
}
9696

9797
/**
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');`.
100100
*/
101101
public function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
102102
{
103103
$this->assertThatForResponse(new LogicalNot(new ResponseHeaderSame($headerName, $expectedValue)), $message);
104104
}
105105

106106
/**
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');`.
109109
*/
110110
public function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void
111111
{
112112
$this->assertThatForResponse(new ResponseHeaderSame($headerName, $expectedValue), $message);
113113
}
114114

115115
/**
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).
117117
*/
118118
public function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void
119119
{
120120
$this->assertThatForResponse(new ResponseIsSuccessful($verbose), $message);
121121
}
122122

123123
/**
124-
* Asserts the response is unprocessable (HTTP status is 422)
124+
* Asserts that the response is unprocessable (HTTP status code is 422).
125125
*/
126126
public function assertResponseIsUnprocessable(string $message = '', bool $verbose = true): void
127127
{
128128
$this->assertThatForResponse(new ResponseIsUnprocessable($verbose), $message);
129129
}
130130

131131
/**
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.
133134
*/
134135
public function assertResponseNotHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void
135136
{
136137
$this->assertThatForResponse(new LogicalNot(new ResponseHasCookie($name, $path, $domain)), $message);
137138
}
138139

139140
/**
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');`.
141143
*/
142144
public function assertResponseNotHasHeader(string $headerName, string $message = ''): void
143145
{
144146
$this->assertThatForResponse(new LogicalNot(new ResponseHasHeader($headerName)), $message);
145147
}
146148

147149
/**
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.
150153
*/
151154
public function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void
152155
{
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);
160157

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);
162163
}
164+
163165
if ($expectedCode) {
164-
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseStatusCodeSame($expectedCode));
166+
$this->assertThatForResponse(new ResponseStatusCodeSame($expectedCode), $message);
165167
}
166-
167-
$this->assertThatForResponse($constraint, $message);
168168
}
169169

170170
/**
171-
* Asserts a specific HTTP status code.
171+
* Asserts that the response status code matches the expected code.
172172
*/
173173
public function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void
174174
{
@@ -178,23 +178,18 @@ public function assertResponseStatusCodeSame(int $expectedCode, string $message
178178
/**
179179
* Asserts the request matches the given route and optionally route parameters.
180180
*/
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+
185185
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);
190187
}
191-
192-
$this->assertThat($this->getClient()->getRequest(), $constraint, $message);
193188
}
194189

195190
/**
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.
198193
*
199194
* ```php
200195
* <?php
@@ -214,7 +209,7 @@ public function rebootClientKernel(): void
214209

215210
/**
216211
* 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.
218213
*
219214
* ```php
220215
* <?php
@@ -224,7 +219,7 @@ public function rebootClientKernel(): void
224219
* $I->seePageIsAvailable('/dashboard'); // Same as above
225220
* ```
226221
*
227-
* @param string|null $url
222+
* @param string|null $url The URL of the page to check. If null, the current page is checked.
228223
*/
229224
public function seePageIsAvailable(?string $url = null): void
230225
{
@@ -237,7 +232,7 @@ public function seePageIsAvailable(?string $url = null): void
237232
}
238233

239234
/**
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.
241236
*
242237
* ```php
243238
* <?php
@@ -246,21 +241,24 @@ public function seePageIsAvailable(?string $url = null): void
246241
*/
247242
public function seePageRedirectsTo(string $page, string $redirectsTo): void
248243
{
249-
$this->getClient()->followRedirects(false);
244+
$client = $this->getClient();
245+
$client->followRedirects(false);
250246
$this->amOnPage($page);
251-
$response = $this->getClient()->getResponse();
247+
252248
$this->assertTrue(
253-
$response->isRedirection()
249+
$client->getResponse()->isRedirection(),
250+
'The response is not a redirection.'
254251
);
255-
$this->getClient()->followRedirect();
252+
253+
$client->followRedirect();
256254
$this->seeInCurrentUrl($redirectsTo);
257255
}
258256

259257
/**
260-
* Submit a form specifying the form name only once.
258+
* Submits a form by specifying the form name only once.
261259
*
262260
* 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.
264262
*
265263
* ```php
266264
* <?php
@@ -270,8 +268,8 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
270268
* ]);
271269
* ```
272270
*
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.
275273
*/
276274
public function submitSymfonyForm(string $name, array $fields): void
277275
{

0 commit comments

Comments
 (0)