Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DomCrawler] Add checkbox assertions for functional tests #38287

Merged
merged 1 commit into from Sep 24, 2020

Conversation

mnapoli
Copy link
Contributor

@mnapoli mnapoli commented Sep 24, 2020

Q A
Branch? master for features
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR symfony/symfony-docs#...

Add new assertions for checkboxes, to use in functional tests.

Example:

    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.

@mnapoli
Copy link
Contributor Author

mnapoli commented Sep 24, 2020

I also needed to assert on selects and that wasn't possible to add a similar helper.

So I went a different route and opened #38288: I think both PRs can be merged because they can cover different use cases (this PR doesn't require a form).

@nicolas-grekas nicolas-grekas added this to the next milestone Sep 24, 2020
fabpot
fabpot approved these changes Sep 24, 2020
@fabpot
Copy link
Member

fabpot commented Sep 24, 2020

Thank you @mnapoli.

@fabpot fabpot merged commit af90e1e into symfony:master Sep 24, 2020
3 checks passed
fabpot added a commit that referenced this pull request Sep 24, 2020
…(mnapoli)

This PR was merged into the 5.2-dev branch.

Discussion
----------

[DomCrawler] Add `assertFormValue()` in `WebTestCase`

| 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 checking form values, to use in functional tests.

Example:

```php
    public function testForm()
    {
        $this->visit('/edit-form');

        self::assertNoFormValue('#form', 'activateMembership'); // checkboxes don't have values when unchecked
        self::assertFormValue('#form', 'trialPeriod', '15');

        $this->client->submitForm('Save', [
            'activateMembership' => 'on',
            'trialPeriod' => '7',
        ]);

        self::assertResponseIsSuccessful();
        self::assertFormValue('#form', 'activateMembership', 'on');
        self::assertFormValue('#form', 'trialPeriod', '7');
    }
```

I resorted to using the `->form()` API because:

- checking the value of checkboxes was not possible today via existing assertions (I opened #38287 for that)
- checking the value of selects was impossible using CSS selectors (as far as I know), but possible with the `->form()` helper

Commits
-------

c3e0336 [DomCrawler] Add `assertFormValue()` and `assertNoFormValue()` in `WebTestCase`
@mnapoli mnapoli deleted the domcrawler/assert-checkbox branch Sep 24, 2020
@nicolas-grekas nicolas-grekas modified the milestones: next, 5.2 Oct 5, 2020
@fabpot fabpot mentioned this pull request Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants