Skip to content

Commit d183645

Browse files
committed
Add backend web test case for checkboxes
1 parent 66f85e8 commit d183645

File tree

4 files changed

+96
-9
lines changed

4 files changed

+96
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ public function __construct(private FormFactoryInterface $formFactory)
3434

3535
protected function instantiateForm(): FormInterface
3636
{
37-
return $this->formFactory->create(FormType1::class);
37+
return $this->formFactory->createNamed('form', FormType1::class);
3838
}
3939
}

src/LiveComponent/tests/Fixture/Form/FormType1.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
212
declare(strict_types=1);
313

414
namespace Symfony\UX\LiveComponent\Tests\Fixture\Form;
@@ -14,6 +24,9 @@
1424
use Symfony\Component\Form\FormBuilderInterface;
1525
use Symfony\Component\OptionsResolver\OptionsResolver;
1626

27+
/**
28+
* @author Jakub Caban <[email protected]>
29+
*/
1730
class FormType1 extends AbstractType
1831
{
1932
public function buildForm(FormBuilderInterface $builder, array $options)

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

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,119 @@
11
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
212
declare(strict_types=1);
313

414
namespace Symfony\UX\LiveComponent\Tests\Functional\EventListener;
515

616
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
717
use Symfony\UX\LiveComponent\LiveComponentHydrator;
818
use Symfony\UX\LiveComponent\Tests\ContainerBC;
9-
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
19+
use Symfony\UX\LiveComponent\Tests\Fixture\Component\FormComponent1;
1020
use Symfony\UX\TwigComponent\ComponentFactory;
1121
use Zenstruck\Browser\Response\HtmlResponse;
1222
use Zenstruck\Browser\Test\HasBrowser;
1323
use Zenstruck\Foundry\Test\Factories;
1424
use Zenstruck\Foundry\Test\ResetDatabase;
1525

26+
/**
27+
* @author Jakub Caban <[email protected]>
28+
*/
1629
class ComponentWithFormTest extends KernelTestCase
1730
{
1831
use ContainerBC;
1932
use Factories;
2033
use HasBrowser;
2134
use ResetDatabase;
2235

23-
public function testRenderFormComponent(): void
36+
public function testHandleCheckboxChanges(): void
2437
{
2538
/** @var LiveComponentHydrator $hydrator */
2639
$hydrator = self::getContainer()->get('ux.live_component.component_hydrator');
2740

2841
/** @var ComponentFactory $factory */
2942
$factory = self::getContainer()->get('ux.twig_component.component_factory');
3043

31-
/** @var Component2 $component */
44+
/** @var FormComponent1 $component */
3245
$component = $factory->create('form_component1');
3346

3447
$dehydrated = $hydrator->dehydrate($component);
35-
$token = null;
48+
$bareForm = [
49+
'text' => '',
50+
'textarea' => '',
51+
'range' => '',
52+
'choice' => '',
53+
'choice_expanded' => '',
54+
'choice_multiple' => ['2'],
55+
'checkbox_checked' => '1',
56+
'file' => '',
57+
'hidden' => ''
58+
];
59+
$fullBareData = array_merge(
60+
$bareForm,
61+
[
62+
'checkbox' => null
63+
]
64+
);
3665

3766
$this->browser()
3867
->throwExceptions()
3968
->get('/_components/form_component1?'.http_build_query($dehydrated))
4069
->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');
70+
->assertContains('<input type="checkbox" id="form_choice_multiple_1" name="form[choice_multiple][]" value="2" checked="checked" />')
71+
->assertContains('<input type="checkbox" id="form_choice_multiple_0" name="form[choice_multiple][]" value="1" />')
72+
->assertContains('<input type="checkbox" id="form_checkbox" name="form[checkbox]" required="required" value="1" />')
73+
->assertContains('<input type="checkbox" id="form_checkbox_checked" name="form[checkbox_checked]" required="required" value="1" checked="checked" />')
74+
->use(function (HtmlResponse $response) use(&$fullBareData, &$dehydrated, &$bareForm) {
75+
$data = json_decode(
76+
$response->crawler()->filter('div')->first()->attr('data-live-data-value'),
77+
true
78+
);
79+
self::assertEquals($fullBareData, $data['form']);
80+
81+
// check both multiple fields
82+
$bareForm['choice_multiple'] = $fullBareData['choice_multiple'] = ['2', '1'];
83+
84+
$dehydrated['form'] = $bareForm;
85+
})
86+
->get('/_components/form_component1?'.http_build_query($dehydrated))
87+
->assertContains('<input type="checkbox" id="form_choice_multiple_1" name="form[choice_multiple][]" value="2" checked="checked" />')
88+
->assertContains('<input type="checkbox" id="form_choice_multiple_0" name="form[choice_multiple][]" value="1" checked="checked" />')
89+
->use(function (HtmlResponse $response) use(&$fullBareData, &$dehydrated, &$bareForm) {
90+
$data = json_decode(
91+
$response->crawler()->filter('div')->first()->attr('data-live-data-value'),
92+
true
93+
);
94+
self::assertEquals($fullBareData, $data['form']);
95+
96+
// uncheck multiple, check single checkbox
97+
$bareForm['checkbox'] = $fullBareData['checkbox'] = '1';
98+
$bareForm['choice_multiple'] = $fullBareData['choice_multiple'] = [];
99+
100+
// uncheck previously checked checkbox
101+
unset($bareForm['checkbox_checked']);
102+
$fullBareData['checkbox_checked'] = null;
103+
104+
$dehydrated['form'] = $bareForm;
105+
})
106+
->get('/_components/form_component1?'.http_build_query($dehydrated))
107+
->assertContains('<input type="checkbox" id="form_choice_multiple_1" name="form[choice_multiple][]" value="2" />')
108+
->assertContains('<input type="checkbox" id="form_choice_multiple_0" name="form[choice_multiple][]" value="1" />')
109+
->assertContains('<input type="checkbox" id="form_checkbox" name="form[checkbox]" required="required" value="1" checked="checked" />')
110+
->assertContains('<input type="checkbox" id="form_checkbox_checked" name="form[checkbox_checked]" required="required" value="1" />')
111+
->use(function (HtmlResponse $response) use($fullBareData) {
112+
$data = json_decode(
113+
$response->crawler()->filter('div')->first()->attr('data-live-data-value'),
114+
true
115+
);
116+
self::assertEquals($fullBareData, $data['form']);
45117
})
46118
;
47119
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* file that was distributed with this source code.
1010
*/
1111

12+
declare(strict_types=1);
13+
1214
namespace Symfony\UX\LiveComponent\Tests\Unit\Form;
1315

1416
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

0 commit comments

Comments
 (0)