77use Emul \ArrayToClassMapper \Mapper ;
88use Emul \ArrayToClassMapper \DocBlock \DocBlockParser ;
99use Emul \ArrayToClassMapper \DocBlock \Entity \DocBlockType ;
10- use Emul \ArrayToClassMapper \Test \Unit \Stub \CustomDocBlockTypeArrayStub ;
10+ use Emul \ArrayToClassMapper \Test \Unit \Stub \ClassDocBlockTypedArrayStub ;
11+ use Emul \ArrayToClassMapper \Test \Unit \Stub \CustomDocBlockTypedArrayStub ;
1112use Emul \ArrayToClassMapper \Test \Unit \Stub \CustomDocBlockTypedStub ;
13+ use Emul \ArrayToClassMapper \Test \Unit \Stub \CustomStub ;
1214use Emul \ArrayToClassMapper \Test \Unit \Stub \CustomTypedStub ;
13- use Emul \ArrayToClassMapper \Test \Unit \Stub \ScalarDocBlockTypeArrayStub ;
15+ use Emul \ArrayToClassMapper \Test \Unit \Stub \ScalarDocBlockTypedArrayStub ;
1416use Emul \ArrayToClassMapper \Test \Unit \Stub \ScalarDocBlockTypedStub ;
1517use Emul \ArrayToClassMapper \Test \Unit \Stub \ScalarTypedStub ;
1618use Emul \ArrayToClassMapper \Test \Unit \Stub \TypelessArrayStub ;
@@ -67,8 +69,8 @@ public function testMapWhenArrayTypedPropertyGivenWithBuiltInDockBlockType_shoul
6769 'scalarTypedArray ' => ['1 ' , '2 ' ],
6870 ];
6971
70- /** @var ScalarDocBlockTypeArrayStub $result */
71- $ result = $ mapper ->map ($ input , ScalarDocBlockTypeArrayStub ::class);
72+ /** @var ScalarDocBlockTypedArrayStub $result */
73+ $ result = $ mapper ->map ($ input , ScalarDocBlockTypedArrayStub ::class);
7274
7375 $ this ->assertSame ([1 , 2 ], $ result ->getScalarTypedArray ());
7476 }
@@ -84,15 +86,15 @@ public function testMapWhenArrayTypedPropertyGivenWithCustomDockBlockType_should
8486 );
8587
8688 $ input = [
87- 'customArray ' => [
89+ 'objectArray ' => [
8890 ['int ' => 1 ],
8991 ['int ' => 2 ],
9092 ],
9193 ];
9294
93- /** @var CustomDocBlockTypeArrayStub $result */
94- $ result = $ mapper ->map ($ input , CustomDocBlockTypeArrayStub ::class);
95- $ mappedArray = $ result ->getCustomArray ();
95+ /** @var ClassDocBlockTypedArrayStub $result */
96+ $ result = $ mapper ->map ($ input , ClassDocBlockTypedArrayStub ::class);
97+ $ mappedArray = $ result ->getObjectArray ();
9698
9799 $ this ->assertCount (2 , $ mappedArray );
98100 $ this ->assertSame (1 , $ mappedArray [0 ]->getInt ());
@@ -167,6 +169,39 @@ public function testMapWhenCustomDocBlockTypedPropertyGiven_shouldMapWithGivenMa
167169 $ this ->assertSame ($ currentTime , $ result ->getCurrentTime ()->toDateTimeString ());
168170 }
169171
172+ public function testMapWhenCustomDocBlockTypedArrayPropertyGiven_shouldMapWithGivenMapper ()
173+ {
174+ $ this ->expectTypeRetrievedFromDocBlock (
175+ '/** @var \Emul\ArrayToClassMapper\Test\Unit\Stub\CustomStub[] */ ' ,
176+ new DocBlockType ('\Emul\ArrayToClassMapper\Test\Unit\Stub\CustomStub ' , false , false , false )
177+ );
178+ $ this ->expectTypeRetrievedFromDocBlock ('' , null );
179+
180+ $ input = [
181+ 'objectArray ' => [
182+ ['key ' => 'first ' , 'value ' => '1 ' ],
183+ ['key ' => 'second ' , 'value ' => '2 ' ],
184+ ]
185+ ];
186+ $ customMapper = \Closure::fromCallable (function (array $ data ) {
187+ return new CustomStub ('prefix_ ' , $ data ['key ' ], $ data ['value ' ]);
188+ });
189+
190+ $ mapper = $ this ->getMapper ();
191+ $ mapper ->addCustomMapper (CustomStub::class, $ customMapper );
192+
193+ /** @var CustomDocBlockTypedArrayStub $result */
194+ $ result = $ mapper ->map ($ input , CustomDocBlockTypedArrayStub::class);
195+
196+ $ this ->assertCount (2 , $ result ->getObjectArray ());
197+ $ this ->assertInstanceOf (CustomStub::class, $ result ->getObjectArray ()[0 ]);
198+ $ this ->assertInstanceOf (CustomStub::class, $ result ->getObjectArray ()[1 ]);
199+ $ this ->assertSame ('prefix_first ' , $ result ->getObjectArray ()[0 ]->getKey ());
200+ $ this ->assertSame ('prefix_1 ' , $ result ->getObjectArray ()[0 ]->getValue ());
201+ $ this ->assertSame ('prefix_second ' , $ result ->getObjectArray ()[1 ]->getKey ());
202+ $ this ->assertSame ('prefix_2 ' , $ result ->getObjectArray ()[1 ]->getValue ());
203+ }
204+
170205 private function expectTypeRetrievedFromDocBlock (string $ docBlock , ?DocBlockType $ expectedResult )
171206 {
172207 $ this ->docBlockParser ->shouldReceive ('getType ' )->with ($ docBlock )->andReturn ($ expectedResult );
0 commit comments