Completed
Push — feature/EVO-5751-mongodb-3.x ( 5b7d5c...7088ec )
by Lucas
38:47 queued 22:41
created

DefinitionTest::testTextIndexes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * test json definition
4
 */
5
6
namespace Graviton\GeneratorBundle\Tests\Definition;
7
8
use Graviton\GeneratorBundle\Definition\DefinitionElementInterface;
9
use Graviton\GeneratorBundle\Definition\JsonDefinition;
10
use Graviton\GeneratorBundle\Definition\JsonDefinitionField;
11
use Graviton\GeneratorBundle\Definition\JsonDefinitionArray;
12
use Graviton\GeneratorBundle\Definition\JsonDefinitionHash;
13
use Graviton\GeneratorBundle\Definition\JsonDefinitionRel;
14
use Graviton\GeneratorBundle\Definition\Schema;
15
use JMS\Serializer\SerializerBuilder;
16
17
/**
18
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
19
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link     http://swisscom.ch
21
 */
22
class DefinitionTest extends \PHPUnit_Framework_TestCase
23
{
24
    private $fullDefPath;
25
    private $minimalPath;
26
    private $noIdPath;
27
    private $invalidPath;
28
    private $wrongUriPath;
29
    private $subDocumentPath;
30
    private $relationsPath;
31
    private $rolesPath;
32
    private $nestedFieldPath;
33
    private $nestedRelationsPath;
34
35
    /**
36
     * setup
37
     *
38
     * @return void
39
     */
40
    public function setUp()
41
    {
42
        $this->fullDefPath = __DIR__.'/resources/test-full.json';
43
        $this->minimalPath = __DIR__.'/resources/test-minimal.json';
44
        $this->noIdPath = __DIR__.'/resources/test-noid.json';
45
        $this->invalidPath = __DIR__.'/resources/test-invalid.json';
46
        $this->wrongUriPath = __DIR__.'/resources/test-minimal-wrong-uri.json';
47
        $this->subDocumentPath = __DIR__.'/resources/test-minimal-sub.json';
48
        $this->relationsPath = __DIR__.'/resources/test-minimal-relations.json';
49
        $this->rolesPath = __DIR__.'/resources/test-roles.json';
50
        $this->nestedFieldPath = __DIR__.'/resources/test-nested-fields.json';
51
        $this->nestedRelationsPath = __DIR__.'/resources/test-nested-relations.json';
52
    }
53
54
    /**
55
     * @param string $file Definition file path
56
     * @return JsonDefinition
57
     */
58 View Code Duplication
    private function loadJsonDefinition($file)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $serializer = SerializerBuilder::create()
61
            ->addDefaultHandlers()
62
            ->addDefaultSerializationVisitors()
63
            ->addDefaultDeserializationVisitors()
64
            ->addMetadataDir(__DIR__.'/../../Resources/config/serializer', 'Graviton\\GeneratorBundle')
65
            ->setCacheDir(sys_get_temp_dir())
66
            ->setDebug(true)
67
            ->build();
68
69
        return new JsonDefinition(
70
            $serializer->deserialize(
71
                file_get_contents($file),
72
                'Graviton\\GeneratorBundle\\Definition\\Schema\\Definition',
73
                'json'
74
            )
75
        );
76
    }
77
78
    /**
79
     * invalid handling
80
     *
81
     * @expectedException \JMS\Serializer\Exception\RuntimeException
82
     *
83
     * @return void
84
     */
85
    public function testInvalidHandling()
86
    {
87
        $this->loadJsonDefinition($this->invalidPath);
88
    }
89
90
    /**
91
     * no id
92
     *
93
     * @expectedException \RuntimeException
94
     *
95
     * @return void
96
     */
97
    public function testNoId()
98
    {
99
        $this->loadJsonDefinition($this->noIdPath)->getId();
100
    }
101
102
    /**
103
     * basics
104
     *
105
     * @return void
106
     */
107
    public function testBasics()
108
    {
109
        $jsonDef = $this->loadJsonDefinition($this->fullDefPath);
110
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinition', $jsonDef);
111
        $this->assertEquals('Showcase', $jsonDef->getId());
112
        $this->assertEquals('A service showcasing all of our generator features', $jsonDef->getDescription());
113
        $this->assertTrue($jsonDef->hasController());
114
        $this->assertTrue($jsonDef->hasFixtures());
115
        $this->assertFalse($jsonDef->isReadOnlyService());
116
    }
117
118
    /**
119
     * full
120
     *
121
     * @return void
122
     */
123
    public function testFull()
124
    {
125
        $jsonDef = $this->loadJsonDefinition($this->fullDefPath);
126
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinition', $jsonDef);
127
128
        // we only assert what we didn't assert in testBasics()
129
        $this->assertEquals(0, count($jsonDef->getRelations()));
130
        $this->assertEquals(16, count($jsonDef->getFields()));
131
        $this->assertEquals('/hans/showcase', $jsonDef->getRouterBase());
132
        $this->assertEquals(5, $jsonDef->getFixtureOrder());
133
        $this->assertFalse($jsonDef->isSubDocument());
134
135
        $this->assertEquals(
136
            '\Graviton\CoreBundle\Controller\ShowcaseExtensionController',
137
            $jsonDef->getBaseController()
138
        );
139
140
        $this->assertInstanceOf(
141
            'Graviton\GeneratorBundle\Definition\JsonDefinitionField',
142
            $jsonDef->getField('anotherInt')
143
        );
144
    }
145
146
    /**
147
     * minimal
148
     *
149
     * @return void
150
     */
151
    public function testMinimal()
152
    {
153
        $jsonDef = $this->loadJsonDefinition($this->minimalPath);
154
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinition', $jsonDef);
155
156
        $this->assertFalse($jsonDef->hasController());
157
        $this->assertFalse($jsonDef->hasFixtures());
158
        $this->assertFalse($jsonDef->getRouterBase());
159
        $this->assertTrue($jsonDef->isReadOnlyService());
160
161
        // test default behavior if nothing is specified..
162
        $this->assertEquals(0, count($jsonDef->getRelations()));
163
        $this->assertEquals(0, count($jsonDef->getFields()));
164
        $this->assertEquals(100, $jsonDef->getFixtureOrder());
165
        $this->assertEquals('RestController', $jsonDef->getBaseController());
166
        $this->assertNull($jsonDef->getField('test'));
167
        $this->assertNull($jsonDef->getNamespace());
168
        $this->assertFalse($jsonDef->isSubDocument());
169
    }
170
171
    /**
172
     * namespace
173
     *
174
     * @return void
175
     */
176
    public function testNamespaceSetting()
177
    {
178
        $jsonDef = $this->loadJsonDefinition($this->fullDefPath);
179
180
        $this->assertNull($jsonDef->getNamespace());
181
        $jsonDef->setNamespace('Hans\Namespace');
182
        $this->assertEquals('Hans\Namespace', $jsonDef->getNamespace());
183
184
        $jsonDef->setNamespace('Hans\Namespace\\');
185
        $this->assertEquals('Hans\Namespace', $jsonDef->getNamespace());
186
    }
187
188
    /**
189
     * sub document
190
     *
191
     * @return void
192
     */
193
    public function testSubDocument()
194
    {
195
        $jsonDef = $this->loadJsonDefinition($this->subDocumentPath);
196
        $this->assertTrue($jsonDef->isSubDocument());
197
    }
198
199
    /**
200
     * relations
201
     *
202
     * @return void
203
     */
204
    public function testRelations()
205
    {
206
        $jsonDef = $this->loadJsonDefinition($this->relationsPath);
207
        $relations = $jsonDef->getRelations();
208
209
        $this->assertEquals(2, count($relations));
210
211
        $this->assertInstanceOf(
212
            'Graviton\\GeneratorBundle\\Definition\\Schema\\Relation',
213
            $relations['anotherInt']
214
        );
215
        $this->assertEquals('embed', $relations['anotherInt']->getType());
216
217
        $this->assertInstanceOf(
218
            'Graviton\\GeneratorBundle\\Definition\\Schema\\Relation',
219
            $relations['someFloatyDouble']
220
        );
221
        $this->assertEquals('ref', $relations['someFloatyDouble']->getType());
222
    }
223
224
    /**
225
     * uri fixing
226
     *
227
     * @return void
228
     */
229
    public function testUriFixing()
230
    {
231
        $jsonDef = $this->loadJsonDefinition($this->wrongUriPath);
232
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinition', $jsonDef);
233
        $this->assertEquals('/hans/showcase', $jsonDef->getRouterBase());
234
    }
235
236
    /**
237
     * role set definition
238
     *
239
     * @return void
240
     */
241
    public function testRoles()
242
    {
243
        $jsonDef = $this->loadJsonDefinition($this->rolesPath);
244
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinition', $jsonDef);
245
        $this->assertEquals(array('GRAVITON_USER'), $jsonDef->getRoles());
246
    }
247
248
    /**
249
     * @return void
250
     */
251
    public function testNestedFields()
252
    {
253
        $definition = $this->loadJsonDefinition($this->nestedFieldPath);
254
        $this->assertEquals(
255
            [
256
                'id' => new JsonDefinitionField(
257
                    'id',
258
                    (new Schema\Field())
259
                        ->setName('id')
260
                        ->setType('varchar')
261
                ),
262
                'hash' => new JsonDefinitionHash(
263
                    'hash',
264
                    $definition,
265
                    [
266
                        'abc' => new JsonDefinitionField(
267
                            'abc',
268
                            (new Schema\Field())
269
                                ->setName('hash.abc')
270
                                ->setType('integer')
271
                        ),
272
                        'def' => new JsonDefinitionField(
273
                            'def',
274
                            (new Schema\Field())
275
                                ->setName('hash.def')
276
                                ->setType('boolean')
277
                        ),
278
                    ],
279
                    (new Schema\Field())
280
                        ->setName('hash')
281
                        ->setType('hash')
282
                        ->setExposeAs('$hash')
283
                        ->setRequired(true)
284
                ),
285
                'array' => new JsonDefinitionArray(
286
                    'array',
287
                    new JsonDefinitionField(
288
                        'array',
289
                        (new Schema\Field())
290
                            ->setName('array.0')
291
                            ->setType('string')
292
                    )
293
                ),
294
                'arrayarray' => new JsonDefinitionArray(
295
                    'arrayarray',
296
                    new JsonDefinitionArray(
297
                        'arrayarray',
298
                        new JsonDefinitionArray(
299
                            'arrayarray',
300
                            new JsonDefinitionField(
301
                                'arrayarray',
302
                                (new Schema\Field())
303
                                    ->setName('arrayarray.0.0.0')
304
                                    ->setType('integer')
305
                            )
306
                        )
307
                    )
308
                ),
309
                'arrayhash' => new JsonDefinitionArray(
310
                    'arrayhash',
311
                    new JsonDefinitionHash(
312
                        'arrayhash',
313
                        $definition,
314
                        [
315
                            'mno' => new JsonDefinitionField(
316
                                'mno',
317
                                (new Schema\Field())
318
                                    ->setName('arrayhash.0.mno')
319
                                    ->setType('string')
320
                            ),
321
                            'pqr' => new JsonDefinitionField(
322
                                'pqr',
323
                                (new Schema\Field())
324
                                    ->setName('arrayhash.0.pqr')
325
                                    ->setType('float')
326
                            ),
327
                        ]
328
                    )
329
                ),
330
                'deep' => new JsonDefinitionArray(
331
                    'deep',
332
                    new JsonDefinitionHash(
333
                        'deep',
334
                        $definition,
335
                        [
336
                            'b' => new JsonDefinitionArray(
337
                                'b',
338
                                new JsonDefinitionHash(
339
                                    'b',
340
                                    $definition,
341
                                    [
342
                                        'c' => new JsonDefinitionHash(
343
                                            'c',
344
                                            $definition,
345
                                            [
346
                                                'd' => new JsonDefinitionHash(
347
                                                    'd',
348
                                                    $definition,
349
                                                    [
350
                                                        'e' => new JsonDefinitionField(
351
                                                            'e',
352
                                                            (new Schema\Field())
353
                                                                ->setName('deep.0.b.0.c.d.e')
354
                                                                ->setType('varchar')
355
                                                        ),
356
                                                    ]
357
                                                ),
358
                                            ],
359
                                            (new Schema\Field())
360
                                                ->setName('deep.0.b.0.c')
361
                                                ->setType('hash')
362
                                                ->setDescription('description')
363
                                                ->setExposeAs('$c')
364
                                                ->setRequired(false)
365
                                        ),
366
                                    ]
367
                                )
368
                            ),
369
                            'c' => new JsonDefinitionField(
370
                                'c',
371
                                (new Schema\Field())
372
                                    ->setName('deep.0.c')
373
                                    ->setType('string')
374
                            ),
375
                            'd' => new JsonDefinitionArray(
376
                                'd',
377
                                new JsonDefinitionField(
378
                                    'd',
379
                                    (new Schema\Field())
380
                                        ->setName('deep.0.d.0')
381
                                        ->setType('integer')
382
                                )
383
                            ),
384
                        ]
385
                    )
386
                ),
387
            ],
388
            $definition->getFields()
389
        );
390
    }
391
392
    /**
393
     * @return void
394
     */
395
    public function testHashToJsonDefinition()
396
    {
397
        $definition = $this->loadJsonDefinition($this->nestedFieldPath);
398
399
        /** @var JsonDefinitionHash $field */
400
        $field = $this->getFieldByPath($definition, 'hash');
401
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionHash', $field);
402
403
        $this->assertEquals(
404
            (new JsonDefinition(
405
                (new Schema\Definition())
406
                    ->setId('FieldTestHash')
407
                    ->setIsSubDocument(true)
408
                    ->setTarget(
409
                        (new Schema\Target())
410
                            ->addField(
411
                                (new Schema\Field())
412
                                    ->setName('abc')
413
                                    ->setType('integer')
414
                            )
415
                            ->addField(
416
                                (new Schema\Field())
417
                                    ->setName('def')
418
                                    ->setType('boolean')
419
                            )
420
                    )
421
            )),
422
            $field->getJsonDefinition()
423
        );
424
425
        /** @var JsonDefinitionHash $field */
426
        $field = $this->getFieldByPath($definition, 'arrayhash.0');
427
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionHash', $field);
428
429
        $this->assertEquals(
430
            (new JsonDefinition(
431
                (new Schema\Definition())
432
                    ->setId('FieldTestArrayhash')
433
                    ->setIsSubDocument(true)
434
                    ->setTarget(
435
                        (new Schema\Target())
436
                            ->addField(
437
                                (new Schema\Field())
438
                                    ->setName('mno')
439
                                    ->setType('string')
440
                            )
441
                            ->addField(
442
                                (new Schema\Field())
443
                                    ->setName('pqr')
444
                                    ->setType('float')
445
                            )
446
                    )
447
            )),
448
            $field->getJsonDefinition()
449
        );
450
451
        /** @var JsonDefinitionHash $field */
452
        $field = $this->getFieldByPath($definition, 'deep.0');
453
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionHash', $field);
454
455
        $this->assertEquals(
456
            new JsonDefinition(
457
                (new Schema\Definition())
458
                    ->setId('FieldTestDeep')
459
                    ->setIsSubDocument(true)
460
                    ->setTarget(
461
                        (new Schema\Target())
462
                            ->addField(
463
                                (new Schema\Field())
464
                                    ->setName('b.0.c')
465
                                    ->setType('hash')
466
                                    ->setExposeAs('$c')
467
                                    ->setDescription('description')
468
                                    ->setRequired(false)
469
                            )
470
                            ->addField(
471
                                (new Schema\Field())
472
                                    ->setName('b.0.c.d.e')
473
                                    ->setType('varchar')
474
                            )
475
                            ->addField(
476
                                (new Schema\Field())
477
                                    ->setName('c')
478
                                    ->setType('string')
479
                            )
480
                            ->addField(
481
                                (new Schema\Field())
482
                                    ->setName('d.0')
483
                                    ->setType('integer')
484
                            )
485
                    )
486
            ),
487
            $field->getJsonDefinition()
488
        );
489
    }
490
491
    /**
492
     * @return void
493
     */
494
    public function testNestedRelations()
495
    {
496
        $definition = $this->loadJsonDefinition($this->nestedRelationsPath);
497
498
        /** @var JsonDefinitionHash $field */
499
        $field = $this->getFieldByPath($definition, 'hash');
500
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionHash', $field);
501
502
        $this->assertEquals(
503
            (new JsonDefinition(
504
                (new Schema\Definition())
505
                    ->setId('RelationTestHash')
506
                    ->setIsSubDocument(true)
507
                    ->setTarget(
508
                        (new Schema\Target())
509
                            ->addField(
510
                                (new Schema\Field())
511
                                    ->setName('embedOne')
512
                                    ->setType('class:Entity')
513
                            )
514
                            ->addField(
515
                                (new Schema\Field())
516
                                    ->setName('referenceOne')
517
                                    ->setType('class:Entity')
518
                            )
519
                            ->addField(
520
                                (new Schema\Field())
521
                                    ->setName('embedMany')
522
                                    ->setType('class:Entity[]')
523
                            )
524
                            ->addField(
525
                                (new Schema\Field())
526
                                    ->setName('referenceMany')
527
                                    ->setType('class:Entity[]')
528
                            )
529
                            ->addRelation(
530
                                (new Schema\Relation())
531
                                    ->setType(JsonDefinitionRel::REL_TYPE_EMBED)
532
                                    ->setLocalProperty('embedOne')
533
                            )
534
                            ->addRelation(
535
                                (new Schema\Relation())
536
                                    ->setType(JsonDefinitionRel::REL_TYPE_EMBED)
537
                                    ->setLocalProperty('embedMany')
538
                            )
539
                    )
540
            )),
541
            $field->getJsonDefinition()
542
        );
543
544
        /** @var JsonDefinitionRel $embedField */
545
        $embedField = $this->getFieldByPath($definition, 'hash.embedOne');
546
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionRel', $embedField);
547
        $this->assertEquals(JsonDefinitionRel::REL_TYPE_EMBED, $embedField->getDefAsArray()['relType']);
548
        $this->assertEquals('Entity', $embedField->getType());
549
550
        /** @var JsonDefinitionRel $embedField */
551
        $embedField = $this->getFieldByPath($definition, 'hash.embedMany.0');
552
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionRel', $embedField);
553
        $this->assertEquals(JsonDefinitionRel::REL_TYPE_EMBED, $embedField->getDefAsArray()['relType']);
554
        $this->assertEquals('Entity', $embedField->getType());
555
556
        /** @var JsonDefinitionRel $referenceField */
557
        $referenceField = $this->getFieldByPath($definition, 'hash.referenceOne');
558
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionRel', $referenceField);
559
        $this->assertEquals(JsonDefinitionRel::REL_TYPE_REF, $referenceField->getDefAsArray()['relType']);
560
        $this->assertEquals('Entity', $referenceField->getType());
561
562
        /** @var JsonDefinitionRel $referenceField */
563
        $referenceField = $this->getFieldByPath($definition, 'hash.referenceMany.0');
564
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionRel', $referenceField);
565
        $this->assertEquals(JsonDefinitionRel::REL_TYPE_REF, $referenceField->getDefAsArray()['relType']);
566
        $this->assertEquals('Entity', $referenceField->getType());
567
568
569
570
        /** @var JsonDefinitionRel $embedField */
571
        $embedField = $this->getFieldByPath($definition, 'deep.0.sub.embedOne');
572
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionRel', $embedField);
573
        $this->assertEquals(JsonDefinitionField::REL_TYPE_EMBED, $embedField->getDefAsArray()['relType']);
574
        $this->assertEquals('Entity', $embedField->getType());
575
576
        /** @var JsonDefinitionRel $embedField */
577
        $embedField = $this->getFieldByPath($definition, 'deep.0.sub.subsub.0.embedMany.0');
578
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionRel', $embedField);
579
        $this->assertEquals(JsonDefinitionField::REL_TYPE_EMBED, $embedField->getDefAsArray()['relType']);
580
        $this->assertEquals('Entity', $embedField->getType());
581
582
        /** @var JsonDefinitionRel $referenceField */
583
        $referenceField = $this->getFieldByPath($definition, 'deep.0.sub.referenceOne');
584
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionRel', $referenceField);
585
        $this->assertEquals(JsonDefinitionField::REL_TYPE_REF, $referenceField->getDefAsArray()['relType']);
586
        $this->assertEquals('Entity', $referenceField->getType());
587
588
        /** @var JsonDefinitionRel $referenceField */
589
        $referenceField = $this->getFieldByPath($definition, 'deep.0.sub.subsub.0.referenceMany.0');
590
        $this->assertInstanceOf('Graviton\GeneratorBundle\Definition\JsonDefinitionRel', $referenceField);
591
        $this->assertEquals(JsonDefinitionField::REL_TYPE_REF, $referenceField->getDefAsArray()['relType']);
592
        $this->assertEquals('Entity', $referenceField->getType());
593
    }
594
595
596
    /**
597
     * Primitive array test
598
     *
599
     * @return void
600
     */
601
    public function testPrimitiveArray()
602
    {
603
        $definition = $this->loadJsonDefinition(__DIR__.'/resources/test-primitive-array.json');
604
        $this->assertEquals(
605
            (new JsonDefinition(
606
                (new Schema\Definition())
607
                    ->setId('PrimitiveArray')
608
                    ->setTarget(
609
                        (new Schema\Target())
610
                            ->addField(
611
                                (new Schema\Field())
612
                                    ->setName('id')
613
                                    ->setType('string')
614
                            )
615
                            ->addField(
616
                                (new Schema\Field())
617
                                    ->setName('intarray.0')
618
                                    ->setType('int')
619
                            )
620
                            ->addField(
621
                                (new Schema\Field())
622
                                    ->setName('hash.intarray.0')
623
                                    ->setType('int')
624
                            )
625
                            ->addField(
626
                                (new Schema\Field())
627
                                    ->setName('hasharray.0.intarray.0')
628
                                    ->setType('int')
629
                            )
630
                    )
631
            )),
632
            $definition
633
        );
634
635
        /** @var JsonDefinitionArray $field */
636
        $field = $this->getFieldByPath($definition, 'intarray');
637
        $this->assertInstanceOf(JsonDefinitionArray::class, $field);
638
        $this->assertEquals('int[]', $field->getType());
639
        $this->assertEquals('intarray', $field->getName());
640
641
        /** @var JsonDefinitionField $field */
642
        $field = $this->getFieldByPath($definition, 'intarray.0');
643
        $this->assertInstanceOf(JsonDefinitionField::class, $field);
644
        $this->assertEquals('int', $field->getType());
645
        $this->assertEquals('intarray', $field->getName());
646
647
        /** @var JsonDefinitionArray $field */
648
        $field = $this->getFieldByPath($definition, 'hash.intarray');
649
        $this->assertInstanceOf(JsonDefinitionArray::class, $field);
650
        $this->assertEquals('int[]', $field->getType());
651
        $this->assertEquals('intarray', $field->getName());
652
653
        /** @var JsonDefinitionField $field */
654
        $field = $this->getFieldByPath($definition, 'hash.intarray.0');
655
        $this->assertInstanceOf(JsonDefinitionField::class, $field);
656
        $this->assertEquals('int', $field->getType());
657
        $this->assertEquals('intarray', $field->getName());
658
659
        /** @var JsonDefinitionArray $field */
660
        $field = $this->getFieldByPath($definition, 'hasharray.0.intarray');
661
        $this->assertInstanceOf(JsonDefinitionArray::class, $field);
662
        $this->assertEquals('int[]', $field->getType());
663
        $this->assertEquals('intarray', $field->getName());
664
665
        /** @var JsonDefinitionField $field */
666
        $field = $this->getFieldByPath($definition, 'hasharray.0.intarray.0');
667
        $this->assertInstanceOf(JsonDefinitionField::class, $field);
668
        $this->assertEquals('int', $field->getType());
669
        $this->assertEquals('intarray', $field->getName());
670
    }
671
672
    /**
673
     * test if indexes are exposed in def
674
     *
675
     * @return void
676
     */
677
    public function testIndexes()
678
    {
679
        $jsonDef = $this->loadJsonDefinition($this->fullDefPath);
680
        $this->assertInternalType('array', $jsonDef->getIndexes());
681
    }
682
683
    /**
684
     * test if indexes are exposed in def
685
     *
686
     * @return void
687
     */
688
    public function testTextIndexes()
689
    {
690
        $jsonDef = $this->loadJsonDefinition($this->fullDefPath);
691
        $this->assertInternalType('array', $jsonDef->getTextIndexes());
692
    }
693
694
    /**
695
     * Get field by path
696
     *
697
     * @param JsonDefinition $definition JSON definition
698
     * @param string         $path       Path to field
699
     * @return DefinitionElementInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be DefinitionElementInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
700
     */
701
    private function getFieldByPath(JsonDefinition $definition, $path)
702
    {
703
        $items = explode('.', $path);
704
        $field = $definition->getField(array_shift($items));
705
        foreach ($items as $item) {
706
            if ($item === '0') {
707
                if (!$field instanceof JsonDefinitionArray) {
708
                    throw new \InvalidArgumentException(sprintf('Error path: "%s"', $path));
709
                }
710
                $field = $field->getElement();
711
            } else {
712
                if (!$field instanceof JsonDefinitionHash) {
713
                    throw new \InvalidArgumentException(sprintf('Error path: "%s"', $path));
714
                }
715
                $field = $field->getJsonDefinition()->getField($item);
716
            }
717
        }
718
        return $field;
719
    }
720
}
721