Skip to content

Commit 90b7573

Browse files
committed
Symfony assertion refinement
1 parent 72cf4d1 commit 90b7573

File tree

3 files changed

+54
-121
lines changed

3 files changed

+54
-121
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: 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
{

src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php

Lines changed: 23 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,84 +4,41 @@
44

55
namespace Codeception\Module\Symfony;
66

7-
use PHPUnit\Framework\Constraint\LogicalAnd;
7+
use PHPUnit\Framework\Constraint\Constraint;
88
use PHPUnit\Framework\Constraint\LogicalNot;
9-
use Symfony\Component\DomCrawler\Crawler;
10-
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerAnySelectorTextContains;
11-
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerAnySelectorTextSame;
129
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorAttributeValueSame;
13-
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorCount;
1410
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists;
1511
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextContains;
1612
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextSame;
1713

1814
trait DomCrawlerAssertionsTrait
1915
{
20-
/**
21-
* Asserts that any element matching the given selector does contain the expected text.
22-
*/
23-
public function assertAnySelectorTextContains(string $selector, string $text, string $message = ''): void
24-
{
25-
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints(
26-
new CrawlerSelectorExists($selector),
27-
new CrawlerAnySelectorTextContains($selector, $text)
28-
), $message);
29-
}
30-
31-
/**
32-
* Asserts that any element matching the given selector does not contain the expected text.
33-
*/
34-
public function assertAnySelectorTextNotContains(string $selector, string $text, string $message = ''): void
35-
{
36-
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints(
37-
new CrawlerSelectorExists($selector),
38-
new LogicalNot(new CrawlerAnySelectorTextContains($selector, $text))
39-
), $message);
40-
}
41-
42-
/**
43-
* Asserts that any element matching the given selector does equal the expected text.
44-
*/
45-
public function assertAnySelectorTextSame(string $selector, string $text, string $message = ''): void
46-
{
47-
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints(
48-
new CrawlerSelectorExists($selector),
49-
new CrawlerAnySelectorTextSame($selector, $text)
50-
), $message);
51-
}
52-
5316
/**
5417
* Asserts that the checkbox with the given name is checked.
5518
*/
5619
public function assertCheckboxChecked(string $fieldName, string $message = ''): void
5720
{
58-
$this->assertThat(
59-
$this->getCrawler(),
60-
new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"),
61-
$message
62-
);
21+
$this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"), $message);
6322
}
6423

6524
/**
6625
* Asserts that the checkbox with the given name is not checked.
6726
*/
6827
public function assertCheckboxNotChecked(string $fieldName, string $message = ''): void
6928
{
70-
$this->assertThat(
71-
$this->getCrawler(),
72-
new LogicalNot(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked")),
73-
$message
74-
);
29+
$this->assertThatCrawler(new LogicalNot(
30+
new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked")
31+
), $message);
7532
}
7633

7734
/**
7835
* Asserts that value of the form input with the given name does not equal the expected value.
7936
*/
8037
public function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void
8138
{
82-
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints(
83-
new CrawlerSelectorExists("input[name=\"$fieldName\"]"),
84-
new LogicalNot(new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue))
39+
$this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]"), $message);
40+
$this->assertThatCrawler(new LogicalNot(
41+
new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)
8542
), $message);
8643
}
8744

@@ -90,10 +47,11 @@ public function assertInputValueNotSame(string $fieldName, string $expectedValue
9047
*/
9148
public function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
9249
{
93-
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints(
94-
new CrawlerSelectorExists("input[name=\"$fieldName\"]"),
95-
new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)
96-
), $message);
50+
$this->assertThatCrawler(new CrawlerSelectorExists("input[name=\"$fieldName\"]"), $message);
51+
$this->assertThatCrawler(
52+
new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue),
53+
$message
54+
);
9755
}
9856

9957
/**
@@ -112,65 +70,51 @@ public function assertPageTitleSame(string $expectedTitle, string $message = '')
11270
$this->assertSelectorTextSame('title', $expectedTitle, $message);
11371
}
11472

115-
/**
116-
* Asserts that the expected number of selector elements are in the response.
117-
*/
118-
public function assertSelectorCount(int $expectedCount, string $selector, string $message = ''): void
119-
{
120-
$this->assertThat($this->getCrawler(), new CrawlerSelectorCount($expectedCount, $selector), $message);
121-
}
122-
12373
/**
12474
* Asserts that the given selector does match at least one element in the response.
12575
*/
12676
public function assertSelectorExists(string $selector, string $message = ''): void
12777
{
128-
$this->assertThat($this->getCrawler(), new CrawlerSelectorExists($selector), $message);
78+
$this->assertThatCrawler(new CrawlerSelectorExists($selector), $message);
12979
}
13080

13181
/**
13282
* Asserts that the given selector does not match at least one element in the response.
13383
*/
13484
public function assertSelectorNotExists(string $selector, string $message = ''): void
13585
{
136-
$this->assertThat($this->getCrawler(), new LogicalNot(new CrawlerSelectorExists($selector)), $message);
86+
$this->assertThatCrawler(new LogicalNot(new CrawlerSelectorExists($selector)), $message);
13787
}
13888

13989
/**
14090
* Asserts that the first element matching the given selector does contain the expected text.
14191
*/
14292
public function assertSelectorTextContains(string $selector, string $text, string $message = ''): void
14393
{
144-
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints(
145-
new CrawlerSelectorExists($selector),
146-
new CrawlerSelectorTextContains($selector, $text)
147-
), $message);
94+
$this->assertThatCrawler(new CrawlerSelectorExists($selector), $message);
95+
$this->assertThatCrawler(new CrawlerSelectorTextContains($selector, $text), $message);
14896
}
14997

15098
/**
15199
* Asserts that the first element matching the given selector does not contain the expected text.
152100
*/
153101
public function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void
154102
{
155-
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints(
156-
new CrawlerSelectorExists($selector),
157-
new LogicalNot(new CrawlerSelectorTextContains($selector, $text))
158-
), $message);
103+
$this->assertThatCrawler(new CrawlerSelectorExists($selector), $message);
104+
$this->assertThatCrawler(new LogicalNot(new CrawlerSelectorTextContains($selector, $text)), $message);
159105
}
160106

161107
/**
162108
* Asserts that the contents of the first element matching the given selector does equal the expected text.
163109
*/
164110
public function assertSelectorTextSame(string $selector, string $text, string $message = ''): void
165111
{
166-
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints(
167-
new CrawlerSelectorExists($selector),
168-
new CrawlerSelectorTextSame($selector, $text)
169-
), $message);
112+
$this->assertThatCrawler(new CrawlerSelectorExists($selector), $message);
113+
$this->assertThatCrawler(new CrawlerSelectorTextSame($selector, $text), $message);
170114
}
171115

172-
protected function getCrawler(): Crawler
116+
protected function assertThatCrawler(Constraint $constraint, string $message): void
173117
{
174-
return $this->client->getCrawler();
118+
$this->assertThat($this->client->getCrawler(), $constraint, $message);
175119
}
176120
}

0 commit comments

Comments
 (0)