diff --git a/tests/Fixtures/ArrayLike/LazyDoctrineCollection.php b/tests/Fixtures/ArrayLike/LazyDoctrineCollection.php new file mode 100644 index 0000000..ad2ce54 --- /dev/null +++ b/tests/Fixtures/ArrayLike/LazyDoctrineCollection.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ArrayLike; + +use Doctrine\Common\Collections\AbstractLazyCollection; + +/** + * @extends AbstractLazyCollection + */ +class LazyDoctrineCollection extends AbstractLazyCollection +{ + protected function doInitialize() + { + throw new \LogicException("Not expected to be initialized"); + } +} diff --git a/tests/Fixtures/ArrayLike/ObjectWithCollectionProperty.php b/tests/Fixtures/ArrayLike/ObjectWithCollectionProperty.php new file mode 100644 index 0000000..49204a3 --- /dev/null +++ b/tests/Fixtures/ArrayLike/ObjectWithCollectionProperty.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ArrayLike; + +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; +use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties; + +class ObjectWithCollectionProperty +{ + /** + * @var Collection + */ + public Collection $property; + + public function __construct() + { + $this->property = new ArrayCollection(); + $this->property->add(new ObjectWithScalarProperties()); + $this->property->add(new ObjectWithScalarProperties()); + $this->property->add(new ObjectWithScalarProperties()); + } +} diff --git a/tests/Fixtures/ArrayLike/ObjectWithLazyDoctrineCollectionProperty.php b/tests/Fixtures/ArrayLike/ObjectWithLazyDoctrineCollectionProperty.php new file mode 100644 index 0000000..1343b15 --- /dev/null +++ b/tests/Fixtures/ArrayLike/ObjectWithLazyDoctrineCollectionProperty.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ArrayLike; + +class ObjectWithLazyDoctrineCollectionProperty +{ + public LazyDoctrineCollection $property; + + public function __construct() + { + $this->property = new LazyDoctrineCollection(); + } +} diff --git a/tests/Fixtures/ArrayLikeDto/ObjectWithCollectionPropertyDto.php b/tests/Fixtures/ArrayLikeDto/ObjectWithCollectionPropertyDto.php new file mode 100644 index 0000000..276fe1d --- /dev/null +++ b/tests/Fixtures/ArrayLikeDto/ObjectWithCollectionPropertyDto.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto; + +use Doctrine\Common\Collections\Collection; +use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarPropertiesDto; + +class ObjectWithCollectionPropertyDto +{ + /** + * @var ?Collection + */ + public ?Collection $property = null; +} diff --git a/tests/IntegrationTest/TraversableToArrayAccessMappingTest.php b/tests/IntegrationTest/TraversableToArrayAccessMappingTest.php index faeedf7..65941f2 100644 --- a/tests/IntegrationTest/TraversableToArrayAccessMappingTest.php +++ b/tests/IntegrationTest/TraversableToArrayAccessMappingTest.php @@ -13,6 +13,8 @@ namespace Rekalogika\Mapper\Tests\IntegrationTest; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Rekalogika\Mapper\Exception\MissingMemberValueTypeException; use Rekalogika\Mapper\Mapper; use Rekalogika\Mapper\Tests\Common\AbstractIntegrationTest; @@ -22,6 +24,7 @@ use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithArrayAccessPropertyDto; use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithArrayPropertyDto; use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithArrayPropertyDtoWithIntKey; +use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithCollectionPropertyDto; use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarPropertiesDto; class TraversableToArrayAccessMappingTest extends AbstractIntegrationTest @@ -134,6 +137,28 @@ public function testTraversableToArrayDto(): void $this->assertEquals(1.1, $one->d); } + public function testTraversableToCollectionDto(): void + { + $source = new ObjectWithTraversableProperties(); + + $result = $this->mapper->map($source, ObjectWithCollectionPropertyDto::class); + + $this->assertInstanceOf(ObjectWithCollectionPropertyDto::class, $result); + $property = $result->property; + $this->assertInstanceOf(Collection::class, $property); + $this->assertInstanceOf(ArrayCollection::class, $property); + + $property = $result->property; + + $member = $property?->get(1); + $this->assertInstanceOf(ObjectWithScalarPropertiesDto::class, $member); + + $this->assertEquals(1, $member->a); + $this->assertEquals("string", $member->b); + $this->assertEquals(true, $member->c); + $this->assertEquals(1.1, $member->d); + } + public function testArrayToArrayAccessDto(): void { $source = new ObjectWithArrayProperty(); @@ -163,6 +188,29 @@ public function testArrayToArrayDto(): void $this->assertEquals(1.1, $one->d); } + public function testArrayToCollectionDto(): void + { + $source = new ObjectWithArrayProperty(); + + $result = $this->mapper->map($source, ObjectWithCollectionPropertyDto::class); + + $this->assertInstanceOf(ObjectWithCollectionPropertyDto::class, $result); + $property = $result->property; + $this->assertInstanceOf(Collection::class, $property); + $this->assertInstanceOf(ArrayCollection::class, $property); + + $property = $result->property; + + $member = $property?->get(1); + $this->assertInstanceOf(ObjectWithScalarPropertiesDto::class, $member); + + $this->assertEquals(1, $member->a); + $this->assertEquals("string", $member->b); + $this->assertEquals(true, $member->c); + $this->assertEquals(1.1, $member->d); + } + + // // preinitialized // @@ -225,7 +273,7 @@ public function testArrayToArrayDtoPreInit(): void } // - // + // source has string key, target has int key // public function testSourceStringKeyToTargetIntKey(): void diff --git a/tests/IntegrationTest/TraversableToTraversableMappingTest.php b/tests/IntegrationTest/TraversableToTraversableMappingTest.php index 52ae7ae..e9d4fcb 100644 --- a/tests/IntegrationTest/TraversableToTraversableMappingTest.php +++ b/tests/IntegrationTest/TraversableToTraversableMappingTest.php @@ -15,6 +15,7 @@ use Rekalogika\Mapper\Tests\Common\AbstractIntegrationTest; use Rekalogika\Mapper\Tests\Fixtures\ArrayLike\ObjectWithArrayProperty; +use Rekalogika\Mapper\Tests\Fixtures\ArrayLike\ObjectWithLazyDoctrineCollectionProperty; use Rekalogika\Mapper\Tests\Fixtures\ArrayLike\ObjectWithTraversableProperties; use Rekalogika\Mapper\Tests\Fixtures\ArrayLikeDto\ObjectWithTraversablePropertyDto; use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarPropertiesDto; @@ -64,4 +65,18 @@ public function testArrayToTraversableDto(): void $this->assertEquals(1.1, $item->d); } } + + public function testLazy(): void + { + $source = new ObjectWithLazyDoctrineCollectionProperty(); + + $result = $this->mapper->map($source, ObjectWithTraversablePropertyDto::class); + + $this->assertInstanceOf(ObjectWithTraversablePropertyDto::class, $result); + $this->assertNotNull($result->property); + $this->assertInstanceOf(\Generator::class, $result->property); + + $this->expectException(\LogicException::class); + foreach ($result->property as $item); + } }