Skip to content

Commit 9c528ef

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

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\UX\Autocomplete\Tests\Integration;
6+
7+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
8+
use Symfony\UX\Autocomplete\AutocompleteResultsExecutor;
9+
use Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter\CustomAttributesProductAutocompleter;
10+
use Symfony\UX\Autocomplete\Tests\Fixtures\Factory\ProductFactory;
11+
use Symfony\UX\Autocomplete\Tests\Fixtures\Kernel;
12+
use Zenstruck\Foundry\Test\Factories;
13+
use Zenstruck\Foundry\Test\ResetDatabase;
14+
15+
class AutocompleteResultsExecutorTest extends KernelTestCase
16+
{
17+
use Factories;
18+
use ResetDatabase;
19+
20+
public function testItReturnsExtraAttributes(): void
21+
{
22+
$kernel = new Kernel('test', true);
23+
$kernel->disableForms();
24+
$kernel->boot();
25+
26+
$product = ProductFactory::createOne(['name' => 'Foo']);
27+
28+
/** @var AutocompleteResultsExecutor $executor */
29+
$executor = $kernel->getContainer()->get('public.results_executor');
30+
$autocompleter = $kernel->getContainer()->get(CustomAttributesProductAutocompleter::class);
31+
$data = $executor->fetchResults($autocompleter, '', 1);
32+
$this->assertCount(1, $data->results);
33+
$this->assertSame(['disabled' => true, 'value' => $product->getId(), 'text' => 'Foo'], $data->results[0]);
34+
}
35+
}

0 commit comments

Comments
 (0)