Skip to content

Commit

Permalink
test: test lazy constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Oct 15, 2024
1 parent 760f2da commit 3be3763
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 14 additions & 8 deletions tests/config/rekalogika-mapper/generated-mappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Rekalogika\Mapper\Tests\Fixtures\LazyObject;

class ObjectWithIdAndNameMustBeCalled
class ObjectWithIdAndName
{
private bool $idCalled = false;

Expand Down Expand Up @@ -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;
}
}
24 changes: 24 additions & 0 deletions tests/src/Fixtures/LazyObject/ObjectWithIdInConstructorDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* 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;
}
24 changes: 22 additions & 2 deletions tests/src/IntegrationTest/LazyObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit 3be3763

Please sign in to comment.