Skip to content

Commit

Permalink
[Autocomplete] Add a test to make sure extra attributes are properly …
Browse files Browse the repository at this point in the history
…merged with value and text
  • Loading branch information
J-Ben87 committed Feb 1, 2025
1 parent f798f73 commit 9c528ef
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter;

class CustomAttributesProductAutocompleter extends CustomProductAutocompleter
{
public function getAttributes(object $entity): array
{
return ['disabled' => true];
}
}
8 changes: 8 additions & 0 deletions src/Autocomplete/tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\UX\Autocomplete\AutocompleteBundle;
use Symfony\UX\Autocomplete\DependencyInjection\AutocompleteFormTypePass;
use Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter\CustomAttributesProductAutocompleter;
use Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter\CustomGroupByProductAutocompleter;
use Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter\CustomProductAutocompleter;
use Symfony\UX\Autocomplete\Tests\Fixtures\Form\ProductType;
Expand Down Expand Up @@ -176,6 +177,13 @@ protected function configureContainer(ContainerConfigurator $c): void
'alias' => 'custom_group_by_product',
]);

$services->set(CustomAttributesProductAutocompleter::class)
->public()
->arg(1, new Reference('ux.autocomplete.entity_search_util'))
->tag(AutocompleteFormTypePass::ENTITY_AUTOCOMPLETER_TAG, [
'alias' => 'custom_attributes_product',
]);

$services->alias('public.results_executor', 'ux.autocomplete.results_executor')
->public();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Symfony\UX\Autocomplete\Tests\Integration;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\Autocomplete\AutocompleteResultsExecutor;
use Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter\CustomAttributesProductAutocompleter;
use Symfony\UX\Autocomplete\Tests\Fixtures\Factory\ProductFactory;
use Symfony\UX\Autocomplete\Tests\Fixtures\Kernel;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

class AutocompleteResultsExecutorTest extends KernelTestCase
{
use Factories;
use ResetDatabase;

public function testItReturnsExtraAttributes(): void
{
$kernel = new Kernel('test', true);
$kernel->disableForms();
$kernel->boot();

$product = ProductFactory::createOne(['name' => 'Foo']);

/** @var AutocompleteResultsExecutor $executor */
$executor = $kernel->getContainer()->get('public.results_executor');
$autocompleter = $kernel->getContainer()->get(CustomAttributesProductAutocompleter::class);
$data = $executor->fetchResults($autocompleter, '', 1);
$this->assertCount(1, $data->results);
$this->assertSame(['disabled' => true, 'value' => $product->getId(), 'text' => 'Foo'], $data->results[0]);
}
}

0 comments on commit 9c528ef

Please sign in to comment.