Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DomCrawler] Allow to add choices to single select #53559

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.1
---

* Allow to add choices to single select

7.0
---

Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ public function setValue(string|array|bool|null $value): void
*/
public function addChoice(\DOMElement $node): void
{
if (!$this->multiple && 'radio' !== $this->type) {
if (!$this->multiple && ('radio' !== $this->type && 'select' !== $this->type)) {
throw new \LogicException(sprintf('Unable to add a choice for "%s" as it is not multiple or is not a radio button.', $this->name));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message (and method docblock) should be updated.

@stof Would that be a BC break, as select were refused before ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smnandre given that's an @internal method BC break does not apply here

}

$option = $this->buildOptionValue($node);
$this->options[] = $option;

if ($node->hasAttribute('checked')) {
if ($node->hasAttribute('checked') || $node->hasAttribute('selected')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type === "radio" a&& attribute checked || type === select && attribute selected ?

$this->value = $option['value'];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ public function testSelects()
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is an array');
}

$option = $this->createNode('option', 'Hello World', ['value' => 'hello_world']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a separate test instead of adding more things in the same test. This provides a much better experience:

  • we have a test name telling us what this is about
  • the status of the test is separate, giving us better failures

Existing dom-crawler tests are not following the testing best practices because this is one of the oldest components of Symfony, at a time where the usage of phpunit in Symfony was still quite new.

$field->addChoice($option);

$this->assertNotSame('hello_world', $field->getValue());
$field->setValue('hello_world');
$this->assertSame('hello_world', $field->getValue(), '->setValue() changes the selected option to dynamically added one');

$option = $this->createNode('option', 'Mr. Robot', ['value' => 'mr_robot', 'selected' => true]);
$field->addChoice($option);

$this->assertSame('mr_robot', $field->getValue(), '->addChoice() changes the value to added choice if selected attribute is set');
}

public function testSelectWithEmptyBooleanAttribute()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.