Skip to content

Commit 66f85e8

Browse files
committed
Rework FormComponent1 to use form type and register it in test kernel.
1 parent 546c5a7 commit 66f85e8

File tree

7 files changed

+139
-49
lines changed

7 files changed

+139
-49
lines changed

src/LiveComponent/tests/Fixture/Component/FormComponent1.php

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,9 @@
1111

1212
namespace Symfony\UX\LiveComponent\Tests\Fixture\Component;
1313

14+
use Symfony\UX\LiveComponent\Tests\Fixture\Form\FormType1;
1415
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15-
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
16-
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17-
use Symfony\Component\Form\Extension\Core\Type\FileType;
18-
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
19-
use Symfony\Component\Form\Extension\Core\Type\RangeType;
20-
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
21-
use Symfony\Component\Form\Extension\Core\Type\TextType;
22-
use Symfony\Component\Form\FormBuilderInterface;
16+
use Symfony\Component\Form\FormFactoryInterface;
2317
use Symfony\Component\Form\FormInterface;
2418
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
2519
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
@@ -28,51 +22,18 @@
2822
/**
2923
* @author Jakub Caban <[email protected]>
3024
*/
31-
#[AsLiveComponent('component6')]
25+
#[AsLiveComponent('form_component1')]
3226
class FormComponent1 extends AbstractController
3327
{
3428
use ComponentWithFormTrait;
3529
use DefaultActionTrait;
3630

37-
private FormBuilderInterface $builder;
38-
39-
public function __construct(FormBuilderInterface $builder)
31+
public function __construct(private FormFactoryInterface $formFactory)
4032
{
41-
$this->builder = $builder;
4233
}
4334

4435
protected function instantiateForm(): FormInterface
4536
{
46-
$this->builder
47-
->add('text', TextType::class)
48-
->add('textarea', TextareaType::class)
49-
->add('range', RangeType::class)
50-
->add('choice', ChoiceType::class, [
51-
'choices' => [
52-
'foo' => 1,
53-
'bar' => 2,
54-
],
55-
])
56-
->add('choice_expanded', ChoiceType::class, [
57-
'choices' => [
58-
'foo' => 1,
59-
'bar' => 2,
60-
],
61-
'expanded' => true,
62-
])
63-
->add('choice_multiple', ChoiceType::class, [
64-
'choices' => [
65-
'foo' => 1,
66-
'bar' => 2,
67-
],
68-
'expanded' => true,
69-
'multiple' => true,
70-
])
71-
->add('checkbox', CheckboxType::class)
72-
->add('file', FileType::class)
73-
->add('hidden', HiddenType::class)
74-
;
75-
76-
return $this->builder->getForm();
37+
return $this->formFactory->create(FormType1::class);
7738
}
7839
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Symfony\UX\LiveComponent\Tests\Fixture\Form;
5+
6+
use Symfony\Component\Form\AbstractType;
7+
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
8+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
9+
use Symfony\Component\Form\Extension\Core\Type\FileType;
10+
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
11+
use Symfony\Component\Form\Extension\Core\Type\RangeType;
12+
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
13+
use Symfony\Component\Form\Extension\Core\Type\TextType;
14+
use Symfony\Component\Form\FormBuilderInterface;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
class FormType1 extends AbstractType
18+
{
19+
public function buildForm(FormBuilderInterface $builder, array $options)
20+
{
21+
$builder
22+
->add('text', TextType::class)
23+
->add('textarea', TextareaType::class)
24+
->add('range', RangeType::class)
25+
->add('choice', ChoiceType::class, [
26+
'choices' => [
27+
'foo' => 1,
28+
'bar' => 2,
29+
],
30+
])
31+
->add('choice_expanded', ChoiceType::class, [
32+
'choices' => [
33+
'foo' => 1,
34+
'bar' => 2,
35+
],
36+
'expanded' => true,
37+
])
38+
->add('choice_multiple', ChoiceType::class, [
39+
'choices' => [
40+
'foo' => 1,
41+
'bar' => 2,
42+
],
43+
'expanded' => true,
44+
'multiple' => true,
45+
'data' => [2]
46+
])
47+
->add('checkbox', CheckboxType::class)
48+
->add('checkbox_checked', CheckboxType::class, [
49+
'data' => true
50+
])
51+
->add('file', FileType::class)
52+
->add('hidden', HiddenType::class)
53+
;
54+
}
55+
56+
public function configureOptions(OptionsResolver $resolver)
57+
{
58+
$resolver->setDefaults([
59+
'csrf_protection' => false
60+
]);
61+
}
62+
}

src/LiveComponent/tests/Fixture/Kernel.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
use Symfony\Bundle\TwigBundle\TwigBundle;
1919
use Symfony\Component\Config\Loader\LoaderInterface;
2020
use Symfony\Component\DependencyInjection\ContainerBuilder;
21+
use Symfony\Component\DependencyInjection\Reference;
22+
use Symfony\Component\Form\Extension\Core\Type\FormType;
23+
use Symfony\Component\Form\FormBuilder;
24+
use Symfony\Component\Form\FormBuilderInterface;
25+
use Symfony\Component\Form\FormFactory;
26+
use Symfony\Component\Form\FormFactoryInterface;
2127
use Symfony\Component\HttpFoundation\Response;
2228
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
2329
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
@@ -27,6 +33,7 @@
2733
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
2834
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component3;
2935
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component6;
36+
use Symfony\UX\LiveComponent\Tests\Fixture\Component\FormComponent1;
3037
use Symfony\UX\TwigComponent\TwigComponentBundle;
3138
use Twig\Environment;
3239

@@ -67,13 +74,15 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
6774
$componentB = $c->register(Component2::class)->setAutoconfigured(true)->setAutowired(true);
6875
$componentC = $c->register(Component3::class)->setAutoconfigured(true)->setAutowired(true);
6976
$componentF = $c->register(Component6::class)->setAutoconfigured(true)->setAutowired(true);
77+
$componentFormA = $c->register(FormComponent1::class)->setAutoconfigured(true)->setAutowired(true);
7078

7179
if (self::VERSION_ID < 50300) {
7280
// add tag manually
7381
$componentA->addTag('twig.component', ['key' => 'component1'])->addTag('controller.service_arguments');
7482
$componentB->addTag('twig.component', ['key' => 'component2', 'default_action' => 'defaultAction'])->addTag('controller.service_arguments');
7583
$componentC->addTag('twig.component', ['key' => 'component3'])->addTag('controller.service_arguments');
7684
$componentF->addTag('twig.component', ['key' => 'component6'])->addTag('controller.service_arguments');
85+
$componentFormA->addTag('twig.component', ['key' => 'form_component1'])->addTag('controller.service_arguments');
7786
}
7887

7988
$sessionConfig = self::VERSION_ID < 50300 ? ['storage_id' => 'session.storage.mock_file'] : ['storage_factory_id' => 'session.storage.factory.mock_file'];

src/LiveComponent/tests/Fixture/templates/components/component1.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
Prop1: {{ this.prop1.id }}
55
Prop2: {{ this.prop2|date('Y-m-d g:i') }}
66
Prop3: {{ this.prop3 }}
7-
Prop4: {{ this.prop4|default('(none)') }}
7+
Prop4: {{ thias.prop4|default('(none)') }}
88
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div
2+
{{ init_live_component(this) }}
3+
>
4+
{{ form(this.form) }}
5+
</div>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Symfony\UX\LiveComponent\Tests\Functional\EventListener;
5+
6+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7+
use Symfony\UX\LiveComponent\LiveComponentHydrator;
8+
use Symfony\UX\LiveComponent\Tests\ContainerBC;
9+
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
10+
use Symfony\UX\TwigComponent\ComponentFactory;
11+
use Zenstruck\Browser\Response\HtmlResponse;
12+
use Zenstruck\Browser\Test\HasBrowser;
13+
use Zenstruck\Foundry\Test\Factories;
14+
use Zenstruck\Foundry\Test\ResetDatabase;
15+
16+
class ComponentWithFormTest extends KernelTestCase
17+
{
18+
use ContainerBC;
19+
use Factories;
20+
use HasBrowser;
21+
use ResetDatabase;
22+
23+
public function testRenderFormComponent(): void
24+
{
25+
/** @var LiveComponentHydrator $hydrator */
26+
$hydrator = self::getContainer()->get('ux.live_component.component_hydrator');
27+
28+
/** @var ComponentFactory $factory */
29+
$factory = self::getContainer()->get('ux.twig_component.component_factory');
30+
31+
/** @var Component2 $component */
32+
$component = $factory->create('form_component1');
33+
34+
$dehydrated = $hydrator->dehydrate($component);
35+
$token = null;
36+
37+
$this->browser()
38+
->throwExceptions()
39+
->get('/_components/form_component1?'.http_build_query($dehydrated))
40+
->assertSuccessful()
41+
->assertHeaderContains('Content-Type', 'html')
42+
->use(function (HtmlResponse $response) use (&$token) {
43+
// get a valid token to use for actions
44+
$token = $response->crawler()->filter('div')->first()->attr('data-live-csrf-value');
45+
})
46+
;
47+
}
48+
}

src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@
1111

1212
namespace Symfony\UX\LiveComponent\Tests\Unit\Form;
1313

14-
use Symfony\Component\Form\Test\TypeTestCase;
14+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15+
use Symfony\UX\LiveComponent\Tests\ContainerBC;
1516
use Symfony\UX\LiveComponent\Tests\Fixture\Component\FormComponent1;
1617

1718
/**
1819
* @author Jakub Caban <[email protected]>
1920
*/
20-
class ComponentWithFormTest extends TypeTestCase
21+
class ComponentWithFormTest extends KernelTestCase
2122
{
23+
use ContainerBC;
24+
2225
public function testFormValues(): void
2326
{
24-
$component = new FormComponent1($this->factory->createBuilder());
27+
$formFactory = self::getContainer()->get('form.factory');
28+
$component = new FormComponent1($formFactory);
2529

2630
$this->assertSame(
2731
[
@@ -30,8 +34,9 @@ public function testFormValues(): void
3034
'range' => '',
3135
'choice' => '',
3236
'choice_expanded' => '',
33-
'choice_multiple' => [],
37+
'choice_multiple' => ['2'],
3438
'checkbox' => null,
39+
'checkbox_checked' => '1',
3540
'file' => '',
3641
'hidden' => '',
3742
],

0 commit comments

Comments
 (0)