5
5
use InvalidArgumentException ;
6
6
use PHPUnit \Framework \TestCase ;
7
7
use SergiX44 \Hydrator \Exception ;
8
+ use SergiX44 \Hydrator \Exception \InvalidObjectException ;
8
9
use SergiX44 \Hydrator \Hydrator ;
9
10
use SergiX44 \Hydrator \HydratorInterface ;
11
+ use SergiX44 \Hydrator \Tests \Fixtures \ObjectWithAbstract ;
12
+ use SergiX44 \Hydrator \Tests \Fixtures \ObjectWithInvalidAbstract ;
13
+ use SergiX44 \Hydrator \Tests \Fixtures \Store \Apple ;
14
+ use SergiX44 \Hydrator \Tests \Fixtures \Store \AppleJack ;
15
+ use SergiX44 \Hydrator \Tests \Fixtures \Store \AppleSauce ;
16
+ use SergiX44 \Hydrator \Tests \Fixtures \Store \Fruit ;
10
17
use SergiX44 \Hydrator \Tests \Fixtures \Store \Tag ;
11
18
use SergiX44 \Hydrator \Tests \Fixtures \Store \TagPrice ;
12
19
use TypeError ;
@@ -127,7 +134,10 @@ public function testHydrateAttributedProperty(): void
127
134
$ this ->markTestSkipped ('php >= 8 is required. ' );
128
135
}
129
136
130
- $ object = (new Hydrator ())->hydrate (Fixtures \ObjectWithAttributedAlias::class, ['non-normalized-value ' => 'foo ' ]);
137
+ $ object = (new Hydrator ())->hydrate (
138
+ Fixtures \ObjectWithAttributedAlias::class,
139
+ ['non-normalized-value ' => 'foo ' ]
140
+ );
131
141
132
142
$ this ->assertSame ('foo ' , $ object ->value );
133
143
}
@@ -335,10 +345,12 @@ public function testHydrateArrayableProperty(): void
335
345
336
346
public function testHydrateTypedArrayableProperty (): void
337
347
{
338
- $ object = (new Hydrator ())->hydrate (Fixtures \ObjectWithTypedArray::class, ['value ' => [
339
- ['name ' => 'foo ' ],
340
- ['name ' => 'bar ' ],
341
- ]]);
348
+ $ object = (new Hydrator ())->hydrate (Fixtures \ObjectWithTypedArray::class, [
349
+ 'value ' => [
350
+ ['name ' => 'foo ' ],
351
+ ['name ' => 'bar ' ],
352
+ ],
353
+ ]);
342
354
343
355
$ this ->assertIsArray ($ object ->value );
344
356
$ this ->assertInstanceOf (Tag::class, $ object ->value [0 ]);
@@ -350,10 +362,12 @@ public function testHydrateTypedArrayableProperty(): void
350
362
351
363
public function testHydrateTypedNestedArrayableProperty (): void
352
364
{
353
- $ object = (new Hydrator ())->hydrate (Fixtures \ObjectWithTypedArrayOfArray::class, ['value ' => [
354
- [['name ' => 'foo ' ], ['name ' => 'fef ' ]],
355
- [['name ' => 'bar ' ], ['name ' => 'fif ' ]],
356
- ]]);
365
+ $ object = (new Hydrator ())->hydrate (Fixtures \ObjectWithTypedArrayOfArray::class, [
366
+ 'value ' => [
367
+ [['name ' => 'foo ' ], ['name ' => 'fef ' ]],
368
+ [['name ' => 'bar ' ], ['name ' => 'fif ' ]],
369
+ ],
370
+ ]);
357
371
358
372
$ this ->assertIsArray ($ object ->value );
359
373
$ this ->assertIsArray ($ object ->value [0 ]);
@@ -604,10 +618,12 @@ public function testHydrateAssociatedPropertyWithInvalidData(): void
604
618
605
619
public function testHydrateAssociationCollectionProperty (): void
606
620
{
607
- $ o = (new Hydrator ())->hydrate (Fixtures \ObjectWithAssociations::class, ['value ' => [
608
- 'foo ' => ['value ' => 'foo ' ],
609
- 'bar ' => ['value ' => 'bar ' ],
610
- ]]);
621
+ $ o = (new Hydrator ())->hydrate (Fixtures \ObjectWithAssociations::class, [
622
+ 'value ' => [
623
+ 'foo ' => ['value ' => 'foo ' ],
624
+ 'bar ' => ['value ' => 'bar ' ],
625
+ ],
626
+ ]);
611
627
612
628
$ this ->assertNotNull ($ o ->value ['foo ' ]);
613
629
$ this ->assertSame ('foo ' , $ o ->value ['foo ' ]->value );
@@ -618,10 +634,12 @@ public function testHydrateAssociationCollectionProperty(): void
618
634
619
635
public function testHydrateAssociationCollectionPropertyUsingDataObject (): void
620
636
{
621
- $ o = (new Hydrator ())->hydrate (Fixtures \ObjectWithAssociations::class, ['value ' => (object ) [
622
- 'foo ' => (object ) ['value ' => 'foo ' ],
623
- 'bar ' => (object ) ['value ' => 'bar ' ],
624
- ]]);
637
+ $ o = (new Hydrator ())->hydrate (Fixtures \ObjectWithAssociations::class, [
638
+ 'value ' => (object ) [
639
+ 'foo ' => (object ) ['value ' => 'foo ' ],
640
+ 'bar ' => (object ) ['value ' => 'bar ' ],
641
+ ],
642
+ ]);
625
643
626
644
$ this ->assertNotNull ($ o ->value ['foo ' ]);
627
645
$ this ->assertSame ('foo ' , $ o ->value ['foo ' ]->value );
@@ -721,4 +739,51 @@ public function testHydrateProductWithJsonAsObject(): void
721
739
$ this ->assertSame ('dccd816f-bb28-41f3-b1a9-ddaff1fdec5b ' , $ product ->tags [1 ]->name );
722
740
$ this ->assertSame (2 , $ product ->status ->value );
723
741
}
742
+
743
+ public function testHydrateAbstractObject (): void
744
+ {
745
+ $ o = (new Hydrator ())->hydrate (Apple::class, [
746
+ 'type ' => 'sauce ' ,
747
+ 'sweetness ' => 100 ,
748
+ 'category ' => null ,
749
+ ]);
750
+
751
+ $ this ->assertInstanceOf (AppleSauce::class, $ o );
752
+ $ this ->assertSame ('sauce ' , $ o ->type );
753
+ $ this ->assertSame (100 , $ o ->sweetness );
754
+ }
755
+
756
+ public function testHydrateAbstractObjectWithoutInterface (): void
757
+ {
758
+ $ this ->expectException (InvalidObjectException::class);
759
+
760
+ (new Hydrator ())->hydrate (Fruit::class, ['name ' => 'apple ' ]);
761
+ }
762
+
763
+ public function testHydrateAbstractProperty (): void
764
+ {
765
+ $ o = (new Hydrator ())->hydrate (new ObjectWithAbstract (), [
766
+ 'value ' => [
767
+ 'type ' => 'jack ' ,
768
+ 'sweetness ' => null ,
769
+ 'category ' => 'brandy ' ,
770
+ ],
771
+ ]);
772
+
773
+ $ this ->assertInstanceOf (ObjectWithAbstract::class, $ o );
774
+ $ this ->assertInstanceOf (AppleJack::class, $ o ->value );
775
+ $ this ->assertSame ('jack ' , $ o ->value ->type );
776
+ $ this ->assertSame ('brandy ' , $ o ->value ->category );
777
+ }
778
+
779
+ public function testHydrateInvalidAbstractObject (): void
780
+ {
781
+ $ this ->expectException (InvalidObjectException::class);
782
+
783
+ (new Hydrator ())->hydrate (new ObjectWithInvalidAbstract (), [
784
+ 'value ' => [
785
+ 'name ' => 'apple ' ,
786
+ ],
787
+ ]);
788
+ }
724
789
}
0 commit comments