|
1 | 1 | <?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 | + |
2 | 12 | declare(strict_types=1);
|
3 | 13 |
|
4 | 14 | namespace Symfony\UX\LiveComponent\Tests\Functional\EventListener;
|
5 | 15 |
|
6 | 16 | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
7 | 17 | use Symfony\UX\LiveComponent\LiveComponentHydrator;
|
8 | 18 | use Symfony\UX\LiveComponent\Tests\ContainerBC;
|
9 |
| -use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2; |
| 19 | +use Symfony\UX\LiveComponent\Tests\Fixture\Component\FormComponent1; |
10 | 20 | use Symfony\UX\TwigComponent\ComponentFactory;
|
11 | 21 | use Zenstruck\Browser\Response\HtmlResponse;
|
12 | 22 | use Zenstruck\Browser\Test\HasBrowser;
|
13 | 23 | use Zenstruck\Foundry\Test\Factories;
|
14 | 24 | use Zenstruck\Foundry\Test\ResetDatabase;
|
15 | 25 |
|
| 26 | +/** |
| 27 | + * @author Jakub Caban <[email protected]> |
| 28 | + */ |
16 | 29 | class ComponentWithFormTest extends KernelTestCase
|
17 | 30 | {
|
18 | 31 | use ContainerBC;
|
19 | 32 | use Factories;
|
20 | 33 | use HasBrowser;
|
21 | 34 | use ResetDatabase;
|
22 | 35 |
|
23 |
| - public function testRenderFormComponent(): void |
| 36 | + public function testHandleCheckboxChanges(): void |
24 | 37 | {
|
25 | 38 | /** @var LiveComponentHydrator $hydrator */
|
26 | 39 | $hydrator = self::getContainer()->get('ux.live_component.component_hydrator');
|
27 | 40 |
|
28 | 41 | /** @var ComponentFactory $factory */
|
29 | 42 | $factory = self::getContainer()->get('ux.twig_component.component_factory');
|
30 | 43 |
|
31 |
| - /** @var Component2 $component */ |
| 44 | + /** @var FormComponent1 $component */ |
32 | 45 | $component = $factory->create('form_component1');
|
33 | 46 |
|
34 | 47 | $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 | + ); |
36 | 65 |
|
37 | 66 | $this->browser()
|
38 | 67 | ->throwExceptions()
|
39 | 68 | ->get('/_components/form_component1?'.http_build_query($dehydrated))
|
40 | 69 | ->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']); |
45 | 117 | })
|
46 | 118 | ;
|
47 | 119 | }
|
|
0 commit comments