Skip to content

Commit

Permalink
[DomCrawler] Add assertCheckboxChecked()
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Sep 24, 2020
1 parent 7cb8ba5 commit f26758e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CHANGELOG
* Added `TemplateAwareDataCollectorInterface` and `AbstractDataCollector` to simplify custom data collector creation and leverage autoconfiguration
* Add `cache.adapter.redis_tag_aware` tag to use `RedisCacheAwareAdapter`
* added `framework.http_client.retry_failing` configuration tree
* added `assertCheckboxChecked()` and `assertCheckboxNotChecked()` in `WebTestCase`

5.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use PHPUnit\Framework\Constraint\LogicalNot;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\DomCrawler\Test\Constraint as DomCrawlerConstraint;
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorAttributeValueSame;
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists;

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

public static function assertCheckboxChecked(string $fieldName, string $message = ''): void
{
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new CrawlerSelectorExists("input[name=\"$fieldName\"]"),
new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'checked', 'checked')
), $message);
}

public static function assertCheckboxNotChecked(string $fieldName, string $message = ''): void
{
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new CrawlerSelectorExists("input[name=\"$fieldName\"]"),
new LogicalNot(new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'checked', 'checked'))
), $message);
}

private static function getCrawler(): Crawler
{
if (!$crawler = self::getClient()->getCrawler()) {
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ public function testAssertInputValueNotSame()
$this->getCrawlerTester(new Crawler('<html><body><form><input type="text" name="password" value="pa$$">'))->assertInputValueNotSame('password', 'pa$$');
}

public function testAssertCheckboxChecked()
{
$this->getCrawlerTester(new Crawler('<html><body><form><input type="checkbox" name="rememberMe" checked>'))->assertCheckboxChecked('rememberMe');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('matches selector "input[name="rememberMe"]" and has a node matching selector "input[name="rememberMe"]" with attribute "checked" of value "checked".');
$this->getCrawlerTester(new Crawler('<html><body><form><input type="checkbox" name="rememberMe">'))->assertCheckboxChecked('rememberMe');
}

public function testAssertCheckboxNotChecked()
{
$this->getCrawlerTester(new Crawler('<html><body><form><input type="checkbox" name="rememberMe">'))->assertCheckboxNotChecked('rememberMe');
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('matches selector "input[name="rememberMe"]" and does not have a node matching selector "input[name="rememberMe"]" with attribute "checked" of value "checked".');
$this->getCrawlerTester(new Crawler('<html><body><form><input type="checkbox" name="rememberMe" checked>'))->assertCheckboxNotChecked('rememberMe');
}

public function testAssertRequestAttributeValueSame()
{
$this->getRequestTester()->assertRequestAttributeValueSame('foo', 'bar');
Expand Down

0 comments on commit f26758e

Please sign in to comment.