-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for mapping to objects with existing value (#56)
- Loading branch information
Showing
8 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
tests/Fixtures/ObjectWithExistingValue/FurtherInnerObject.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/Fixtures/ObjectWithExistingValueDto/FurtherInnerObjectDto.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
tests/Fixtures/ObjectWithExistingValueDto/InnerObjectDto.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
tests/Fixtures/ObjectWithExistingValueDto/RootObjectDto.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |