Vous êtes connecté en tant que anonymous Se Deconnecter
Browse code

Application modulaire fonctionnelle !

Emmanuel ROY authored on 12/08/2019 15:10:25
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,760 +0,0 @@
1
-<?php declare(strict_types=1);
2
-/**
3
- * Part of Windwalker project Test files.  @codingStandardsIgnoreStart
4
- *
5
- * @copyright  Copyright (C) 2019 LYRASOFT Taiwan, Inc.
6
- * @license    LGPL-2.0-or-later
7
- */
8
-
9
-namespace Windwalker\Structure\Test;
10
-
11
-use Windwalker\Structure\Structure;
12
-use Windwalker\Structure\StructureHelper;
13
-use Windwalker\Test\TestCase\AbstractBaseTestCase;
14
-
15
-/**
16
- * Test class of Structure
17
- *
18
- * @since 2.0
19
- */
20
-class StructureTest extends AbstractBaseTestCase
21
-{
22
-    /**
23
-     * Test instance.
24
-     *
25
-     * @var Structure
26
-     */
27
-    protected $instance;
28
-
29
-    /**
30
-     * Sets up the fixture, for example, opens a network connection.
31
-     * This method is called before a test is executed.
32
-     *
33
-     * @return void
34
-     */
35
-    protected function setUp(): void
36
-    {
37
-        $this->instance = new Structure($this->getTestData());
38
-    }
39
-
40
-    /**
41
-     * getTestData
42
-     *
43
-     * @return  array
44
-     */
45
-    protected function getTestData()
46
-    {
47
-        return [
48
-            'flower' => 'sakura',
49
-            'olive' => 'peace',
50
-            'pos1' => [
51
-                'sunflower' => 'love',
52
-            ],
53
-            'pos2' => [
54
-                'cornflower' => 'elegant',
55
-            ],
56
-            'array' => [
57
-                'A',
58
-                'B',
59
-                'C',
60
-            ],
61
-        ];
62
-    }
63
-
64
-    /**
65
-     * Tears down the fixture, for example, closes a network connection.
66
-     * This method is called after a test is executed.
67
-     *
68
-     * @return void
69
-     */
70
-    protected function tearDown(): void
71
-    {
72
-    }
73
-
74
-    /**
75
-     * Method to test __clone().
76
-     *
77
-     * @return void
78
-     *
79
-     * @covers \Windwalker\Structure\Structure::__clone
80
-     */
81
-    public function test__clone()
82
-    {
83
-        $structure1 = new Structure($this->getTestData());
84
-
85
-        $structure2 = clone $structure1;
86
-
87
-        $this->assertEquals($structure1, $structure2);
88
-    }
89
-
90
-    /**
91
-     * Method to test __toString().
92
-     *
93
-     * @return void
94
-     *
95
-     * @covers \Windwalker\Structure\Structure::__toString
96
-     */
97
-    public function test__toString()
98
-    {
99
-        $this->assertJsonStringEqualsJsonString(json_encode($this->getTestData()), (string) $this->instance);
100
-    }
101
-
102
-    /**
103
-     * Method to test jsonSerialize().
104
-     *
105
-     * @return void
106
-     *
107
-     * @covers \Windwalker\Structure\Structure::jsonSerialize
108
-     */
109
-    public function testJsonSerialize()
110
-    {
111
-        $this->assertJsonStringEqualsJsonString(json_encode($this->getTestData()), (string) $this->instance);
112
-    }
113
-
114
-    /**
115
-     * Method to test def().
116
-     *
117
-     * @return void
118
-     *
119
-     * @covers \Windwalker\Structure\Structure::def
120
-     */
121
-    public function testDef()
122
-    {
123
-        $this->assertNull($this->instance->get('lily'));
124
-
125
-        $this->instance->def('lily', 'love');
126
-
127
-        $this->assertEquals('love', $this->instance->get('lily'));
128
-    }
129
-
130
-    /**
131
-     * Method to test exists().
132
-     *
133
-     * @return void
134
-     *
135
-     * @covers \Windwalker\Structure\Structure::exists
136
-     */
137
-    public function testExists()
138
-    {
139
-        $this->assertFalse($this->instance->exists('rose'));
140
-        $this->assertTrue($this->instance->exists('flower'));
141
-    }
142
-
143
-    /**
144
-     * Method to test get().
145
-     *
146
-     * @return void
147
-     *
148
-     * @covers \Windwalker\Structure\Structure::get
149
-     */
150
-    public function testGet()
151
-    {
152
-        $this->assertEquals($this->instance->get('flower', 'canna'), 'sakura');
153
-
154
-        $this->assertEquals($this->instance->get('not.exists', 'canna'), 'canna');
155
-
156
-        $this->assertNull($this->instance->get('not.exists'));
157
-
158
-        $this->assertEquals($this->instance->get('pos1.sunflower'), 'love');
159
-    }
160
-
161
-    /**
162
-     * Method to test loadArray().
163
-     *
164
-     * @return void
165
-     *
166
-     * @covers \Windwalker\Structure\Structure::load
167
-     */
168
-    public function testLoadArray()
169
-    {
170
-        $structure = new Structure();
171
-
172
-        $structure->load($this->getTestData());
173
-
174
-        $this->assertEquals($structure->get('olive'), 'peace');
175
-
176
-        $this->assertEquals($structure->get('pos1.sunflower'), 'love');
177
-    }
178
-
179
-    /**
180
-     * Method to test loadObject().
181
-     *
182
-     * @return void
183
-     *
184
-     * @covers \Windwalker\Structure\Structure::load
185
-     */
186
-    public function testLoadObject()
187
-    {
188
-        $structure = new Structure();
189
-
190
-        $structure->load((object) $this->getTestData());
191
-
192
-        $this->assertEquals($structure->get('olive'), 'peace');
193
-
194
-        $this->assertEquals($structure->get('pos1.sunflower'), 'love');
195
-    }
196
-
197
-    /**
198
-     * Method to test loadFile().
199
-     *
200
-     * @return void
201
-     *
202
-     * @covers \Windwalker\Structure\Structure::loadFile
203
-     */
204
-    public function testLoadFile()
205
-    {
206
-        $structure = new Structure();
207
-
208
-        $this->assertEquals(
209
-            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.json', 'json')->get('flower'),
210
-            'sakura'
211
-        );
212
-        $this->assertEquals(
213
-            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.yml', 'yaml')->get('flower'),
214
-            'sakura'
215
-        );
216
-        $this->assertEquals(
217
-            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.ini', 'ini')->get('flower'),
218
-            'sakura'
219
-        );
220
-        $this->assertEquals(
221
-            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.xml', 'xml')->get('flower'),
222
-            'sakura'
223
-        );
224
-        $this->assertEquals(
225
-            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.php', 'php')->get('flower'),
226
-            'sakura'
227
-        );
228
-    }
229
-
230
-    /**
231
-     * Method to test loadString().
232
-     *
233
-     * @return void
234
-     *
235
-     * @covers \Windwalker\Structure\Structure::loadString
236
-     * @throws \Exception
237
-     */
238
-    public function testLoadString()
239
-    {
240
-        $structure = new Structure();
241
-
242
-        $this->assertEquals(
243
-            $structure->reset()->loadString(
244
-                file_get_contents(__DIR__ . '/Stubs/flower.json'),
245
-                'json'
246
-            )->get('flower'),
247
-            'sakura'
248
-        );
249
-        $this->assertEquals(
250
-            $structure->reset()->loadString(
251
-                file_get_contents(__DIR__ . '/Stubs/flower.yml'),
252
-                'yaml'
253
-            )->get('flower'),
254
-            'sakura'
255
-        );
256
-        $this->assertEquals(
257
-            $structure->reset()->loadString(
258
-                file_get_contents(__DIR__ . '/Stubs/flower.ini'),
259
-                'ini'
260
-            )->get('flower'),
261
-            'sakura'
262
-        );
263
-        $this->assertEquals(
264
-            $structure->reset()->loadString(
265
-                file_get_contents(__DIR__ . '/Stubs/flower.xml'),
266
-                'xml'
267
-            )->get('flower'),
268
-            'sakura'
269
-        );
270
-        $this->assertEquals(
271
-            $structure->reset()->loadString(
272
-                file_get_contents(__DIR__ . '/Stubs/flower.hjson'),
273
-                'hjson'
274
-            )->get('flower'),
275
-            'sakura'
276
-        );
277
-        $this->assertEquals(
278
-            $structure->reset()->loadString(
279
-                file_get_contents(__DIR__ . '/Stubs/flower.toml'),
280
-                'toml',
281
-                ['load_raw' => true]
282
-            )->get('flower'),
283
-            'sakura'
284
-        );
285
-    }
286
-
287
-    /**
288
-     * Method to test merge().
289
-     *
290
-     * @return void
291
-     *
292
-     * @covers \Windwalker\Structure\Structure::merge
293
-     * @throws \Exception
294
-     */
295
-    public function testMerge()
296
-    {
297
-        // Test recursive merge
298
-        $object1 = '{
299
-            "foo" : "foo value",
300
-            "bar" : {
301
-                "bar1" : "bar value 1",
302
-                "bar2" : "bar value 2",
303
-                "bar3" : "bar value 3"
304
-            }
305
-        }';
306
-        $object2 = '{
307
-            "foo" : "foo value",
308
-            "bar" : {
309
-                "bar2" : "new bar value 2",
310
-                "bar3" : null
311
-            }
312
-        }';
313
-
314
-        $structure1 = new Structure(json_decode($object1));
315
-        $structure2 = new Structure(json_decode($object2));
316
-
317
-        $structure1->merge($structure2);
318
-
319
-        $this->assertEquals(
320
-            'new bar value 2',
321
-            $structure1->get('bar.bar2'),
322
-            'Line: ' . __LINE__ . '. bar.bar2 should be override.'
323
-        );
324
-        $this->assertEquals(
325
-            'bar value 1',
326
-            $structure1->get('bar.bar1'),
327
-            'Line: ' . __LINE__ . '. bar.bar1 should not be override.'
328
-        );
329
-        $this->assertSame(
330
-            'bar value 3',
331
-            $structure1->get('bar.bar3'),
332
-            'Line: ' . __LINE__ . '. bar.bar3 should not be override.'
333
-        );
334
-
335
-        $structure = new Structure(['flower' => 'rose', 'honor' => 'Osmanthus month']);
336
-
337
-        $structure->merge($this->instance);
338
-
339
-        $this->assertEquals($structure->get('flower'), 'sakura');
340
-        $this->assertEquals($structure->get('honor'), 'Osmanthus month');
341
-    }
342
-
343
-    /**
344
-     * Method to test merge().
345
-     *
346
-     * @return void
347
-     *
348
-     * @covers \Windwalker\Structure\Structure::merge
349
-     */
350
-    public function testMergeWithIgnoreValues()
351
-    {
352
-        // Test recursive merge
353
-        $object1 = '{
354
-            "foo" : "foo value",
355
-            "bar" : {
356
-                "bar1" : "bar value 1",
357
-                "bar2" : "bar value 2",
358
-                "bar3" : "bar value 3"
359
-            }
360
-        }';
361
-        $object2 = '{
362
-            "foo" : "foo value",
363
-            "bar" : {
364
-                "bar2" : "new bar value 2",
365
-                "bar3" : ""
366
-            }
367
-        }';
368
-
369
-        $structure1 = new Structure(json_decode($object1));
370
-        $structure2 = new Structure(json_decode($object2));
371
-
372
-        $structure1->setIgnoreValues([null, '']);
373
-        $structure1->merge($structure2);
374
-
375
-        $this->assertEquals(
376
-            'new bar value 2',
377
-            $structure1->get('bar.bar2'),
378
-            'Line: ' . __LINE__ . '. bar.bar2 should be override.'
379
-        );
380
-        $this->assertEquals(
381
-            'bar value 1',
382
-            $structure1->get('bar.bar1'),
383
-            'Line: ' . __LINE__ . '. bar.bar1 should not be override.'
384
-        );
385
-        $this->assertSame(
386
-            'bar value 3',
387
-            $structure1->get('bar.bar3'),
388
-            'Line: ' . __LINE__ . '. bar.bar3 should not be override.'
389
-        );
390
-
391
-        $structure = new Structure(['flower' => 'rose', 'honor' => 'Osmanthus month']);
392
-
393
-        $structure->merge($this->instance);
394
-
395
-        $this->assertEquals($structure->get('flower'), 'sakura');
396
-        $this->assertEquals($structure->get('honor'), 'Osmanthus month');
397
-    }
398
-
399
-    /**
400
-     * testMergeTo
401
-     *
402
-     * @return  void
403
-     *
404
-     * @covers \Windwalker\Structure\Structure::mergeTo
405
-     */
406
-    public function testMergeTo()
407
-    {
408
-        $structure = new Structure(['sunflower' => 'shine', 'honor' => 'Osmanthus month']);
409
-
410
-        $this->instance->mergeTo('pos1', $structure);
411
-
412
-        $this->assertEquals($this->instance->get('pos1.sunflower'), 'shine');
413
-        $this->assertEquals($this->instance->get('pos1.honor'), 'Osmanthus month');
414
-
415
-        $this->instance->mergeTo('foo.bar', $structure);
416
-
417
-        $this->assertEquals($this->instance->get('foo.bar.sunflower'), 'shine');
418
-        $this->assertEquals($this->instance->get('foo.bar.honor'), 'Osmanthus month');
419
-    }
420
-
421
-    /**
422
-     * Method to test offsetExists().
423
-     *
424
-     * @return void
425
-     *
426
-     * @covers \Windwalker\Structure\Structure::offsetExists
427
-     */
428
-    public function testOffsetExists()
429
-    {
430
-        $this->assertTrue(isset($this->instance['flower']));
431
-        $this->assertFalse(isset($this->instance['carbon']));
432
-    }
433
-
434
-    /**
435
-     * Method to test offsetGet().
436
-     *
437
-     * @return void
438
-     *
439
-     * @covers \Windwalker\Structure\Structure::offsetGet
440
-     */
441
-    public function testOffsetGet()
442
-    {
443
-        $this->assertEquals($this->instance['flower'], 'sakura');
444
-    }
445
-
446
-    /**
447
-     * Method to test offsetSet().
448
-     *
449
-     * @return void
450
-     *
451
-     * @covers \Windwalker\Structure\Structure::offsetSet
452
-     */
453
-    public function testOffsetSet()
454
-    {
455
-        $this->instance['bird'] = 'flying';
456
-
457
-        $this->assertEquals($this->instance['bird'], 'flying');
458
-    }
459
-
460
-    /**
461
-     * Method to test offsetUnset().
462
-     *
463
-     * @return void
464
-     *
465
-     * @covers \Windwalker\Structure\Structure::offsetUnset
466
-     */
467
-    public function testOffsetUnset()
468
-    {
469
-        unset($this->instance['bird']);
470
-
471
-        $this->assertEquals($this->instance['bird'], null);
472
-    }
473
-
474
-    /**
475
-     * Method to test set().
476
-     *
477
-     * @return void
478
-     *
479
-     * @covers \Windwalker\Structure\Structure::set
480
-     */
481
-    public function testSet()
482
-    {
483
-        $this->instance->set('tree.bird', 'sleeping');
484
-
485
-        $this->assertEquals($this->instance->get('tree.bird'), 'sleeping');
486
-    }
487
-
488
-    /**
489
-     * Method to test setRaw()
490
-     *
491
-     * @return  void
492
-     *
493
-     * @covers \Windwalker\Structure\Structure::setRaw
494
-     */
495
-    public function testSetRaw()
496
-    {
497
-        $object = (object) ['foo' => 'bar'];
498
-
499
-        $this->instance->setRaw('tree.bird', $object);
500
-
501
-        $this->assertEquals('bar', $this->instance->get('tree.bird.foo'));
502
-        $this->assertSame($object, $this->instance->get('tree.bird'));
503
-    }
504
-
505
-    /**
506
-     * Method to test toArray().
507
-     *
508
-     * @return void
509
-     *
510
-     * @covers \Windwalker\Structure\Structure::toArray
511
-     */
512
-    public function testToArray()
513
-    {
514
-        $structure = new Structure($this->getTestData());
515
-
516
-        $this->assertEquals($structure->toArray(), $this->getTestData());
517
-    }
518
-
519
-    /**
520
-     * Method to test toObject().
521
-     *
522
-     * @return void
523
-     *
524
-     * @covers \Windwalker\Structure\Structure::toObject
525
-     */
526
-    public function testToObject()
527
-    {
528
-        $structure = new Structure($this->getTestData());
529
-
530
-        $this->assertEquals($structure->toObject(), StructureHelper::toObject($this->getTestData()));
531
-    }
532
-
533
-    /**
534
-     * Method to test toString().
535
-     *
536
-     * @return void
537
-     *
538
-     * @covers \Windwalker\Structure\Structure::toString
539
-     */
540
-    public function testToString()
541
-    {
542
-        $structure = new Structure($this->getTestData());
543
-
544
-        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.ini'), $structure->toString('ini'));
545
-        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.json'), $structure->toString('json'));
546
-        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.yml'), $structure->toString('yml'));
547
-        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.xml'), $structure->toString('xml'));
548
-        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.php'), $structure->toString('php', ['strict' => true]));
549
-    }
550
-
551
-    /**
552
-     * Method to test flatten().
553
-     *
554
-     * @return void
555
-     *
556
-     * @covers \Windwalker\Structure\Structure::flatten
557
-     */
558
-    public function testFlatten()
559
-    {
560
-        $flatted = $this->instance->flatten();
561
-
562
-        $this->assertEquals($flatted['pos1.sunflower'], 'love');
563
-
564
-        $flatted = $this->instance->flatten('/');
565
-
566
-        $this->assertEquals($flatted['pos1/sunflower'], 'love');
567
-    }
568
-
569
-    /**
570
-     * testAppend
571
-     *
572
-     * @return  void
573
-     *
574
-     * @covers \Windwalker\Structure\Structure::push
575
-     */
576
-    public function testPush()
577
-    {
578
-        $structure = new Structure();
579
-
580
-        $structure->set('foo', ['var1', 'var2', 'var3']);
581
-
582
-        $structure->push('foo', 'var4');
583
-
584
-        $this->assertEquals('var4', $structure->get('foo.3'));
585
-
586
-        $structure->push('foo', 'var5', 'var6');
587
-
588
-        $this->assertEquals('var5', $structure->get('foo.4'));
589
-        $this->assertEquals('var6', $structure->get('foo.5'));
590
-
591
-        $structure->setRaw('foo2', (object) ['var1', 'var2', 'var3']);
592
-
593
-        $b = $structure->get('foo2');
594
-
595
-        $this->assertTrue(is_object($b));
596
-
597
-        $structure->push('foo2', 'var4');
598
-
599
-        $b = $structure->get('foo2');
600
-
601
-        $this->assertTrue(is_array($b));
602
-    }
603
-
604
-    /**
605
-     * testShift
606
-     *
607
-     * @return  void
608
-     *
609
-     * @covers \Windwalker\Structure\Structure::shift
610
-     */
611
-    public function testShift()
612
-    {
613
-        $structure = new Structure();
614
-
615
-        $structure->set('foo.bar', ['var1', 'var2', 'var3']);
616
-
617
-        $this->assertEquals('var1', $structure->shift('foo.bar'));
618
-
619
-        $this->assertEquals('var2', $structure->get('foo.bar.0'));
620
-
621
-        $structure->setRaw('foo.bar2', (object) ['v1' => 'var1', 'v2' => 'var2', 'v3' => 'var3']);
622
-
623
-        $this->assertEquals('var1', $structure->shift('foo.bar2'));
624
-
625
-        $this->assertEquals('var2', $structure->get('foo.bar2.v2'));
626
-
627
-        $this->assertTrue(is_array($structure->get('foo.bar2')));
628
-    }
629
-
630
-    /**
631
-     * testPop
632
-     *
633
-     * @return  void
634
-     *
635
-     * @covers \Windwalker\Structure\Structure::pop
636
-     */
637
-    public function testPop()
638
-    {
639
-        $structure = new Structure();
640
-
641
-        $structure->set('foo.bar', ['var1', 'var2', 'var3']);
642
-
643
-        $this->assertEquals('var3', $structure->pop('foo.bar'));
644
-
645
-        $this->assertNull($structure->get('foo.bar.2'));
646
-
647
-        $structure->setRaw('foo.bar2', (object) ['v1' => 'var1', 'v2' => 'var2', 'v3' => 'var3']);
648
-
649
-        $this->assertEquals('var3', $structure->pop('foo.bar2'));
650
-
651
-        $this->assertNull($structure->get('foo.bar2.v3'));
652
-
653
-        $this->assertTrue(is_array($structure->get('foo.bar2')));
654
-    }
655
-
656
-    /**
657
-     * testUnshift
658
-     *
659
-     * @return  void
660
-     *
661
-     * @covers \Windwalker\Structure\Structure::unshift
662
-     */
663
-    public function testUnshift()
664
-    {
665
-        $structure = new Structure();
666
-
667
-        $structure->set('foo', ['var1', 'var2', 'var3']);
668
-
669
-        $structure->unshift('foo', 'var4');
670
-
671
-        $this->assertEquals('var4', $structure->get('foo.0'));
672
-
673
-        $structure->unshift('foo', 'var5', 'var6');
674
-
675
-        $this->assertEquals('var5', $structure->get('foo.0'));
676
-        $this->assertEquals('var6', $structure->get('foo.1'));
677
-
678
-        $structure->setRaw('foo2', (object) ['var1', 'var2', 'var3']);
679
-
680
-        $b = $structure->get('foo2');
681
-
682
-        $this->assertTrue(is_object($b));
683
-
684
-        $structure->unshift('foo2', 'var4');
685
-
686
-        $b = $structure->get('foo2');
687
-
688
-        $this->assertTrue(is_array($b));
689
-    }
690
-
691
-    /**
692
-     * testReset
693
-     *
694
-     * @return  void
695
-     *
696
-     * @covers  \Windwalker\Structure\Structure::reset
697
-     */
698
-    public function testReset()
699
-    {
700
-        $this->instance->reset();
701
-
702
-        $this->assertEquals([], $this->instance->getRaw());
703
-    }
704
-
705
-    /**
706
-     * testGetRaw
707
-     *
708
-     * @return  void
709
-     *
710
-     * @covers  \Windwalker\Structure\Structure::getRaw
711
-     */
712
-    public function testGetRaw()
713
-    {
714
-        $this->assertEquals($this->getTestData(), $this->instance->getRaw());
715
-    }
716
-
717
-    /**
718
-     * testGetIterator
719
-     *
720
-     * @return  void
721
-     *
722
-     * @covers  \Windwalker\Structure\Structure::getIterator
723
-     */
724
-    public function testGetIterator()
725
-    {
726
-        $this->assertInstanceOf('RecursiveArrayIterator', $this->instance->getIterator());
727
-
728
-        $this->assertEquals($this->getTestData(), iterator_to_array($this->instance));
729
-        $this->assertEquals(
730
-            iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->getTestData()))),
731
-            iterator_to_array(new \RecursiveIteratorIterator($this->instance))
732
-        );
733
-    }
734
-
735
-    /**
736
-     * testCount
737
-     *
738
-     * @return  void
739
-     *
740
-     * @covers  \Windwalker\Structure\Structure::count
741
-     */
742
-    public function testCount()
743
-    {
744
-        $this->assertEquals(5, count($this->instance));
745
-    }
746
-
747
-    /**
748
-     * loadFile
749
-     *
750
-     * @param string $file
751
-     *
752
-     * @return  string
753
-     */
754
-    protected function loadFile($file)
755
-    {
756
-        $text = file_get_contents($file);
757
-
758
-        return $text;
759
-    }
760
-}
Browse code

initial commit

Emmanuel ROY authored on 09/08/2019 08:39:02
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,760 @@
1
+<?php declare(strict_types=1);
2
+/**
3
+ * Part of Windwalker project Test files.  @codingStandardsIgnoreStart
4
+ *
5
+ * @copyright  Copyright (C) 2019 LYRASOFT Taiwan, Inc.
6
+ * @license    LGPL-2.0-or-later
7
+ */
8
+
9
+namespace Windwalker\Structure\Test;
10
+
11
+use Windwalker\Structure\Structure;
12
+use Windwalker\Structure\StructureHelper;
13
+use Windwalker\Test\TestCase\AbstractBaseTestCase;
14
+
15
+/**
16
+ * Test class of Structure
17
+ *
18
+ * @since 2.0
19
+ */
20
+class StructureTest extends AbstractBaseTestCase
21
+{
22
+    /**
23
+     * Test instance.
24
+     *
25
+     * @var Structure
26
+     */
27
+    protected $instance;
28
+
29
+    /**
30
+     * Sets up the fixture, for example, opens a network connection.
31
+     * This method is called before a test is executed.
32
+     *
33
+     * @return void
34
+     */
35
+    protected function setUp(): void
36
+    {
37
+        $this->instance = new Structure($this->getTestData());
38
+    }
39
+
40
+    /**
41
+     * getTestData
42
+     *
43
+     * @return  array
44
+     */
45
+    protected function getTestData()
46
+    {
47
+        return [
48
+            'flower' => 'sakura',
49
+            'olive' => 'peace',
50
+            'pos1' => [
51
+                'sunflower' => 'love',
52
+            ],
53
+            'pos2' => [
54
+                'cornflower' => 'elegant',
55
+            ],
56
+            'array' => [
57
+                'A',
58
+                'B',
59
+                'C',
60
+            ],
61
+        ];
62
+    }
63
+
64
+    /**
65
+     * Tears down the fixture, for example, closes a network connection.
66
+     * This method is called after a test is executed.
67
+     *
68
+     * @return void
69
+     */
70
+    protected function tearDown(): void
71
+    {
72
+    }
73
+
74
+    /**
75
+     * Method to test __clone().
76
+     *
77
+     * @return void
78
+     *
79
+     * @covers \Windwalker\Structure\Structure::__clone
80
+     */
81
+    public function test__clone()
82
+    {
83
+        $structure1 = new Structure($this->getTestData());
84
+
85
+        $structure2 = clone $structure1;
86
+
87
+        $this->assertEquals($structure1, $structure2);
88
+    }
89
+
90
+    /**
91
+     * Method to test __toString().
92
+     *
93
+     * @return void
94
+     *
95
+     * @covers \Windwalker\Structure\Structure::__toString
96
+     */
97
+    public function test__toString()
98
+    {
99
+        $this->assertJsonStringEqualsJsonString(json_encode($this->getTestData()), (string) $this->instance);
100
+    }
101
+
102
+    /**
103
+     * Method to test jsonSerialize().
104
+     *
105
+     * @return void
106
+     *
107
+     * @covers \Windwalker\Structure\Structure::jsonSerialize
108
+     */
109
+    public function testJsonSerialize()
110
+    {
111
+        $this->assertJsonStringEqualsJsonString(json_encode($this->getTestData()), (string) $this->instance);
112
+    }
113
+
114
+    /**
115
+     * Method to test def().
116
+     *
117
+     * @return void
118
+     *
119
+     * @covers \Windwalker\Structure\Structure::def
120
+     */
121
+    public function testDef()
122
+    {
123
+        $this->assertNull($this->instance->get('lily'));
124
+
125
+        $this->instance->def('lily', 'love');
126
+
127
+        $this->assertEquals('love', $this->instance->get('lily'));
128
+    }
129
+
130
+    /**
131
+     * Method to test exists().
132
+     *
133
+     * @return void
134
+     *
135
+     * @covers \Windwalker\Structure\Structure::exists
136
+     */
137
+    public function testExists()
138
+    {
139
+        $this->assertFalse($this->instance->exists('rose'));
140
+        $this->assertTrue($this->instance->exists('flower'));
141
+    }
142
+
143
+    /**
144
+     * Method to test get().
145
+     *
146
+     * @return void
147
+     *
148
+     * @covers \Windwalker\Structure\Structure::get
149
+     */
150
+    public function testGet()
151
+    {
152
+        $this->assertEquals($this->instance->get('flower', 'canna'), 'sakura');
153
+
154
+        $this->assertEquals($this->instance->get('not.exists', 'canna'), 'canna');
155
+
156
+        $this->assertNull($this->instance->get('not.exists'));
157
+
158
+        $this->assertEquals($this->instance->get('pos1.sunflower'), 'love');
159
+    }
160
+
161
+    /**
162
+     * Method to test loadArray().
163
+     *
164
+     * @return void
165
+     *
166
+     * @covers \Windwalker\Structure\Structure::load
167
+     */
168
+    public function testLoadArray()
169
+    {
170
+        $structure = new Structure();
171
+
172
+        $structure->load($this->getTestData());
173
+
174
+        $this->assertEquals($structure->get('olive'), 'peace');
175
+
176
+        $this->assertEquals($structure->get('pos1.sunflower'), 'love');
177
+    }
178
+
179
+    /**
180
+     * Method to test loadObject().
181
+     *
182
+     * @return void
183
+     *
184
+     * @covers \Windwalker\Structure\Structure::load
185
+     */
186
+    public function testLoadObject()
187
+    {
188
+        $structure = new Structure();
189
+
190
+        $structure->load((object) $this->getTestData());
191
+
192
+        $this->assertEquals($structure->get('olive'), 'peace');
193
+
194
+        $this->assertEquals($structure->get('pos1.sunflower'), 'love');
195
+    }
196
+
197
+    /**
198
+     * Method to test loadFile().
199
+     *
200
+     * @return void
201
+     *
202
+     * @covers \Windwalker\Structure\Structure::loadFile
203
+     */
204
+    public function testLoadFile()
205
+    {
206
+        $structure = new Structure();
207
+
208
+        $this->assertEquals(
209
+            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.json', 'json')->get('flower'),
210
+            'sakura'
211
+        );
212
+        $this->assertEquals(
213
+            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.yml', 'yaml')->get('flower'),
214
+            'sakura'
215
+        );
216
+        $this->assertEquals(
217
+            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.ini', 'ini')->get('flower'),
218
+            'sakura'
219
+        );
220
+        $this->assertEquals(
221
+            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.xml', 'xml')->get('flower'),
222
+            'sakura'
223
+        );
224
+        $this->assertEquals(
225
+            $structure->reset()->loadFile(__DIR__ . '/Stubs/flower.php', 'php')->get('flower'),
226
+            'sakura'
227
+        );
228
+    }
229
+
230
+    /**
231
+     * Method to test loadString().
232
+     *
233
+     * @return void
234
+     *
235
+     * @covers \Windwalker\Structure\Structure::loadString
236
+     * @throws \Exception
237
+     */
238
+    public function testLoadString()
239
+    {
240
+        $structure = new Structure();
241
+
242
+        $this->assertEquals(
243
+            $structure->reset()->loadString(
244
+                file_get_contents(__DIR__ . '/Stubs/flower.json'),
245
+                'json'
246
+            )->get('flower'),
247
+            'sakura'
248
+        );
249
+        $this->assertEquals(
250
+            $structure->reset()->loadString(
251
+                file_get_contents(__DIR__ . '/Stubs/flower.yml'),
252
+                'yaml'
253
+            )->get('flower'),
254
+            'sakura'
255
+        );
256
+        $this->assertEquals(
257
+            $structure->reset()->loadString(
258
+                file_get_contents(__DIR__ . '/Stubs/flower.ini'),
259
+                'ini'
260
+            )->get('flower'),
261
+            'sakura'
262
+        );
263
+        $this->assertEquals(
264
+            $structure->reset()->loadString(
265
+                file_get_contents(__DIR__ . '/Stubs/flower.xml'),
266
+                'xml'
267
+            )->get('flower'),
268
+            'sakura'
269
+        );
270
+        $this->assertEquals(
271
+            $structure->reset()->loadString(
272
+                file_get_contents(__DIR__ . '/Stubs/flower.hjson'),
273
+                'hjson'
274
+            )->get('flower'),
275
+            'sakura'
276
+        );
277
+        $this->assertEquals(
278
+            $structure->reset()->loadString(
279
+                file_get_contents(__DIR__ . '/Stubs/flower.toml'),
280
+                'toml',
281
+                ['load_raw' => true]
282
+            )->get('flower'),
283
+            'sakura'
284
+        );
285
+    }
286
+
287
+    /**
288
+     * Method to test merge().
289
+     *
290
+     * @return void
291
+     *
292
+     * @covers \Windwalker\Structure\Structure::merge
293
+     * @throws \Exception
294
+     */
295
+    public function testMerge()
296
+    {
297
+        // Test recursive merge
298
+        $object1 = '{
299
+            "foo" : "foo value",
300
+            "bar" : {
301
+                "bar1" : "bar value 1",
302
+                "bar2" : "bar value 2",
303
+                "bar3" : "bar value 3"
304
+            }
305
+        }';
306
+        $object2 = '{
307
+            "foo" : "foo value",
308
+            "bar" : {
309
+                "bar2" : "new bar value 2",
310
+                "bar3" : null
311
+            }
312
+        }';
313
+
314
+        $structure1 = new Structure(json_decode($object1));
315
+        $structure2 = new Structure(json_decode($object2));
316
+
317
+        $structure1->merge($structure2);
318
+
319
+        $this->assertEquals(
320
+            'new bar value 2',
321
+            $structure1->get('bar.bar2'),
322
+            'Line: ' . __LINE__ . '. bar.bar2 should be override.'
323
+        );
324
+        $this->assertEquals(
325
+            'bar value 1',
326
+            $structure1->get('bar.bar1'),
327
+            'Line: ' . __LINE__ . '. bar.bar1 should not be override.'
328
+        );
329
+        $this->assertSame(
330
+            'bar value 3',
331
+            $structure1->get('bar.bar3'),
332
+            'Line: ' . __LINE__ . '. bar.bar3 should not be override.'
333
+        );
334
+
335
+        $structure = new Structure(['flower' => 'rose', 'honor' => 'Osmanthus month']);
336
+
337
+        $structure->merge($this->instance);
338
+
339
+        $this->assertEquals($structure->get('flower'), 'sakura');
340
+        $this->assertEquals($structure->get('honor'), 'Osmanthus month');
341
+    }
342
+
343
+    /**
344
+     * Method to test merge().
345
+     *
346
+     * @return void
347
+     *
348
+     * @covers \Windwalker\Structure\Structure::merge
349
+     */
350
+    public function testMergeWithIgnoreValues()
351
+    {
352
+        // Test recursive merge
353
+        $object1 = '{
354
+            "foo" : "foo value",
355
+            "bar" : {
356
+                "bar1" : "bar value 1",
357
+                "bar2" : "bar value 2",
358
+                "bar3" : "bar value 3"
359
+            }
360
+        }';
361
+        $object2 = '{
362
+            "foo" : "foo value",
363
+            "bar" : {
364
+                "bar2" : "new bar value 2",
365
+                "bar3" : ""
366
+            }
367
+        }';
368
+
369
+        $structure1 = new Structure(json_decode($object1));
370
+        $structure2 = new Structure(json_decode($object2));
371
+
372
+        $structure1->setIgnoreValues([null, '']);
373
+        $structure1->merge($structure2);
374
+
375
+        $this->assertEquals(
376
+            'new bar value 2',
377
+            $structure1->get('bar.bar2'),
378
+            'Line: ' . __LINE__ . '. bar.bar2 should be override.'
379
+        );
380
+        $this->assertEquals(
381
+            'bar value 1',
382
+            $structure1->get('bar.bar1'),
383
+            'Line: ' . __LINE__ . '. bar.bar1 should not be override.'
384
+        );
385
+        $this->assertSame(
386
+            'bar value 3',
387
+            $structure1->get('bar.bar3'),
388
+            'Line: ' . __LINE__ . '. bar.bar3 should not be override.'
389
+        );
390
+
391
+        $structure = new Structure(['flower' => 'rose', 'honor' => 'Osmanthus month']);
392
+
393
+        $structure->merge($this->instance);
394
+
395
+        $this->assertEquals($structure->get('flower'), 'sakura');
396
+        $this->assertEquals($structure->get('honor'), 'Osmanthus month');
397
+    }
398
+
399
+    /**
400
+     * testMergeTo
401
+     *
402
+     * @return  void
403
+     *
404
+     * @covers \Windwalker\Structure\Structure::mergeTo
405
+     */
406
+    public function testMergeTo()
407
+    {
408
+        $structure = new Structure(['sunflower' => 'shine', 'honor' => 'Osmanthus month']);
409
+
410
+        $this->instance->mergeTo('pos1', $structure);
411
+
412
+        $this->assertEquals($this->instance->get('pos1.sunflower'), 'shine');
413
+        $this->assertEquals($this->instance->get('pos1.honor'), 'Osmanthus month');
414
+
415
+        $this->instance->mergeTo('foo.bar', $structure);
416
+
417
+        $this->assertEquals($this->instance->get('foo.bar.sunflower'), 'shine');
418
+        $this->assertEquals($this->instance->get('foo.bar.honor'), 'Osmanthus month');
419
+    }
420
+
421
+    /**
422
+     * Method to test offsetExists().
423
+     *
424
+     * @return void
425
+     *
426
+     * @covers \Windwalker\Structure\Structure::offsetExists
427
+     */
428
+    public function testOffsetExists()
429
+    {
430
+        $this->assertTrue(isset($this->instance['flower']));
431
+        $this->assertFalse(isset($this->instance['carbon']));
432
+    }
433
+
434
+    /**
435
+     * Method to test offsetGet().
436
+     *
437
+     * @return void
438
+     *
439
+     * @covers \Windwalker\Structure\Structure::offsetGet
440
+     */
441
+    public function testOffsetGet()
442
+    {
443
+        $this->assertEquals($this->instance['flower'], 'sakura');
444
+    }
445
+
446
+    /**
447
+     * Method to test offsetSet().
448
+     *
449
+     * @return void
450
+     *
451
+     * @covers \Windwalker\Structure\Structure::offsetSet
452
+     */
453
+    public function testOffsetSet()
454
+    {
455
+        $this->instance['bird'] = 'flying';
456
+
457
+        $this->assertEquals($this->instance['bird'], 'flying');
458
+    }
459
+
460
+    /**
461
+     * Method to test offsetUnset().
462
+     *
463
+     * @return void
464
+     *
465
+     * @covers \Windwalker\Structure\Structure::offsetUnset
466
+     */
467
+    public function testOffsetUnset()
468
+    {
469
+        unset($this->instance['bird']);
470
+
471
+        $this->assertEquals($this->instance['bird'], null);
472
+    }
473
+
474
+    /**
475
+     * Method to test set().
476
+     *
477
+     * @return void
478
+     *
479
+     * @covers \Windwalker\Structure\Structure::set
480
+     */
481
+    public function testSet()
482
+    {
483
+        $this->instance->set('tree.bird', 'sleeping');
484
+
485
+        $this->assertEquals($this->instance->get('tree.bird'), 'sleeping');
486
+    }
487
+
488
+    /**
489
+     * Method to test setRaw()
490
+     *
491
+     * @return  void
492
+     *
493
+     * @covers \Windwalker\Structure\Structure::setRaw
494
+     */
495
+    public function testSetRaw()
496
+    {
497
+        $object = (object) ['foo' => 'bar'];
498
+
499
+        $this->instance->setRaw('tree.bird', $object);
500
+
501
+        $this->assertEquals('bar', $this->instance->get('tree.bird.foo'));
502
+        $this->assertSame($object, $this->instance->get('tree.bird'));
503
+    }
504
+
505
+    /**
506
+     * Method to test toArray().
507
+     *
508
+     * @return void
509
+     *
510
+     * @covers \Windwalker\Structure\Structure::toArray
511
+     */
512
+    public function testToArray()
513
+    {
514
+        $structure = new Structure($this->getTestData());
515
+
516
+        $this->assertEquals($structure->toArray(), $this->getTestData());
517
+    }
518
+
519
+    /**
520
+     * Method to test toObject().
521
+     *
522
+     * @return void
523
+     *
524
+     * @covers \Windwalker\Structure\Structure::toObject
525
+     */
526
+    public function testToObject()
527
+    {
528
+        $structure = new Structure($this->getTestData());
529
+
530
+        $this->assertEquals($structure->toObject(), StructureHelper::toObject($this->getTestData()));
531
+    }
532
+
533
+    /**
534
+     * Method to test toString().
535
+     *
536
+     * @return void
537
+     *
538
+     * @covers \Windwalker\Structure\Structure::toString
539
+     */
540
+    public function testToString()
541
+    {
542
+        $structure = new Structure($this->getTestData());
543
+
544
+        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.ini'), $structure->toString('ini'));
545
+        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.json'), $structure->toString('json'));
546
+        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.yml'), $structure->toString('yml'));
547
+        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.xml'), $structure->toString('xml'));
548
+        $this->assertStringSafeEquals($this->loadFile(__DIR__ . '/Stubs/flower.php'), $structure->toString('php', ['strict' => true]));
549
+    }
550
+
551
+    /**
552
+     * Method to test flatten().
553
+     *
554
+     * @return void
555
+     *
556
+     * @covers \Windwalker\Structure\Structure::flatten
557
+     */
558
+    public function testFlatten()
559
+    {
560
+        $flatted = $this->instance->flatten();
561
+
562
+        $this->assertEquals($flatted['pos1.sunflower'], 'love');
563
+
564
+        $flatted = $this->instance->flatten('/');
565
+
566
+        $this->assertEquals($flatted['pos1/sunflower'], 'love');
567
+    }
568
+
569
+    /**
570
+     * testAppend
571
+     *
572
+     * @return  void
573
+     *
574
+     * @covers \Windwalker\Structure\Structure::push
575
+     */
576
+    public function testPush()
577
+    {
578
+        $structure = new Structure();
579
+
580
+        $structure->set('foo', ['var1', 'var2', 'var3']);
581
+
582
+        $structure->push('foo', 'var4');
583
+
584
+        $this->assertEquals('var4', $structure->get('foo.3'));
585
+
586
+        $structure->push('foo', 'var5', 'var6');
587
+
588
+        $this->assertEquals('var5', $structure->get('foo.4'));
589
+        $this->assertEquals('var6', $structure->get('foo.5'));
590
+
591
+        $structure->setRaw('foo2', (object) ['var1', 'var2', 'var3']);
592
+
593
+        $b = $structure->get('foo2');
594
+
595
+        $this->assertTrue(is_object($b));
596
+
597
+        $structure->push('foo2', 'var4');
598
+
599
+        $b = $structure->get('foo2');
600
+
601
+        $this->assertTrue(is_array($b));
602
+    }
603
+
604
+    /**
605
+     * testShift
606
+     *
607
+     * @return  void
608
+     *
609
+     * @covers \Windwalker\Structure\Structure::shift
610
+     */
611
+    public function testShift()
612
+    {
613
+        $structure = new Structure();
614
+
615
+        $structure->set('foo.bar', ['var1', 'var2', 'var3']);
616
+
617
+        $this->assertEquals('var1', $structure->shift('foo.bar'));
618
+
619
+        $this->assertEquals('var2', $structure->get('foo.bar.0'));
620
+
621
+        $structure->setRaw('foo.bar2', (object) ['v1' => 'var1', 'v2' => 'var2', 'v3' => 'var3']);
622
+
623
+        $this->assertEquals('var1', $structure->shift('foo.bar2'));
624
+
625
+        $this->assertEquals('var2', $structure->get('foo.bar2.v2'));
626
+
627
+        $this->assertTrue(is_array($structure->get('foo.bar2')));
628
+    }
629
+
630
+    /**
631
+     * testPop
632
+     *
633
+     * @return  void
634
+     *
635
+     * @covers \Windwalker\Structure\Structure::pop
636
+     */
637
+    public function testPop()
638
+    {
639
+        $structure = new Structure();
640
+
641
+        $structure->set('foo.bar', ['var1', 'var2', 'var3']);
642
+
643
+        $this->assertEquals('var3', $structure->pop('foo.bar'));
644
+
645
+        $this->assertNull($structure->get('foo.bar.2'));
646
+
647
+        $structure->setRaw('foo.bar2', (object) ['v1' => 'var1', 'v2' => 'var2', 'v3' => 'var3']);
648
+
649
+        $this->assertEquals('var3', $structure->pop('foo.bar2'));
650
+
651
+        $this->assertNull($structure->get('foo.bar2.v3'));
652
+
653
+        $this->assertTrue(is_array($structure->get('foo.bar2')));
654
+    }
655
+
656
+    /**
657
+     * testUnshift
658
+     *
659
+     * @return  void
660
+     *
661
+     * @covers \Windwalker\Structure\Structure::unshift
662
+     */
663
+    public function testUnshift()
664
+    {
665
+        $structure = new Structure();
666
+
667
+        $structure->set('foo', ['var1', 'var2', 'var3']);
668
+
669
+        $structure->unshift('foo', 'var4');
670
+
671
+        $this->assertEquals('var4', $structure->get('foo.0'));
672
+
673
+        $structure->unshift('foo', 'var5', 'var6');
674
+
675
+        $this->assertEquals('var5', $structure->get('foo.0'));
676
+        $this->assertEquals('var6', $structure->get('foo.1'));
677
+
678
+        $structure->setRaw('foo2', (object) ['var1', 'var2', 'var3']);
679
+
680
+        $b = $structure->get('foo2');
681
+
682
+        $this->assertTrue(is_object($b));
683
+
684
+        $structure->unshift('foo2', 'var4');
685
+
686
+        $b = $structure->get('foo2');
687
+
688
+        $this->assertTrue(is_array($b));
689
+    }
690
+
691
+    /**
692
+     * testReset
693
+     *
694
+     * @return  void
695
+     *
696
+     * @covers  \Windwalker\Structure\Structure::reset
697
+     */
698
+    public function testReset()
699
+    {
700
+        $this->instance->reset();
701
+
702
+        $this->assertEquals([], $this->instance->getRaw());
703
+    }
704
+
705
+    /**
706
+     * testGetRaw
707
+     *
708
+     * @return  void
709
+     *
710
+     * @covers  \Windwalker\Structure\Structure::getRaw
711
+     */
712
+    public function testGetRaw()
713
+    {
714
+        $this->assertEquals($this->getTestData(), $this->instance->getRaw());
715
+    }
716
+
717
+    /**
718
+     * testGetIterator
719
+     *
720
+     * @return  void
721
+     *
722
+     * @covers  \Windwalker\Structure\Structure::getIterator
723
+     */
724
+    public function testGetIterator()
725
+    {
726
+        $this->assertInstanceOf('RecursiveArrayIterator', $this->instance->getIterator());
727
+
728
+        $this->assertEquals($this->getTestData(), iterator_to_array($this->instance));
729
+        $this->assertEquals(
730
+            iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->getTestData()))),
731
+            iterator_to_array(new \RecursiveIteratorIterator($this->instance))
732
+        );
733
+    }
734
+
735
+    /**
736
+     * testCount
737
+     *
738
+     * @return  void
739
+     *
740
+     * @covers  \Windwalker\Structure\Structure::count
741
+     */
742
+    public function testCount()
743
+    {
744
+        $this->assertEquals(5, count($this->instance));
745
+    }
746
+
747
+    /**
748
+     * loadFile
749
+     *
750
+     * @param string $file
751
+     *
752
+     * @return  string
753
+     */
754
+    protected function loadFile($file)
755
+    {
756
+        $text = file_get_contents($file);
757
+
758
+        return $text;
759
+    }
760
+}