ElementAnnotationsListenerTest   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 4
dl 0
loc 324
rs 10
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testEventsWithNoMetadata() 0 7 1
A testToOne() 0 16 1
A testToOneMergesOptions() 0 14 1
A testToOneHasNoEffectOnToMany() 0 11 1
A testToManyHasNoEffectOnToOne() 0 11 1
A testToMany() 0 20 1
A testToManyMergesOptions() 0 14 1
A testHandleExcludeAssociation() 0 11 1
A testHandleFilterField() 0 14 1
A testHandlRequiredAssociation() 0 14 1
A testHandlRequiredAssociationSetsNullOption() 0 16 1
A testHandleRequiredField() 0 18 1
A testHandleRequiredFieldNonFieldProperty() 0 8 1
A testHandleTypeField() 0 14 1
A testHandlevalidatorField() 0 18 2
A eventValidatorProvider() 0 18 1
A eventFilterProvider() 0 16 1
A eventTypeProvider() 0 16 1
A eventNameProvider() 0 16 1
A getMetadataEvent() 0 8 1
1
<?php
2
3
namespace DoctrineORMModuleTest\Form;
4
5
use ArrayObject;
6
use DoctrineORMModule\Form\Annotation\ElementAnnotationsListener;
7
use DoctrineORMModuleTest\Framework\TestCase;
8
use Laminas\EventManager\Event;
9
10
class ElementAnnotationsListenerTest extends TestCase
11
{
12
    /**
13
     * @var ElementAnnotationsListener
14
     */
15
    protected $listener;
16
17
    public function setUp() : void
18
    {
19
        $this->listener = new ElementAnnotationsListener($this->getEntityManager());
20
    }
21
22
    /**
23
     * @dataProvider eventNameProvider
24
     */
25
    public function testEventsWithNoMetadata($method)
26
    {
27
        $event = $this->getMetadataEvent();
28
        $this->listener->{$method}($event);
29
30
        $this->addToAssertionCount(1);
0 ignored issues
show
Bug introduced by
The method addToAssertionCount() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
    }
32
33
    public function testToOne()
34
    {
35
        $listener = $this->listener;
36
        $event    = $this->getMetadataEvent();
37
38
        $event->setParam('name', 'targetOne');
39
        $listener->handleToOne($event);
40
41
        $elementSpec = $event->getParam('elementSpec');
42
        $this->assertEquals($this->getEntityManager(), $elementSpec['spec']['options']['object_manager']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
            \DoctrineORMModuleTest\Assets\Entity\TargetEntity::class,
45
            $elementSpec['spec']['options']['target_class']
46
        );
47
        $this->assertEquals(\DoctrineORMModule\Form\Element\EntitySelect::class, $elementSpec['spec']['type']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
48
    }
49
50
    public function testToOneMergesOptions()
51
    {
52
        $listener    = $this->listener;
53
        $event       = $this->getMetadataEvent();
54
        $elementSpec = new ArrayObject();
55
        $elementSpec['spec']['options']['foo'] = 'bar';
56
57
        $event->setParam('name', 'targetOne');
58
        $event->setParam('elementSpec', $elementSpec);
59
        $listener->handleToOne($event);
60
61
        $this->assertEquals('bar', $elementSpec['spec']['options']['foo']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
        $this->assertCount(3, $elementSpec['spec']['options']);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
    }
64
65
    public function testToOneHasNoEffectOnToMany()
66
    {
67
        $listener = $this->listener;
68
        $event    = $this->getMetadataEvent();
69
70
        $event->setParam('name', 'targetMany');
71
        $listener->handleToOne($event);
72
73
        $this->assertNull($event->getParam('elementSpec'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
        $this->assertNull($event->getParam('inputSpec'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
    }
76
77
    public function testToManyHasNoEffectOnToOne()
78
    {
79
        $listener = $this->listener;
80
        $event    = $this->getMetadataEvent();
81
82
        $event->setParam('name', 'targetOne');
83
        $listener->handleToMany($event);
84
85
        $this->assertNull($event->getParam('elementSpec'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        $this->assertNull($event->getParam('inputSpec'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
    }
88
89
    public function testToMany()
90
    {
91
        $listener = $this->listener;
92
        $event    = $this->getMetadataEvent();
93
94
        $event->setParam('name', 'targetMany');
95
        $listener->handleToMany($event);
96
97
        $elementSpec = $event->getParam('elementSpec');
98
        $inputSpec   = $event->getParam('inputSpec');
99
100
        $this->assertTrue($elementSpec['spec']['attributes']['multiple']);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
        $this->assertEquals($this->getEntityManager(), $elementSpec['spec']['options']['object_manager']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
            \DoctrineORMModuleTest\Assets\Entity\FormEntityTarget::class,
104
            $elementSpec['spec']['options']['target_class']
105
        );
106
        $this->assertEquals(\DoctrineORMModule\Form\Element\EntitySelect::class, $elementSpec['spec']['type']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        $this->assertFalse($inputSpec['required']);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
    }
109
110
    public function testToManyMergesOptions()
111
    {
112
        $listener    = $this->listener;
113
        $event       = $this->getMetadataEvent();
114
        $elementSpec = new ArrayObject();
115
        $elementSpec['spec']['options']['foo'] = 'bar';
116
117
        $event->setParam('name', 'targetMany');
118
        $event->setParam('elementSpec', $elementSpec);
119
        $listener->handleToMany($event);
120
121
        $this->assertEquals('bar', $elementSpec['spec']['options']['foo']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
        $this->assertCount(3, $elementSpec['spec']['options']);
0 ignored issues
show
Bug introduced by
The method assertCount() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
    }
124
125
    public function testHandleExcludeAssociation()
126
    {
127
        $listener = $this->listener;
128
        $event    = $this->getMetadataEvent();
129
        $event->setParam('name', 'targetMany');
130
131
        $this->assertTrue($listener->handleExcludeAssociation($event));
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133
        $event->setParam('name', 'targetOne');
134
        $this->assertFalse($listener->handleExcludeAssociation($event));
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
135
    }
136
137
    /**
138
     * @dataProvider eventFilterProvider
139
     */
140
    public function testHandleFilterField($name, $type)
141
    {
142
        $listener = $this->listener;
143
        $event    = $this->getMetadataEvent();
144
145
        $listener->handleFilterField($event);
146
        $this->assertNull($event->getParam('inputSpec'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
148
        $event->setParam('name', $name);
149
        $listener->handleFilterField($event);
150
151
        $inputSpec = $event->getParam('inputSpec');
152
        $this->assertEquals($type, $inputSpec['filters'][0]['name']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
153
    }
154
155
    public function testHandlRequiredAssociation()
156
    {
157
        $listener = $this->listener;
158
        $event    = $this->getMetadataEvent();
159
160
        $listener->handleRequiredAssociation($event);
161
        $this->assertNull($event->getParam('inputSpec'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
162
163
        $event->setParam('name', 'targetMany');
164
        $listener->handleRequiredAssociation($event);
165
166
        $inputSpec = $event->getParam('inputSpec');
167
        $this->assertFalse($inputSpec['required']);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
168
    }
169
170
    public function testHandlRequiredAssociationSetsNullOption()
171
    {
172
        $listener = $this->listener;
173
        $event    = $this->getMetadataEvent();
174
        $event->setParam('name', 'targetOneNullable');
175
        $listener->handleRequiredAssociation($event);
176
177
        $elementSpec = $event->getParam('elementSpec');
178
179
        $this->assertEquals('NULL', $elementSpec['spec']['options']['empty_option']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
180
181
        $listener->handleRequiredAssociation($event);
182
        $elementSpec['spec']['options']['empty_option'] = 'foo';
183
184
        $this->assertEquals('foo', $elementSpec['spec']['options']['empty_option']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
185
    }
186
187
    public function testHandleRequiredField()
188
    {
189
        $listener = $this->listener;
190
        $event    = $this->getMetadataEvent();
191
192
        $event->setParam('name', 'datetime');
193
        $listener->handleRequiredField($event);
194
        $inputSpec = $event->getParam('inputSpec');
195
        $this->assertTrue($inputSpec['required']);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
197
        $event->setParam('name', 'string');
198
        $listener->handleRequiredField($event);
199
        $this->assertTrue($inputSpec['required']);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
200
201
        $event->setParam('name', 'stringNullable');
202
        $listener->handleRequiredField($event);
203
        $this->assertFalse($inputSpec['required']);
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
204
    }
205
206
    public function testHandleRequiredFieldNonFieldProperty()
207
    {
208
        $listener = $this->listener;
209
        $event    = $this->getMetadataEvent();
210
        $event->setParam('name', 'targetMany');
211
        $listener->handleRequiredField($event);
212
        $this->assertFalse(isset($inputSpec['required']));
0 ignored issues
show
Bug introduced by
The variable $inputSpec seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
Bug introduced by
The method assertFalse() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
213
    }
214
215
    /**
216
     * @dataProvider eventTypeProvider
217
     */
218
    public function testHandleTypeField($name, $type)
219
    {
220
        $listener = $this->listener;
221
        $event    = $this->getMetadataEvent();
222
223
        $listener->handleFilterField($event);
224
        $this->assertNull($event->getParam('elementSpec'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
225
226
        $event->setParam('name', $name);
227
        $listener->handleTypeField($event);
228
229
        $elementSpec = $event->getParam('elementSpec');
230
        $this->assertEquals($type, $elementSpec['spec']['type']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
231
    }
232
233
    /**
234
     * @dataProvider eventValidatorProvider
235
     */
236
    public function testHandlevalidatorField($name, $type)
237
    {
238
        $listener = $this->listener;
239
        $event    = $this->getMetadataEvent();
240
241
        $listener->handleFilterField($event);
242
        $this->assertNull($event->getParam('inputSpec'));
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
243
244
        $event->setParam('name', $name);
245
        $listener->handleValidatorField($event);
246
247
        $inputSpec = $event->getParam('inputSpec');
248
        if (null === $type) {
249
            $this->assertEmpty($inputSpec['validators']);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
250
        } else {
251
            $this->assertEquals($type, $inputSpec['validators'][0]['name']);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<DoctrineORMModule...nnotationsListenerTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
252
        }
253
    }
254
255
    public function eventValidatorProvider()
256
    {
257
        return [
258
            ['bool', 'InArray'],
259
            ['boolean', 'InArray'],
260
            ['bigint', 'Int'],
261
            ['float', 'Float'],
262
            ['integer', 'Int'],
263
            ['smallint', 'Int'],
264
            ['datetime', null],
265
            ['datetimetz', null],
266
            ['date', null],
267
            ['time', null],
268
            ['string', 'StringLength'],
269
            ['stringNullable', null],
270
            ['text', null],
271
        ];
272
    }
273
274
    public function eventFilterProvider()
275
    {
276
        return [
277
            ['bool', 'Boolean'],
278
            ['boolean', 'Boolean'],
279
            ['bigint', 'Int'],
280
            ['integer', 'Int'],
281
            ['smallint', 'Int'],
282
            ['datetime', 'StringTrim'],
283
            ['datetimetz', 'StringTrim'],
284
            ['date', 'StringTrim'],
285
            ['time', 'StringTrim'],
286
            ['string', 'StringTrim'],
287
            ['text', 'StringTrim'],
288
        ];
289
    }
290
291
    public function eventTypeProvider()
292
    {
293
        return [
294
            ['bool', \Laminas\Form\Element\Checkbox::class],
295
            ['boolean', \Laminas\Form\Element\Checkbox::class],
296
            ['bigint', \Laminas\Form\Element\Number::class],
297
            ['integer', \Laminas\Form\Element\Number::class],
298
            ['smallint', \Laminas\Form\Element\Number::class],
299
            ['datetime', \Laminas\Form\Element\DateTime::class],
300
            ['datetimetz', \Laminas\Form\Element\DateTime::class],
301
            ['date', \Laminas\Form\Element\Date::class],
302
            ['time', \Laminas\Form\Element\Time::class],
303
            ['string', \Laminas\Form\Element::class],
304
            ['text', \Laminas\Form\Element\Textarea::class],
305
        ];
306
    }
307
308
    public function eventNameProvider()
309
    {
310
        return [
311
            [
312
                'handleFilterField',
313
                'handleTypeField',
314
                'handleValidatorField',
315
                'handleRequiredField',
316
                'handleRequiredAssociation',
317
                'handleExcludeField',
318
                'handleExcludeAssociation',
319
                'handleToOne',
320
                'handleToMany',
321
            ],
322
        ];
323
    }
324
325
    protected function getMetadataEvent()
326
    {
327
        $event    = new Event();
328
        $metadata = $this->getEntityManager()->getClassMetadata(\DoctrineORMModuleTest\Assets\Entity\FormEntity::class);
329
        $event->setParam('metadata', $metadata);
330
331
        return $event;
332
    }
333
}
334