[Autocomplete] Testing an autocomplete
Hello !
I'm currently testing an application that uses symfony/ux-autocomplete in a form and i'm facing an issue where i can't test my form because there is no possible values.
Do you have an idea of what i can do to test this field ?
PHPUnit : 9.6 Symfony : 6.4 UX-Autocomplete : 2.13.2
Hey,
https://github.com/symfony/symfony/pull/47642 should help if someone will care :D
With Crawler you might need to add the <option value="..."> element first, e.g. using ChoiceFormField::addChoice($node) because there is a special check that validates the value option before setting it, simulating what happens in the browser via Javascript.
@yceruto I've tried this but there is still a problem - it's impossible to add choice to single select element.
https://github.com/symfony/symfony/blob/0d9562ff6cdda11c71f0eb2bce0076f0d3a8ea9f/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php#L146-L148
so it's simulating a wrong behavior?
I've been through the same issue here and didn't find any solutions at that time...
I found that ChoiceFormField has disableValidation method but it is internal.
Tried some hacks with Reflection to populate the option to it's option list and then setting value, but still form populates an error that value should not be blank while I explicitly set it..
You do want a full application test ? Or an unit -- integration ? Can you share a bit of code ? There are as many ways to use those classes than there are developers..
For sure an functional test
It's really surprising that ChoiceFormField::addChoice() only work for multiple and radio choices... I don't get why this limitation and also why marked as internal when it's public and useful. I will check.
If someone was inclined to create a mini reproducer that could help motivate people to digg into this with you
Hey guys!
I'm facing an issue with this test where I try to create a user from a form:
public function testNew(): void
{
$this->client->loginUser($this->userRepository->findOneBy(['username' => 'admin']));
$originalNumObjectsInRepository = $this->userRepository->count([]);
$this->client->request('GET', $this->generateNewFormUrl());
$this->client->submitForm('Create', [
'User[username]' => 'Testing',
'User[email]' => '[email protected]',
'User[client][autocomplete]' => '1', //This field is the one facing the issue
'User[password][first]' => 'test',
'User[password][second]' => 'test',
]);
self::assertResponseStatusCodeSame(302);
self::assertSame($originalNumObjectsInRepository + 1, $this->userRepository->count([]));
}
And I'm getting this error:
1) App\Tests\CrudController\UserCrudControllerTest::testNew InvalidArgumentException: Input "User[client][autocomplete]" cannot take "1" as a value (possible values: "").
Even if I'm passing an object or an id...
I'd like to add an option in my autocomplete field (maybe with ChoiceFormField::addChoice()) but I'm facing the LogicException...
As temporal, ugly workaround, you can add the new options this way (this should be done before submitting the form):
\Closure::bind(function () {
// add here the new options e.g.
$this->options[] = ['value' => '1'];
}, $field, ChoiceFormField::class)();
Being $field an instance of ChoiceFormField.
I also have the same issue. As a workaround, Instead of using submitForm(), I do POST requests and it works.
See https://github.com/symfony/ux/blob/2.x/src/Autocomplete/tests/Functional/AutocompleteFormRenderingTest.php for an example with zenstruck/browser.