|
10 | 10 | use SergiX44\Hydrator\Exception\InvalidObjectException; |
11 | 11 | use SergiX44\Hydrator\Hydrator; |
12 | 12 | use SergiX44\Hydrator\HydratorInterface; |
| 13 | +use SergiX44\Hydrator\Tests\Fixtures\DI\Forest; |
| 14 | +use SergiX44\Hydrator\Tests\Fixtures\DI\Leaves; |
13 | 15 | use SergiX44\Hydrator\Tests\Fixtures\DI\Sun; |
14 | 16 | use SergiX44\Hydrator\Tests\Fixtures\DI\Tree; |
| 17 | +use SergiX44\Hydrator\Tests\Fixtures\DI\Wood; |
15 | 18 | use SergiX44\Hydrator\Tests\Fixtures\ObjectWithAbstract; |
16 | 19 | use SergiX44\Hydrator\Tests\Fixtures\ObjectWithInvalidAbstract; |
17 | 20 | use SergiX44\Hydrator\Tests\Fixtures\Resolver\AppleResolver; |
@@ -830,4 +833,65 @@ public function testHydrateWithContainer(): void |
830 | 833 | $this->assertSame($sun, $o->leaves->getSun()); |
831 | 834 | $this->assertSame($sun, $o->wood->getSun()); |
832 | 835 | } |
| 836 | + |
| 837 | + public function testHydrateWithContainerWithNestedInstances(): void |
| 838 | + { |
| 839 | + $sun = new Sun('andromeda'); |
| 840 | + |
| 841 | + $container = new Container(); |
| 842 | + $container->delegate(new ReflectionContainer()); |
| 843 | + $container->addShared(Sun::class, $sun); |
| 844 | + |
| 845 | + $hydrator = new Hydrator($container); |
| 846 | + |
| 847 | + $o = $hydrator->hydrate(Forest::class, [ |
| 848 | + 'trees' => [ |
| 849 | + [ |
| 850 | + 'name' => 'foo', |
| 851 | + 'leaves' => [ |
| 852 | + 'n' => 100, |
| 853 | + ], |
| 854 | + 'wood' => [ |
| 855 | + 'kg' => 120, |
| 856 | + ], |
| 857 | + ], |
| 858 | + [ |
| 859 | + 'name' => 'foo2', |
| 860 | + 'leaves' => [ |
| 861 | + 'n' => 200, |
| 862 | + ], |
| 863 | + 'wood' => [ |
| 864 | + 'kg' => 220, |
| 865 | + ], |
| 866 | + ], |
| 867 | + ], |
| 868 | + ]); |
| 869 | + |
| 870 | + $this->assertIsArray($o->trees); |
| 871 | + $this->assertcount(2, $o->trees); |
| 872 | + |
| 873 | + $this->assertSame('foo', $o->trees[0]->name); |
| 874 | + $this->assertInstanceOf(Leaves::class, $o->trees[0]->leaves); |
| 875 | + $this->assertSame(100, $o->trees[0]->leaves->n); |
| 876 | + $this->assertInstanceOf(Sun::class, $o->trees[0]->leaves->getSun()); |
| 877 | + $this->assertSame('andromeda', $o->trees[0]->leaves->getSun()->getFrom()); |
| 878 | + $this->assertInstanceOf(Wood::class, $o->trees[0]->wood); |
| 879 | + $this->assertSame(120, $o->trees[0]->wood->kg); |
| 880 | + $this->assertInstanceOf(Sun::class, $o->trees[0]->wood->getSun()); |
| 881 | + $this->assertSame('andromeda', $o->trees[0]->wood->getSun()->getFrom()); |
| 882 | + $this->assertInstanceOf(Sun::class, $o->trees[0]->getSun()); |
| 883 | + $this->assertSame('andromeda', $o->trees[0]->getSun()->getFrom()); |
| 884 | + |
| 885 | + $this->assertSame('foo2', $o->trees[1]->name); |
| 886 | + $this->assertInstanceOf(Leaves::class, $o->trees[1]->leaves); |
| 887 | + $this->assertSame(200, $o->trees[1]->leaves->n); |
| 888 | + $this->assertInstanceOf(Sun::class, $o->trees[1]->leaves->getSun()); |
| 889 | + $this->assertSame('andromeda', $o->trees[1]->leaves->getSun()->getFrom()); |
| 890 | + $this->assertInstanceOf(Wood::class, $o->trees[1]->wood); |
| 891 | + $this->assertSame(220, $o->trees[1]->wood->kg); |
| 892 | + $this->assertInstanceOf(Sun::class, $o->trees[1]->wood->getSun()); |
| 893 | + $this->assertSame('andromeda', $o->trees[1]->wood->getSun()->getFrom()); |
| 894 | + $this->assertInstanceOf(Sun::class, $o->trees[1]->getSun()); |
| 895 | + $this->assertSame('andromeda', $o->trees[1]->getSun()->getFrom()); |
| 896 | + } |
833 | 897 | } |
0 commit comments