diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e6e0692..57153c82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * refactor: refactor `ObjectToObjectTransformer` for future optimization * fix: remove unalterable check in `ObjectProcessor` to allow transformers to handle the current target value in all cases +* test: test lazy constructor ## 1.11.0 diff --git a/tests/config/rekalogika-mapper/generated-mappings.php b/tests/config/rekalogika-mapper/generated-mappings.php index ab866e22..f1b033c7 100644 --- a/tests/config/rekalogika-mapper/generated-mappings.php +++ b/tests/config/rekalogika-mapper/generated-mappings.php @@ -448,41 +448,47 @@ ); $mappingCollection->addObjectMapping( - // tests/src/IntegrationTest/LazyObjectTest.php on lines 98, 108 + // tests/src/IntegrationTest/LazyObjectTest.php on lines 99, 109 source: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithId::class, target: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ChildObjectWithIdDto::class ); $mappingCollection->addObjectMapping( - // tests/src/IntegrationTest/LazyObjectTest.php on lines 36, 45 + // tests/src/IntegrationTest/LazyObjectTest.php on lines 37, 46 source: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithId::class, target: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdDto::class ); $mappingCollection->addObjectMapping( - // tests/src/IntegrationTest/LazyObjectTest.php on line 64 + // tests/src/IntegrationTest/LazyObjectTest.php on line 65 source: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithId::class, target: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdEagerDto::class ); $mappingCollection->addObjectMapping( - // tests/src/IntegrationTest/LazyObjectTest.php on line 55 + // tests/src/IntegrationTest/LazyObjectTest.php on line 56 source: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithId::class, target: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdFinalDto::class ); $mappingCollection->addObjectMapping( - // tests/src/IntegrationTest/LazyObjectTest.php on line 80 + // tests/src/IntegrationTest/LazyObjectTest.php on line 81 source: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithId::class, target: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdReadOnlyDto::class ); $mappingCollection->addObjectMapping( - // tests/src/IntegrationTest/LazyObjectTest.php on line 118 - source: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdAndNameMustBeCalled::class, + // tests/src/IntegrationTest/LazyObjectTest.php on line 119 + source: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdAndName::class, target: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdAndNameInConstructorDto::class ); + $mappingCollection->addObjectMapping( + // tests/src/IntegrationTest/LazyObjectTest.php on line 135 + source: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdAndName::class, + target: \Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdInConstructorDto::class + ); + $mappingCollection->addObjectMapping( // tests/src/IntegrationTest/MagicTest.php on line 26 source: \Rekalogika\Mapper\Tests\Fixtures\Magic\ObjectWithMagicGet::class, @@ -886,7 +892,7 @@ ); $mappingCollection->addObjectMapping( - // tests/src/IntegrationTest/LazyObjectTest.php on lines 134, 144 + // tests/src/IntegrationTest/LazyObjectTest.php on lines 154, 164 // tests/src/IntegrationTest/ScalarToScalar/ScalarPropertiesMappingTestCase.php on line 31 source: \Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties::class, target: \Rekalogika\Mapper\Tests\Fixtures\ScalarDto\ObjectWithScalarPropertiesDto::class diff --git a/tests/src/Fixtures/LazyObject/ObjectWithIdAndNameMustBeCalled.php b/tests/src/Fixtures/LazyObject/ObjectWithIdAndName.php similarity index 80% rename from tests/src/Fixtures/LazyObject/ObjectWithIdAndNameMustBeCalled.php rename to tests/src/Fixtures/LazyObject/ObjectWithIdAndName.php index b93b53ff..d22d825f 100644 --- a/tests/src/Fixtures/LazyObject/ObjectWithIdAndNameMustBeCalled.php +++ b/tests/src/Fixtures/LazyObject/ObjectWithIdAndName.php @@ -13,7 +13,7 @@ namespace Rekalogika\Mapper\Tests\Fixtures\LazyObject; -class ObjectWithIdAndNameMustBeCalled +class ObjectWithIdAndName { private bool $idCalled = false; @@ -42,4 +42,14 @@ public function isIdAndNameCalled(): bool { return $this->idCalled && $this->nameCalled; } + + public function isIdCalled(): bool + { + return $this->idCalled; + } + + public function isNameCalled(): bool + { + return $this->nameCalled; + } } diff --git a/tests/src/Fixtures/LazyObject/ObjectWithIdInConstructorDto.php b/tests/src/Fixtures/LazyObject/ObjectWithIdInConstructorDto.php new file mode 100644 index 00000000..01ae3425 --- /dev/null +++ b/tests/src/Fixtures/LazyObject/ObjectWithIdInConstructorDto.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\LazyObject; + +class ObjectWithIdInConstructorDto +{ + public function __construct( + public string $id, + ) {} + + public ?string $name = null; + public ?string $other = null; +} diff --git a/tests/src/IntegrationTest/LazyObjectTest.php b/tests/src/IntegrationTest/LazyObjectTest.php index 1f5cd7ba..cfd1fe50 100644 --- a/tests/src/IntegrationTest/LazyObjectTest.php +++ b/tests/src/IntegrationTest/LazyObjectTest.php @@ -19,10 +19,11 @@ use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ChildObjectWithIdDto; use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithId; use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdAndNameInConstructorDto; -use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdAndNameMustBeCalled; +use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdAndName; use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdDto; use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdEagerDto; use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdFinalDto; +use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdInConstructorDto; use Rekalogika\Mapper\Tests\Fixtures\LazyObject\ObjectWithIdReadOnlyDto; use Rekalogika\Mapper\Tests\Fixtures\Scalar\ObjectWithScalarProperties; use Rekalogika\Mapper\Tests\Fixtures\ScalarDto\ObjectWithScalarPropertiesDto; @@ -114,7 +115,7 @@ public function testIdInParentClassInitialized(): void */ public function testEagerAndLazyPropertyInConstruct(): void { - $source = new ObjectWithIdAndNameMustBeCalled(); + $source = new ObjectWithIdAndName(); $target = $this->mapper->map($source, ObjectWithIdAndNameInConstructorDto::class); $this->assertInstanceOf(LazyObjectInterface::class, $target); @@ -125,6 +126,25 @@ public function testEagerAndLazyPropertyInConstruct(): void $this->assertTrue($target->isLazyObjectInitialized()); } + /** + * If the constructor has only lazy properties, the constructor is lazy. + */ + public function testLazyPropertyOnlyInConstruct(): void + { + $source = new ObjectWithIdAndName(); + $target = $this->mapper->map($source, ObjectWithIdInConstructorDto::class); + $this->assertInstanceOf(LazyObjectInterface::class, $target); + + $this->assertTrue($source->isIdCalled()); + $this->assertFalse($source->isNameCalled()); + $this->assertFalse($target->isLazyObjectInitialized()); + + $name = $target->name; + $this->assertTrue($target->isLazyObjectInitialized()); + $this->assertTrue($source->isNameCalled()); + $this->assertEquals('name', $name); + } + public function testEnablingLazyObject(): void { $options = new MapperOptions(lazyLoading: true);