Skip to content

Commit 76de2a2

Browse files
committed
Add basic test for formValues()
1 parent e76bdb9 commit 76de2a2

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
12+
namespace Symfony\UX\LiveComponent\Tests\Fixture\Component;
13+
14+
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;
23+
use Symfony\Component\Form\FormInterface;
24+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
25+
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
26+
use Symfony\UX\LiveComponent\DefaultActionTrait;
27+
28+
/**
29+
* @author Jakub Caban <[email protected]>
30+
*/
31+
#[AsLiveComponent('component6')]
32+
class Component6 extends AbstractController
33+
{
34+
use DefaultActionTrait;
35+
use ComponentWithFormTrait;
36+
37+
private FormBuilderInterface $builder;
38+
39+
public function __construct(FormBuilderInterface $builder)
40+
{
41+
$this->builder = $builder;
42+
}
43+
44+
protected function instantiateForm(): FormInterface
45+
{
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('checkbox', CheckboxType::class)
64+
->add('file', FileType::class)
65+
->add('hidden', HiddenType::class)
66+
;
67+
68+
return $this->builder->getForm();
69+
}
70+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
12+
namespace Symfony\UX\LiveComponent\Tests\Unit\Form;
13+
14+
use Symfony\Component\Form\Test\TypeTestCase;
15+
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component6;
16+
17+
/**
18+
* @author Jakub Caban <[email protected]>
19+
*/
20+
class ComponentWithFormTest extends TypeTestCase
21+
{
22+
23+
public function testFormValues(): void
24+
{
25+
$component = new Component6($this->factory->createBuilder());
26+
27+
$values = $component->getFormValues();
28+
$this->assertSame(
29+
$values,
30+
[
31+
'text' => '',
32+
'textarea' => '',
33+
'range' => '',
34+
'choice' => '',
35+
'choice_expanded' => null,
36+
'checkbox' => null,
37+
'file' => '',
38+
'hidden' => '',
39+
]
40+
);
41+
}
42+
}

0 commit comments

Comments
 (0)