Skip to content

Commit 84719a6

Browse files
committed
feature #38287 [DomCrawler] Add checkbox assertions for functional tests (mnapoli)
This PR was merged into the 5.2-dev branch. Discussion ---------- [DomCrawler] Add checkbox assertions for functional tests | Q | A | ------------- | --- | Branch? | master for features | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Add new assertions for checkboxes, to use in functional tests. Example: ```php public function testAssertCheckboxChecked() { $this->visit('/edit-form'); $this->client->submitForm('Save', [ 'activateMembership' => 'on', ]); self::assertResponseIsSuccessful(); // Check that the field is checked after the form was submitted self::assertCheckboxChecked('activateMembership'); } ``` This wasn't possible to achieve with the existing `self::assertInputValueSame()` assertion. Commits ------- f26758e1c0 [DomCrawler] Add `assertCheckboxChecked()`
2 parents 4545c48 + 72a5a7e commit 84719a6

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Added `TemplateAwareDataCollectorInterface` and `AbstractDataCollector` to simplify custom data collector creation and leverage autoconfiguration
1212
* Add `cache.adapter.redis_tag_aware` tag to use `RedisCacheAwareAdapter`
1313
* added `framework.http_client.retry_failing` configuration tree
14+
* added `assertCheckboxChecked()` and `assertCheckboxNotChecked()` in `WebTestCase`
1415

1516
5.1.0
1617
-----

Test/DomCrawlerAssertionsTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use PHPUnit\Framework\Constraint\LogicalNot;
1616
use Symfony\Component\DomCrawler\Crawler;
1717
use Symfony\Component\DomCrawler\Test\Constraint as DomCrawlerConstraint;
18+
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorAttributeValueSame;
19+
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists;
1820

1921
/**
2022
* Ideas borrowed from Laravel Dusk's assertions.
@@ -83,6 +85,22 @@ public static function assertInputValueNotSame(string $fieldName, string $expect
8385
), $message);
8486
}
8587

88+
public static function assertCheckboxChecked(string $fieldName, string $message = ''): void
89+
{
90+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
91+
new CrawlerSelectorExists("input[name=\"$fieldName\"]"),
92+
new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'checked', 'checked')
93+
), $message);
94+
}
95+
96+
public static function assertCheckboxNotChecked(string $fieldName, string $message = ''): void
97+
{
98+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
99+
new CrawlerSelectorExists("input[name=\"$fieldName\"]"),
100+
new LogicalNot(new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'checked', 'checked'))
101+
), $message);
102+
}
103+
86104
private static function getCrawler(): Crawler
87105
{
88106
if (!$crawler = self::getClient()->getCrawler()) {

Tests/Test/WebTestCaseTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,22 @@ public function testAssertInputValueNotSame()
220220
$this->getCrawlerTester(new Crawler('<html><body><form><input type="text" name="password" value="pa$$">'))->assertInputValueNotSame('password', 'pa$$');
221221
}
222222

223+
public function testAssertCheckboxChecked()
224+
{
225+
$this->getCrawlerTester(new Crawler('<html><body><form><input type="checkbox" name="rememberMe" checked>'))->assertCheckboxChecked('rememberMe');
226+
$this->expectException(AssertionFailedError::class);
227+
$this->expectExceptionMessage('matches selector "input[name="rememberMe"]" and has a node matching selector "input[name="rememberMe"]" with attribute "checked" of value "checked".');
228+
$this->getCrawlerTester(new Crawler('<html><body><form><input type="checkbox" name="rememberMe">'))->assertCheckboxChecked('rememberMe');
229+
}
230+
231+
public function testAssertCheckboxNotChecked()
232+
{
233+
$this->getCrawlerTester(new Crawler('<html><body><form><input type="checkbox" name="rememberMe">'))->assertCheckboxNotChecked('rememberMe');
234+
$this->expectException(AssertionFailedError::class);
235+
$this->expectExceptionMessage('matches selector "input[name="rememberMe"]" and does not have a node matching selector "input[name="rememberMe"]" with attribute "checked" of value "checked".');
236+
$this->getCrawlerTester(new Crawler('<html><body><form><input type="checkbox" name="rememberMe" checked>'))->assertCheckboxNotChecked('rememberMe');
237+
}
238+
223239
public function testAssertRequestAttributeValueSame()
224240
{
225241
$this->getRequestTester()->assertRequestAttributeValueSame('foo', 'bar');

0 commit comments

Comments
 (0)