Passed
Pull Request — master (#21)
by Pavel
06:33
created

FormProcessorTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 10
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B se::testProcessor() 0 26 2
A se::testProcessorFactory() 0 18 1
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 ScayTrase\Api\Cruds\Tests\Fixtures\SymfonySerializer\SymfonyTestKernel;
11
use ScayTrase\Api\Cruds\Tests\StaticKernelTestTrait;
12
use Symfony\Component\Form\AbstractType;
13
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
14
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
15
use Symfony\Component\Form\Extension\Core\Type\TextType;
16
use Symfony\Component\Form\FormBuilderInterface;
17
18
class FormProcessorTestCase extends WebTestCase
19
{
20
    public function testProcessor()
21
    {
22
        $factory = $this->getContainer()->get('form.factory');
23
24
        $entity    = new SampleEntity();
25
        $entity->b = null;
26
        $entity->a = null;
27
28
        $processor = new FormProcessor(TestForm::class, [], $factory);
29
        $processor->updateEntity($entity, ['a' => 'a', 'b' => 3]);
30
31
        self::assertSame('a', $entity->a);
32
        self::assertSame(3, $entity->b);
33
34
        $processor->updateEntity($entity, ['a' => 3, 'b' => '5']);
35
36
        self::assertSame('3', $entity->a);
37
        self::assertSame(5, $entity->b);
38
39
        try {
40
            $processor->updateEntity($entity, ['b' => ['a' => [5, 42], 7], 'a' => '5']);
41
42
            self::fail('Should be invalid');
43
        } catch (EntityProcessingException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
44
        }
45
    }
46
47
    public function testProcessorFactory()
48
    {
49
        $factory = $this->getContainer()->get('form.factory');
50
        $mapper  = new PublicPropertyMapper();
51
52
        $entity = new SampleEntity();
53
54
        $processorFactory = new MappedEntityFormFactory($factory, $mapper);
55
        $form             = $processorFactory->createFormForClass(get_class($entity));
56
57
        self::assertTrue($form->has('a'));
58
        self::assertTrue($form->has('b'));
59
        self::assertTrue($form->has('c'));
60
61
        self::assertInstanceOf(IntegerType::class, $form->get('a')->getConfig()->getType()->getInnerType());
62
        self::assertInstanceOf(TextType::class, $form->get('b')->getConfig()->getType()->getInnerType());
63
        self::assertInstanceOf(CollectionType::class, $form->get('c')->getConfig()->getType()->getInnerType());
64
    }
65
}
66
67
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...
68
{
69
    /** @var int */
70
    public $a;
71
    /** @var string */
72
    public $b;
73
    /** @var int[] */
74
    public $c = [];
75
}
76
77
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...
78
{
79
    public function buildForm(FormBuilderInterface $builder, array $options)
80
    {
81
        $builder->add('a', TextType::class);
82
        $builder->add('b', IntegerType::class);
83
    }
84
}
85