Skip to content

Commit 7a46b02

Browse files
committed
Symfony assertion refinement
1 parent 72cf4d1 commit 7a46b02

File tree

1 file changed

+30
-41
lines changed

1 file changed

+30
-41
lines changed

src/Codeception/Module/Symfony/BrowserAssertionsTrait.php

Lines changed: 30 additions & 41 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;
@@ -29,10 +28,8 @@ trait BrowserAssertionsTrait
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
/**
@@ -64,10 +61,8 @@ public function assertRequestAttributeValueSame(string $name, string $expectedVa
6461
*/
6562
public function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', ?string $domain = null, string $message = ''): void
6663
{
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);
7166
}
7267

7368
/**
@@ -146,25 +141,22 @@ public function assertResponseNotHasHeader(string $headerName, string $message =
146141

147142
/**
148143
* 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.
150145
*/
151146
public function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void
152147
{
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);
160149

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);
162155
}
156+
163157
if ($expectedCode) {
164-
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseStatusCodeSame($expectedCode));
158+
$this->assertThatForResponse(new ResponseStatusCodeSame($expectedCode), $message);
165159
}
166-
167-
$this->assertThatForResponse($constraint, $message);
168160
}
169161

170162
/**
@@ -178,22 +170,17 @@ public function assertResponseStatusCodeSame(int $expectedCode, string $message
178170
/**
179171
* Asserts the request matches the given route and optionally route parameters.
180172
*/
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+
185177
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);
190179
}
191-
192-
$this->assertThat($this->getClient()->getRequest(), $constraint, $message);
193180
}
194181

195182
/**
196-
* Reboot client's kernel.
183+
* Reboots the client's kernel.
197184
* Can be used to manually reboot kernel when 'rebootable_client' => false
198185
*
199186
* ```php
@@ -237,7 +224,7 @@ public function seePageIsAvailable(?string $url = null): void
237224
}
238225

239226
/**
240-
* Goes to a page and check that it redirects to another.
227+
* Navigates to a page and verifies that it redirects to another.
241228
*
242229
* ```php
243230
* <?php
@@ -246,21 +233,23 @@ public function seePageIsAvailable(?string $url = null): void
246233
*/
247234
public function seePageRedirectsTo(string $page, string $redirectsTo): void
248235
{
249-
$this->getClient()->followRedirects(false);
236+
$client = $this->getClient();
237+
$client->followRedirects(false);
250238
$this->amOnPage($page);
251-
$response = $this->getClient()->getResponse();
239+
252240
$this->assertTrue(
253-
$response->isRedirection()
241+
$client->getResponse()->isRedirection()
254242
);
255-
$this->getClient()->followRedirect();
243+
244+
$client->followRedirect();
256245
$this->seeInCurrentUrl($redirectsTo);
257246
}
258247

259248
/**
260-
* Submit a form specifying the form name only once.
249+
* Submits a form by specifying the form name only once.
261250
*
262251
* 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.
264253
*
265254
* ```php
266255
* <?php
@@ -270,8 +259,8 @@ public function seePageRedirectsTo(string $page, string $redirectsTo): void
270259
* ]);
271260
* ```
272261
*
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
275264
*/
276265
public function submitSymfonyForm(string $name, array $fields): void
277266
{

0 commit comments

Comments
 (0)