Skip to content

Commit

Permalink
feature symfony#38287 [DomCrawler] Add checkbox assertions for functi…
Browse files Browse the repository at this point in the history
…onal 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
-------

f26758e [DomCrawler] Add `assertCheckboxChecked()`
  • Loading branch information
fabpot committed Sep 24, 2020
2 parents 2bdcf4a + f26758e commit af90e1e
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 af90e1e

Please sign in to comment.