Completed
Pull Request — master (#516)
by Gary
09:31
created

testEnsureDateSelectElementCanHydrate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 2
eloc 19
nc 2
nop 0
1
<?php
2
3
namespace DoctrineORMModuleTest\Form;
4
5
use DateTime;
6
use DoctrineORMModule\Form\Annotation\AnnotationBuilder;
7
use DoctrineORMModule\Stdlib\Hydrator\DoctrineEntity;
8
use DoctrineORMModuleTest\Assets\Entity\FormEntity;
9
use DoctrineORMModuleTest\Assets\Entity\FormDateSelect;
10
use DoctrineORMModuleTest\Assets\Entity\Issue237;
11
use DoctrineORMModuleTest\Framework\TestCase;
12
use Zend\Form\Annotation\AnnotationBuilder as ZendAnnotationBuilder;
13
14
class AnnotationBuilderTest extends TestCase
15
{
16
    /**
17
     * @var AnnotationBuilder
18
     */
19
    protected $builder;
20
21
    public function setUp()
22
    {
23
        $this->builder = new AnnotationBuilder($this->getEntityManager());
24
    }
25
26
    /**
27
     * @covers \DoctrineORMModule\Form\Annotation\AnnotationBuilder::getFormSpecification
28
     * @link   https://github.com/doctrine/DoctrineORMModule/issues/237
29
     */
30
    public function testIssue237()
31
    {
32
        $entity = new Issue237();
33
        $spec   = $this->builder->getFormSpecification($entity);
34
35
        $this->assertCount(0, $spec['elements']);
36
    }
37
38
    /**
39
     * @covers \DoctrineORMModule\Form\Annotation\AnnotationBuilder::getFormSpecification
40
     */
41
    public function testAnnotationBuilderSupportsClassNames()
42
    {
43
        $spec = $this->builder->getFormSpecification('DoctrineORMModuleTest\\Assets\\Entity\\Issue237');
44
45
        $this->assertCount(0, $spec['elements'], 'Annotation builder allows also class names');
46
    }
47
48
    /**
49
     * empty_option behavior - !isset can't evaluate null value
50
     * @link https://github.com/doctrine/DoctrineORMModule/pull/247
51
     */
52
    public function testEmptyOptionNullDoesntGenerateValue()
53
    {
54
        $showEmptyValue = true;
55
        $entity         = new FormEntity();
56
        $spec           = $this->builder->getFormSpecification($entity);
57
58
        foreach ($spec['elements'] as $elementSpec) {
59
            if (isset($elementSpec['spec']['options'])) {
60
                if (array_key_exists('empty_option', $elementSpec['spec']['options']) &&
61
                    $elementSpec['spec']['options']['empty_option'] === null
62
                ) {
63
                    $showEmptyValue = false;
64
                    break;
65
                }
66
            }
67
        }
68
        $this->assertFalse($showEmptyValue);
69
    }
70
71
    /**
72
     * Ensure user defined \Type or type attribute overrides the listener one
73
     */
74
    public function testEnsureCustomTypeOrAttributeTypeIsUsedInAnnotations()
75
    {
76
        $userDefinedTypeOverridesListenerType = true;
77
        $entity                               = new FormEntity();
78
79
        $zendAnnotationBuilder = new ZendAnnotationBuilder();
80
        $zendForm              = $zendAnnotationBuilder->createForm($entity);
81
82
        $spec           = $this->builder->getFormSpecification($entity);
83
        $annotationForm = $this->builder->createForm($entity);
84
85
        $attributesToTest = ['specificType', 'specificMultiType', 'specificAttributeType'];
86
87
        foreach ($spec['elements'] as $element) {
88
            $elementName = $element['spec']['name'];
89
            if (in_array($elementName, $attributesToTest)) {
90
                $annotationFormElement = $annotationForm->get($elementName);
91
                $zendFormElement       = $zendForm->get($elementName);
92
93
                $annotationElementAttribute = $annotationFormElement->getAttribute('type');
94
                $zendElementAttribute       = $zendFormElement->getAttribute('type');
95
96
                if ((get_class($zendFormElement) !== get_class($annotationFormElement)) ||
97
                    ($annotationElementAttribute !== $zendElementAttribute)
98
                ) {
99
                    $userDefinedTypeOverridesListenerType = false;
100
                }
101
            }
102
        }
103
        $this->assertTrue($userDefinedTypeOverridesListenerType);
104
    }
105
    
106
    /**
107
     * @link https://github.com/doctrine/DoctrineORMModule/issues/515
108
     */
109
    public function testEnsureDateSelectElementCanHydrate()
110
    {
111
        // Create form
112
        $now      = new DateTime(date('Y-m-d'));
113
        $entity   = new FormDateSelect;
114
        $hydrator = new DoctrineEntity($this->getEntityManager());
0 ignored issues
show
Deprecated Code introduced by
The class DoctrineORMModule\Stdlib\Hydrator\DoctrineEntity has been deprecated with message: will be removed in 0.8.0, use DoctrineModule\Stdlib\Hydrator\DoctrineObject instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
115
        $form     = $this->builder->createForm($entity);
116
        $form->setHydrator($hydrator);
117
        $form->bind($entity);
118
        
119
        // Set data for the DateSelect form element
120
        $form->setData([
121
            'date' => [
122
                'day'   => $now->format('d'),
123
                'month' => $now->format('m'),
124
                'year'  => $now->format('Y'),
125
            ],
126
        ]);
127
        
128
        // Clean entity
129
        $isValid = true;
130
        if (!$form->isValid()) {
131
            $isValid = false;
132
        }
133
        $cleanEntity = $form->getData();
134
        
135
        $this->assertTrue($isValid);
136
        $this->assertInstanceOf('DateTime', $cleanEntity->getDate());
137
        $this->assertEquals($now, $cleanEntity->getDate()->getTimestamp());
138
    }
139
140
    /**
141
     * @link https://github.com/zendframework/zf2/issues/7096
142
     */
143
    public function testFileTypeDoesntGrabStringLengthValidator()
144
    {
145
        $validators = $this
146
            ->builder
147
            ->createForm(new FormEntity())
148
            ->getInputFilter()
149
            ->get('image')
150
            ->getValidatorChain()
151
            ->getValidators();
152
153
        $this->assertEquals(0, count($validators));
154
    }
155
156
    /**
157
     * Ensure prefer_form_input_filter is set to true for the generated form
158
     */
159
    public function testPreferFormInputFilterIsTrue()
160
    {
161
        $entity = new FormEntity();
162
        $form   = $this->builder->createForm($entity);
163
        $this->assertTrue($form->getPreferFormInputFilter());
164
    }
165
}
166