Skip to content

Commit da9f199

Browse files
committed
minor #49839 [BrowserKit] Improve the error message when submitForm() can't find the form (alamirault)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [BrowserKit] Improve the error message when `submitForm()` can't find the form | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #49826 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> When form is not found, error message is now `The form doesn't contain any button with "Save changes" as its content, id, value or name.` instead of `The current node list is empty.` Commits ------- 187c120cd0 [BrowserKit] Improve the error message when `submitForm()` can't find the form
2 parents 7120114 + b17797b commit da9f199

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

AbstractBrowser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ public function submitForm(string $button, array $fieldValues = [], string $meth
333333
}
334334

335335
$buttonNode = $this->crawler->selectButton($button);
336+
337+
if (0 === $buttonNode->count()) {
338+
throw new \InvalidArgumentException(sprintf('There is no button with "%s" as its content, id, value or name.', $button));
339+
}
340+
336341
$form = $buttonNode->form($fieldValues, $method);
337342

338343
return $this->submit($form, [], $serverParameters);

Tests/AbstractBrowserTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,14 @@ public function testSubmitFormNotFound()
350350
$client->setNextResponse(new Response('<html><form action="/foo"><input type="submit" /></form></html>'));
351351
$client->request('GET', 'http://www.example.com/foo/foobar');
352352

353-
try {
354-
$client->submitForm('Register', [
355-
'username' => 'username',
356-
'password' => 'password',
357-
], 'POST');
358-
$this->fail('->submitForm() throws a \InvalidArgumentException if the form could not be found');
359-
} catch (\Exception $e) {
360-
$this->assertInstanceOf(\InvalidArgumentException::class, $e, '->submitForm() throws a \InvalidArgumentException if the form could not be found');
361-
}
353+
$this->expectExceptionObject(
354+
new \InvalidArgumentException('There is no button with "Register" as its content, id, value or name.')
355+
);
356+
357+
$client->submitForm('Register', [
358+
'username' => 'username',
359+
'password' => 'password',
360+
], 'POST');
362361
}
363362

364363
public function testSubmitPreserveAuth()

0 commit comments

Comments
 (0)