FormProcessorTestCase::testProcessor()   B
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 16
nc 3
nop 0
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Tests\Unit\Processor;
4
5
use ScayTrase\Api\Cruds\Adaptors\Symfony\FormProcessor;
6
use ScayTrase\Api\Cruds\Adaptors\Symfony\MappedEntityFormFactory;
7
use ScayTrase\Api\Cruds\Exception\EntityProcessingException;
8
use ScayTrase\Api\Cruds\PublicPropertyMapper;
9
use ScayTrase\Api\Cruds\Tests\WebTestCase;
10
use Symfony\Component\Form\AbstractType;
11
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
12
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
13
use Symfony\Component\Form\Extension\Core\Type\TextType;
14
use Symfony\Component\Form\FormBuilderInterface;
15
16
final class FormProcessorTestCase extends WebTestCase
17
{
18
    public function testProcessor()
19
    {
20
        $factory = $this->getContainer()->get('form.factory');
21
22
        $entity    = new SampleEntity();
23
        $entity->b = null;
24
        $entity->a = null;
25
26
        $processor = new FormProcessor(TestForm::class, [], $factory);
27
        $processor->updateEntity($entity, ['a' => 'a', 'b' => 3]);
28
29
        self::assertSame('a', $entity->a);
30
        self::assertSame(3, $entity->b);
31
32
        $processor->updateEntity($entity, ['a' => 3, 'b' => '5']);
33
34
        self::assertSame('3', $entity->a);
35
        self::assertSame(5, $entity->b);
36
37
        try {
38
            $processor->updateEntity($entity, ['b' => ['a' => [5, 42], 7], 'a' => '5']);
39
40
            self::fail('Should be invalid');
41
        } catch (EntityProcessingException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
42
        }
43
    }
44
45
    public function testProcessorFactory()
46
    {
47
        $factory = $this->getContainer()->get('form.factory');
48
        $mapper  = new PublicPropertyMapper();
49
50
        $entity = new SampleEntity();
51
52
        $processorFactory = new MappedEntityFormFactory($factory, $mapper);
53
        $form             = $processorFactory->createFormForClass(get_class($entity));
54
55
        self::assertTrue($form->has('a'));
56
        self::assertTrue($form->has('b'));
57
        self::assertTrue($form->has('c'));
58
59
        self::assertInstanceOf(IntegerType::class, $form->get('a')->getConfig()->getType()->getInnerType());
60
        self::assertInstanceOf(TextType::class, $form->get('b')->getConfig()->getType()->getInnerType());
61
        self::assertInstanceOf(CollectionType::class, $form->get('c')->getConfig()->getType()->getInnerType());
62
    }
63
}
64
65
class SampleEntity
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
66
{
67
    /** @var int */
68
    public $a;
69
    /** @var string */
70
    public $b;
71
    /** @var int[] */
72
    public $c = [];
73
}
74
75
class TestForm extends AbstractType
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
76
{
77
    public function buildForm(FormBuilderInterface $builder, array $options)
78
    {
79
        $builder->add('a', TextType::class);
80
        $builder->add('b', IntegerType::class);
81
    }
82
}
83