Skip to content

Commit

Permalink
test: add tests for mapping to objects with existing value (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Apr 30, 2024
1 parent d8ab6b1 commit 33a0e48
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
31 changes: 31 additions & 0 deletions tests/Fixtures/ObjectWithExistingValue/FurtherInnerObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\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;
}
}
49 changes: 49 additions & 0 deletions tests/Fixtures/ObjectWithExistingValue/InnerObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\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;
}
}
49 changes: 49 additions & 0 deletions tests/Fixtures/ObjectWithExistingValue/RootObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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\ObjectWithExistingValueDto;

final class FurtherInnerObjectDto
{
public ?string $property = null;
}
20 changes: 20 additions & 0 deletions tests/Fixtures/ObjectWithExistingValueDto/InnerObjectDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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\ObjectWithExistingValueDto;

final class InnerObjectDto
{
public ?string $property = null;
public ?FurtherInnerObjectDto $furtherInnerObject = null;
}
20 changes: 20 additions & 0 deletions tests/Fixtures/ObjectWithExistingValueDto/RootObjectDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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\ObjectWithExistingValueDto;

final class RootObjectDto
{
public ?string $id = null;
public ?InnerObjectDto $innerObject = null;
}
57 changes: 57 additions & 0 deletions tests/IntegrationTest/ObjectWithExistingValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\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());
}
}

0 comments on commit 33a0e48

Please sign in to comment.