Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for mapping to objects with existing value #56

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
Loading