Skip to content

Commit 03b8a2f

Browse files
committed
[Autocomplete] Add a test to make sure extra attributes are properly merged with value and text
1 parent cb5e2aa commit 03b8a2f

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter;
6+
7+
class CustomAttributesProductAutocompleter extends CustomProductAutocompleter
8+
{
9+
public function getAttributes(object $entity): array
10+
{
11+
return ['disabled' => true];
12+
}
13+
}

src/Autocomplete/tests/Fixtures/Kernel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
3535
use Symfony\UX\Autocomplete\AutocompleteBundle;
3636
use Symfony\UX\Autocomplete\DependencyInjection\AutocompleteFormTypePass;
37+
use Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter\CustomAttributesProductAutocompleter;
3738
use Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter\CustomGroupByProductAutocompleter;
3839
use Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter\CustomProductAutocompleter;
3940
use Symfony\UX\Autocomplete\Tests\Fixtures\Form\ProductType;
@@ -176,6 +177,13 @@ protected function configureContainer(ContainerConfigurator $c): void
176177
'alias' => 'custom_group_by_product',
177178
]);
178179

180+
$services->set(CustomAttributesProductAutocompleter::class)
181+
->public()
182+
->arg(1, new Reference('ux.autocomplete.entity_search_util'))
183+
->tag(AutocompleteFormTypePass::ENTITY_AUTOCOMPLETER_TAG, [
184+
'alias' => 'custom_attributes_product',
185+
]);
186+
179187
$services->alias('public.results_executor', 'ux.autocomplete.results_executor')
180188
->public();
181189

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Symfony package.
7+
*
8+
* (c) Fabien Potencier <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Symfony\UX\Autocomplete\Tests\Integration;
15+
16+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
17+
use Symfony\UX\Autocomplete\AutocompleteResultsExecutor;
18+
use Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter\CustomAttributesProductAutocompleter;
19+
use Symfony\UX\Autocomplete\Tests\Fixtures\Factory\ProductFactory;
20+
use Symfony\UX\Autocomplete\Tests\Fixtures\Kernel;
21+
use Zenstruck\Foundry\Test\Factories;
22+
use Zenstruck\Foundry\Test\ResetDatabase;
23+
24+
class AutocompleteResultsExecutorTest extends KernelTestCase
25+
{
26+
use Factories;
27+
use ResetDatabase;
28+
29+
public function testItReturnsExtraAttributes(): void
30+
{
31+
$kernel = new Kernel('test', true);
32+
$kernel->disableForms();
33+
$kernel->boot();
34+
35+
$product = ProductFactory::createOne(['name' => 'Foo']);
36+
37+
/** @var AutocompleteResultsExecutor $executor */
38+
$executor = $kernel->getContainer()->get('public.results_executor');
39+
$autocompleter = $kernel->getContainer()->get(CustomAttributesProductAutocompleter::class);
40+
$data = $executor->fetchResults($autocompleter, '', 1);
41+
$this->assertCount(1, $data->results);
42+
$this->assertSame(['disabled' => true, 'value' => $product->getId(), 'text' => 'Foo'], $data->results[0]);
43+
}
44+
}

0 commit comments

Comments
 (0)