Skip to content

Commit 4523fce

Browse files
committed
minor #3230 Add E2E tests for Autocomplete (Kocal)
This PR was merged into the 2.x branch. Discussion ---------- Add E2E tests for Autocomplete | Q | A | -------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Documentation? | no <!-- required for new features, or documentation updates --> | Issues | Fix #3017 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - For new features, provide some code snippets to help understand usage. - Features and deprecations must be submitted against branch main. - Update/add documentation as required (we can help!) - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Also added Foundry to create some entities when installing the E2E App. Commits ------- 64d4336 Add E2E tests for Autocomplete
2 parents 0b77eb4 + 64d4336 commit 4523fce

File tree

18 files changed

+284
-68
lines changed

18 files changed

+284
-68
lines changed

.github/workflows/browser-tests.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ jobs:
7373

7474
- name: Configure E2E app
7575
run: |
76-
echo 'APP_ENV=prod' >> .env.local
77-
echo 'APP_DEBUG=0' >> .env.local
7876
echo 'APP_SECRET=df4c071596e64cc75a349456f2887ae2419ae650' >> .env.local
7977
working-directory: apps/e2e
8078

@@ -83,11 +81,12 @@ jobs:
8381
with:
8482
working-directory: apps/e2e
8583
dependency-versions: highest
86-
composer-options: --no-dev
8784
custom-cache-suffix: symfony-${{ matrix.symfony }}
8885

8986
- name: Prepare E2E app
9087
run: |
88+
echo 'APP_ENV=prod' >> .env.local
89+
echo 'APP_DEBUG=0' >> .env.local
9190
symfony composer dump-autoload --classmap-authoritative --no-dev
9291
symfony composer dump-env
9392
symfony console asset-map:compile

apps/e2e/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ APP_SECRET=
2323
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
2424
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
2525
#
26-
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data_%kernel.environment%.db"
26+
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data.db"
2727
# DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=8.0.32&charset=utf8mb4"
2828
# DATABASE_URL="mysql://app:[email protected]:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4"
2929
# DATABASE_URL="postgresql://app:[email protected]:5432/app?serverVersion=16&charset=utf8"

apps/e2e/composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"auto-scripts": {
88
"cache:clear": "symfony-cmd",
99
"assets:install %PUBLIC_DIR%": "symfony-cmd",
10-
"importmap:install": "symfony-cmd"
10+
"importmap:install": "symfony-cmd",
11+
"foundry:load-fixtures": "symfony-cmd"
1112
},
1213
"post-install-cmd": [
1314
"@auto-scripts"
@@ -69,7 +70,8 @@
6970
"symfony/debug-bundle": "6.4.*|7.3.*",
7071
"symfony/maker-bundle": "^1.64",
7172
"symfony/stopwatch": "6.4.*|7.3.*",
72-
"symfony/web-profiler-bundle": "6.4.*|7.3.*"
73+
"symfony/web-profiler-bundle": "6.4.*|7.3.*",
74+
"zenstruck/foundry": "^2.8"
7375
},
7476
"config": {
7577
"platform": {

apps/e2e/config/bundles.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@
3030
Symfony\UX\Translator\UxTranslatorBundle::class => ['all' => true],
3131
Symfony\UX\Typed\TypedBundle::class => ['all' => true],
3232
Symfony\UX\Vue\VueBundle::class => ['all' => true],
33+
Zenstruck\Foundry\ZenstruckFoundryBundle::class => ['dev' => true, 'test' => true],
3334
];
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
when@dev: &dev
2+
# See full configuration: https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#full-default-bundle-configuration
3+
zenstruck_foundry:
4+
persistence:
5+
# Flush only once per call of `PersistentObjectFactory::create()`
6+
flush_once: true
7+
8+
# If you use the `make:factory --test` command, you may need to uncomment the following.
9+
# See https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#generate
10+
#services:
11+
# App\Tests\Factory\:
12+
# resource: '%kernel.project_dir%/tests/Factory/'
13+
# autowire: true
14+
# autoconfigure: true
15+
16+
when@test: *dev

apps/e2e/src/Controller/AutocompleteController.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,66 @@
22

33
namespace App\Controller;
44

5-
use App\Form\Type\AutocompleteSelectType;
5+
use App\Form\FruitAutocompleteField;
66
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
8+
use Symfony\Component\HttpFoundation\Response;
79
use Symfony\Component\Routing\Attribute\Route;
810

911
#[Route('/ux-autocomplete')]
1012
final class AutocompleteController extends AbstractController
1113
{
1214
#[Route('/without-ajax')]
13-
public function index()
15+
public function withoutAjax(): Response
1416
{
15-
$form = $this->createForm(AutocompleteSelectType::class);
17+
$formBuilder = $this->createFormBuilder();
18+
$formBuilder->add('favorite_fruit', ChoiceType::class, [
19+
'autocomplete' => true,
20+
'label' => 'Your favorite fruit:',
21+
'choices' => [
22+
'Apple' => 'apple',
23+
'Banana' => 'banana',
24+
'Cherry' => 'cherry',
25+
'Coconut' => 'coconut',
26+
'Grape' => 'grape',
27+
'Kiwi' => 'kiwi',
28+
'Lemon' => 'lemon',
29+
'Mango' => 'mango',
30+
'Orange' => 'orange',
31+
'Papaya' => 'papaya',
32+
'Peach' => 'peach',
33+
'Pineapple' => 'pineapple',
34+
'Pear' => 'pear',
35+
'Pomegranate' => 'pomegranate',
36+
'Pomelo' => 'pomelo',
37+
'Raspberry' => 'raspberry',
38+
'Strawberry' => 'strawberry',
39+
'Watermelon' => 'watermelon',
40+
],
41+
]);
1642

17-
return $this->render(
18-
'ux_autocomplete/index.html.twig',
19-
['form' => $form->createView()]
20-
);
43+
$form = $formBuilder->getForm();
44+
45+
return $this->render('ux_autocomplete/without_ajax.html.twig', [
46+
'form' => $form->createView()
47+
]);
48+
}
49+
50+
#[Route('/with-ajax')]
51+
public function withAjax(): Response
52+
{
53+
$formBuilder = $this->createFormBuilder();
54+
$formBuilder->add('favorite_fruit', FruitAutocompleteField::class);
55+
56+
$form = $formBuilder->getForm();
57+
58+
return $this->render('ux_autocomplete/with_ajax.html.twig', [
59+
'form' => $form->createView()
60+
]);
2161
}
2262

2363
#[Route('/custom-controller')]
24-
public function customController()
64+
public function customController(): Response
2565
{
2666
return $this->render('ux_autocomplete/custom_controller.html.twig');
2767
}

apps/e2e/src/Entity/Fruit.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use App\Repository\FruitRepository;
6+
use Doctrine\ORM\Mapping as ORM;
7+
8+
#[ORM\Entity(repositoryClass: FruitRepository::class)]
9+
class Fruit
10+
{
11+
#[ORM\Id]
12+
#[ORM\Column(length: 64, nullable: false)]
13+
private string $id;
14+
15+
#[ORM\Column(length: 64, nullable: false)]
16+
private string $name;
17+
18+
public static function create(string $id, string $name): self
19+
{
20+
$fruit = new self();
21+
$fruit->id = $id;
22+
$fruit->name = $name;
23+
24+
return $fruit;
25+
}
26+
27+
public function getId(): string
28+
{
29+
return $this->id;
30+
}
31+
32+
public function getName(): string
33+
{
34+
return $this->name;
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Factory;
4+
5+
use App\Entity\Fruit;
6+
use Zenstruck\Foundry\Object\Instantiator;
7+
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
8+
9+
/**
10+
* @extends PersistentProxyObjectFactory<Fruit>
11+
*/
12+
final class FruitFactory extends PersistentProxyObjectFactory
13+
{
14+
public static function class(): string
15+
{
16+
return Fruit::class;
17+
}
18+
19+
/**
20+
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
21+
*/
22+
protected function defaults(): array|callable
23+
{
24+
return [];
25+
}
26+
27+
/**
28+
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
29+
*/
30+
protected function initialize(): static
31+
{
32+
return $this
33+
->instantiateWith(Instantiator::namedConstructor('create'))
34+
;
35+
}
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Form;
4+
5+
use App\Entity\Fruit;
6+
use Symfony\Component\Form\AbstractType;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
8+
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
9+
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;
10+
11+
#[AsEntityAutocompleteField]
12+
class FruitAutocompleteField extends AbstractType
13+
{
14+
public function configureOptions(OptionsResolver $resolver): void
15+
{
16+
$resolver->setDefaults([
17+
'class' => Fruit::class,
18+
'placeholder' => 'Choose a Fruit',
19+
'choice_value' => static fn (?Fruit $fruit) => $fruit?->getId(),
20+
'choice_label' => static fn (Fruit $fruit) => $fruit->getName(),
21+
]);
22+
}
23+
24+
public function getParent(): string
25+
{
26+
return BaseEntityAutocompleteType::class;
27+
}
28+
}

apps/e2e/src/Form/Type/AutocompleteSelectType.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)