8000 feature #38288 [DomCrawler] Add `assertFormValue()` in `WebTestCase` … · symfony/symfony@713f30e · GitHub
[go: up one dir, main page]

Skip to content

Commit 713f30e

Browse files
committed
feature #38288 [DomCrawler] Add assertFormValue() in WebTestCase (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`
2 parents af90e1e + c3e0336 commit 713f30e

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Add `cache.adapter.redis_tag_aware` tag to use `RedisCacheAwareAdapter`
1313
* added `framework.http_client.retry_failing` configuration tree
1414
* added `assertCheckboxChecked()` and `assertCheckboxNotChecked()` in `WebTestCase`
15+
* added `assertFormValue()` and `assertNoFormValue()` in `WebTestCase`
1516

1617
5.1.0
1718
-----

src/Symfony/Bundle/FrameworkBundle/Test/DomCrawlerAssertionsTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ public static function assertCheckboxNotChecked(string $fieldName, string $messa
101101
), $message);
102102
}
103103

104+
public static function assertFormValue(string $formSelector, string $fieldName, string $value, string $message = ''): void
105+
{
106+
$node = 8000 self::getCrawler()->filter($formSelector);
107+
self::assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector));
108+
$values = $node->form()->getValues();
109+
self::assertArrayHasKey($fieldName, $values, $message ?: sprintf('Field "%s" not found in form "%s".', $fieldName, $formSelector));
110+
self::assertSame($value, $values[$fieldName]);
111+
}
112+
113+
public static function assertNoFormValue(string $formSelector, string $fieldName, string $message = ''): void
114+
{
115+
$node = self::getCrawler()->filter($formSelector);
116+
self::assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector));
117+
$values = $node->form()->getValues();
118+
self::assertArrayNotHasKey($fieldName, $values, $message ?: sprintf('Field "%s" has a value in form "%s".', $fieldName, $formSelector));
119+
}
120+
104121
private static function getCrawler(): Crawler
105122
{
106123
if (!$crawler = self::getClient()->getCrawler()) {

src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ public function testAssertCheckboxNotChecked()
236236
$this->getCrawlerTester(new Crawler('<html><body><form><input type="checkbox" name="rememberMe" checked>'))->assertCheckboxNotChecked('rememberMe');
237237
}
238238

239+
public function testAssertFormValue()
240+
{
241+
$this->getCrawlerTester(new Crawler('<html><body><form id="form"><input type="text" name="username" value="Fabien">', 'http://localhost'))->assertFormValue('#form', 'username', 'Fabien');
242+
$this->expectException(AssertionFailedError::class);
243+
$this->expectExceptionMessage('Failed asserting that two strings are identical.');
244+
$this->getCrawlerTester(new Crawler('<html><body><form id="form"><input type="text" name="username" value="Fabien">', 'http://localhost'))->assertFormValue('#form', 'username', 'Jane');
245+
}
246+
247+
public function testAssertNoFormValue()
248+
{
249+
$this->getCrawlerTester(new Crawler('<html><body><form id="form"><input type="checkbox" name="rememberMe">', 'http://localhost'))->assertNoFormValue('#form', 'rememberMe');
250+
$this->expectException(AssertionFailedError::class);
251+
$this->expectExceptionMessage('Field "rememberMe" has a value in form "#form".');
252+
$this->getCrawlerTester(new Crawler('<html><body><form id="form"><input type="checkbox" name="rememberMe" checked>', 'http://localhost'))->assertNoFormValue('#form', 'rememberMe');
253+
}
254+
239255
public function testAssertRequestAttributeValueSame()
240256
{
241257
$this->getRequestTester()->assertRequestAttributeValueSame('foo', 'bar');

0 commit comments

Comments
 (0)
0