diff --git a/CHANGELOG.md b/CHANGELOG.md index ae340d5..59aeab1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 1.2.1 + +* test: add tests for mapping to objects with existing value + ## 1.2.0 * feat: add `IterableMapperInterface` for mapping iterables diff --git a/tests/Fixtures/ObjectWithExistingValue/FurtherInnerObject.php b/tests/Fixtures/ObjectWithExistingValue/FurtherInnerObject.php new file mode 100644 index 0000000..c396420 --- /dev/null +++ b/tests/Fixtures/ObjectWithExistingValue/FurtherInnerObject.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValue; + +final class FurtherInnerObject +{ + private ?string $property = null; + + public function getProperty(): ?string + { + return $this->property; + } + + public function setProperty(?string $property): self + { + $this->property = $property; + + return $this; + } +} diff --git a/tests/Fixtures/ObjectWithExistingValue/InnerObject.php b/tests/Fixtures/ObjectWithExistingValue/InnerObject.php new file mode 100644 index 0000000..2365225 --- /dev/null +++ b/tests/Fixtures/ObjectWithExistingValue/InnerObject.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValue; + +final class InnerObject +{ + private ?string $property = null; + private FurtherInnerObject $furtherInnerObject; + + public function __construct() + { + $this->furtherInnerObject = new FurtherInnerObject(); + } + + public function getProperty(): ?string + { + return $this->property; + } + + public function setProperty(?string $property): self + { + $this->property = $property; + + return $this; + } + + public function getFurtherInnerObject(): FurtherInnerObject + { + return $this->furtherInnerObject; + } + + public function setFurtherInnerObject(FurtherInnerObject $furtherInnerObject): self + { + $this->furtherInnerObject = $furtherInnerObject; + + return $this; + } +} diff --git a/tests/Fixtures/ObjectWithExistingValue/RootObject.php b/tests/Fixtures/ObjectWithExistingValue/RootObject.php new file mode 100644 index 0000000..493638b --- /dev/null +++ b/tests/Fixtures/ObjectWithExistingValue/RootObject.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValue; + +final class RootObject +{ + private string $id = '_'; + private InnerObject $innerObject; + + public function __construct() + { + $this->innerObject = new InnerObject(); + } + + public function getInnerObject(): InnerObject + { + return $this->innerObject; + } + + public function setInnerObject(InnerObject $innerObject): self + { + $this->innerObject = $innerObject; + + return $this; + } + + public function getId(): string + { + return $this->id; + } + + public function setId(string $id): self + { + $this->id = $id; + + return $this; + } +} diff --git a/tests/Fixtures/ObjectWithExistingValueDto/FurtherInnerObjectDto.php b/tests/Fixtures/ObjectWithExistingValueDto/FurtherInnerObjectDto.php new file mode 100644 index 0000000..3274dbe --- /dev/null +++ b/tests/Fixtures/ObjectWithExistingValueDto/FurtherInnerObjectDto.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto; + +final class FurtherInnerObjectDto +{ + public ?string $property = null; +} diff --git a/tests/Fixtures/ObjectWithExistingValueDto/InnerObjectDto.php b/tests/Fixtures/ObjectWithExistingValueDto/InnerObjectDto.php new file mode 100644 index 0000000..019792f --- /dev/null +++ b/tests/Fixtures/ObjectWithExistingValueDto/InnerObjectDto.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto; + +final class InnerObjectDto +{ + public ?string $property = null; + public ?FurtherInnerObjectDto $furtherInnerObject = null; +} diff --git a/tests/Fixtures/ObjectWithExistingValueDto/RootObjectDto.php b/tests/Fixtures/ObjectWithExistingValueDto/RootObjectDto.php new file mode 100644 index 0000000..061d241 --- /dev/null +++ b/tests/Fixtures/ObjectWithExistingValueDto/RootObjectDto.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto; + +final class RootObjectDto +{ + public ?string $id = null; + public ?InnerObjectDto $innerObject = null; +} diff --git a/tests/IntegrationTest/ObjectWithExistingValueTest.php b/tests/IntegrationTest/ObjectWithExistingValueTest.php new file mode 100644 index 0000000..85f774a --- /dev/null +++ b/tests/IntegrationTest/ObjectWithExistingValueTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Tests\IntegrationTest; + +use Rekalogika\Mapper\Tests\Common\FrameworkTestCase; +use Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValue\RootObject; +use Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto\FurtherInnerObjectDto; +use Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto\InnerObjectDto; +use Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto\RootObjectDto; + +class ObjectWithExistingValueTest extends FrameworkTestCase +{ + public function testObjectWithExistingValueDtoToObject(): void + { + $dto = new RootObjectDto(); + $dto->id = 'id'; + $dto->innerObject = new InnerObjectDto(); + $dto->innerObject->property = 'foo'; + $dto->innerObject->furtherInnerObject = new FurtherInnerObjectDto(); + $dto->innerObject->furtherInnerObject->property = 'bar'; + + $object = $this->mapper->map($dto, RootObject::class); + + $this->assertSame('id', $object->getId()); + $this->assertSame('foo', $object->getInnerObject()->getProperty()); + $this->assertSame('bar', $object->getInnerObject()->getFurtherInnerObject()->getProperty()); + } + + public function testObjectWithExistingValueDtoToObjectPreinitialized(): void + { + $dto = new RootObjectDto(); + $dto->id = 'id'; + $dto->innerObject = new InnerObjectDto(); + $dto->innerObject->property = 'foo'; + $dto->innerObject->furtherInnerObject = new FurtherInnerObjectDto(); + $dto->innerObject->furtherInnerObject->property = 'bar'; + + $object = new RootObject(); + + $this->mapper->map($dto, $object); + + $this->assertSame('id', $object->getId()); + $this->assertSame('foo', $object->getInnerObject()->getProperty()); + $this->assertSame('bar', $object->getInnerObject()->getFurtherInnerObject()->getProperty()); + } +}