Completed
Push — master ( 50808e...8a6350 )
by Gianluca
05:45
created

testAnnotationBuilderSupportsClassNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace DoctrineORMModuleTest\Form;
4
5
use DoctrineORMModule\Form\Annotation\AnnotationBuilder;
6
use DoctrineORMModuleTest\Assets\Entity\FormEntity;
7
use DoctrineORMModuleTest\Assets\Entity\Issue237;
8
use DoctrineORMModuleTest\Framework\TestCase;
9
use Zend\Form\Annotation\AnnotationBuilder as ZendAnnotationBuilder;
10
11
class AnnotationBuilderTest extends TestCase
12
{
13
    /**
14
     * @var AnnotationBuilder
15
     */
16
    protected $builder;
17
18
    public function setUp()
19
    {
20
        $this->builder = new AnnotationBuilder($this->getEntityManager());
21
    }
22
23
    /**
24
     * @covers \DoctrineORMModule\Form\Annotation\AnnotationBuilder::getFormSpecification
25
     * @link   https://github.com/doctrine/DoctrineORMModule/issues/237
26
     */
27
    public function testIssue237()
28
    {
29
        $entity = new Issue237();
30
        $spec   = $this->builder->getFormSpecification($entity);
31
32
        $this->assertCount(0, $spec['elements']);
33
    }
34
35
    /**
36
     * @covers \DoctrineORMModule\Form\Annotation\AnnotationBuilder::getFormSpecification
37
     */
38
    public function testAnnotationBuilderSupportsClassNames()
39
    {
40
        $spec = $this->builder->getFormSpecification('DoctrineORMModuleTest\\Assets\\Entity\\Issue237');
41
42
        $this->assertCount(0, $spec['elements'], 'Annotation builder allows also class names');
43
    }
44
45
    /**
46
     * empty_option behavior - !isset can't evaluate null value
47
     * @link https://github.com/doctrine/DoctrineORMModule/pull/247
48
     */
49
    public function testEmptyOptionNullDoesntGenerateValue()
50
    {
51
        $showEmptyValue = true;
52
        $entity         = new FormEntity();
53
        $spec           = $this->builder->getFormSpecification($entity);
54
55
        foreach ($spec['elements'] as $elementSpec) {
56
            if (isset($elementSpec['spec']['options'])) {
57
                if (array_key_exists('empty_option', $elementSpec['spec']['options']) &&
58
                    $elementSpec['spec']['options']['empty_option'] === null
59
                ) {
60
                    $showEmptyValue = false;
61
                    break;
62
                }
63
            }
64
        }
65
        $this->assertFalse($showEmptyValue);
66
    }
67
68
    /**
69
     * Ensure user defined \Type or type attribute overrides the listener one
70
     */
71
    public function testEnsureCustomTypeOrAttributeTypeIsUsedInAnnotations()
72
    {
73
        $userDefinedTypeOverridesListenerType = true;
74
        $entity                               = new FormEntity();
75
76
        $zendAnnotationBuilder = new ZendAnnotationBuilder();
77
        $zendForm              = $zendAnnotationBuilder->createForm($entity);
78
79
        $spec           = $this->builder->getFormSpecification($entity);
80
        $annotationForm = $this->builder->createForm($entity);
81
82
        $attributesToTest = array('specificType', 'specificMultiType', 'specificAttributeType');
83
84
        foreach ($spec['elements'] as $element) {
85
            $elementName = $element['spec']['name'];
86
            if (in_array($elementName, $attributesToTest)) {
87
                $annotationFormElement = $annotationForm->get($elementName);
88
                $zendFormElement       = $zendForm->get($elementName);
89
90
                $annotationElementAttribute = $annotationFormElement->getAttribute('type');
91
                $zendElementAttribute       = $zendFormElement->getAttribute('type');
92
93
                if ((get_class($zendFormElement) !== get_class($annotationFormElement)) ||
94
                    ($annotationElementAttribute !== $zendElementAttribute)
95
                ) {
96
                    $userDefinedTypeOverridesListenerType = false;
97
                }
98
            }
99
        }
100
        $this->assertTrue($userDefinedTypeOverridesListenerType);
101
    }
102
103
    /**
104
     * @link https://github.com/zendframework/zf2/issues/7096
105
     */
106
    public function testFileTypeDoesntGrabStringLengthValidator()
107
    {
108
        $validators = $this
109
            ->builder
110
            ->createForm(new FormEntity())
111
            ->getInputFilter()
112
            ->get('image')
113
            ->getValidatorChain()
114
            ->getValidators();
115
116
        $this->assertEquals(0, count($validators));
117
    }
118
119
    /**
120
     * Ensure prefer_form_input_filter is set to true for the generated form
121
     */
122
    public function testPreferFormInputFilterIsTrue()
123
    {
124
        $entity = new FormEntity();
125
        $form   = $this->builder->createForm($entity);
126
        $this->assertTrue($form->getPreferFormInputFilter());
127
    }
128
}
129