Skip to content

Commit 33a0e48

Browse files
authored
test: add tests for mapping to objects with existing value (#56)
1 parent d8ab6b1 commit 33a0e48

File tree

8 files changed

+249
-0
lines changed

8 files changed

+249
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 1.2.1
4+
5+
* test: add tests for mapping to objects with existing value
6+
37
## 1.2.0
48

59
* feat: add `IterableMapperInterface` for mapping iterables
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValue;
15+
16+
final class FurtherInnerObject
17+
{
18+
private ?string $property = null;
19+
20+
public function getProperty(): ?string
21+
{
22+
return $this->property;
23+
}
24+
25+
public function setProperty(?string $property): self
26+
{
27+
$this->property = $property;
28+
29+
return $this;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValue;
15+
16+
final class InnerObject
17+
{
18+
private ?string $property = null;
19+
private FurtherInnerObject $furtherInnerObject;
20+
21+
public function __construct()
22+
{
23+
$this->furtherInnerObject = new FurtherInnerObject();
24+
}
25+
26+
public function getProperty(): ?string
27+
{
28+
return $this->property;
29+
}
30+
31+
public function setProperty(?string $property): self
32+
{
33+
$this->property = $property;
34+
35+
return $this;
36+
}
37+
38+
public function getFurtherInnerObject(): FurtherInnerObject
39+
{
40+
return $this->furtherInnerObject;
41+
}
42+
43+
public function setFurtherInnerObject(FurtherInnerObject $furtherInnerObject): self
44+
{
45+
$this->furtherInnerObject = $furtherInnerObject;
46+
47+
return $this;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValue;
15+
16+
final class RootObject
17+
{
18+
private string $id = '_';
19+
private InnerObject $innerObject;
20+
21+
public function __construct()
22+
{
23+
$this->innerObject = new InnerObject();
24+
}
25+
26+
public function getInnerObject(): InnerObject
27+
{
28+
return $this->innerObject;
29+
}
30+
31+
public function setInnerObject(InnerObject $innerObject): self
32+
{
33+
$this->innerObject = $innerObject;
34+
35+
return $this;
36+
}
37+
38+
public function getId(): string
39+
{
40+
return $this->id;
41+
}
42+
43+
public function setId(string $id): self
44+
{
45+
$this->id = $id;
46+
47+
return $this;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto;
15+
16+
final class FurtherInnerObjectDto
17+
{
18+
public ?string $property = null;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto;
15+
16+
final class InnerObjectDto
17+
{
18+
public ?string $property = null;
19+
public ?FurtherInnerObjectDto $furtherInnerObject = null;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto;
15+
16+
final class RootObjectDto
17+
{
18+
public ?string $id = null;
19+
public ?InnerObjectDto $innerObject = null;
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Tests\IntegrationTest;
15+
16+
use Rekalogika\Mapper\Tests\Common\FrameworkTestCase;
17+
use Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValue\RootObject;
18+
use Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto\FurtherInnerObjectDto;
19+
use Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto\InnerObjectDto;
20+
use Rekalogika\Mapper\Tests\Fixtures\ObjectWithExistingValueDto\RootObjectDto;
21+
22+
class ObjectWithExistingValueTest extends FrameworkTestCase
23+
{
24+
public function testObjectWithExistingValueDtoToObject(): void
25+
{
26+
$dto = new RootObjectDto();
27+
$dto->id = 'id';
28+
$dto->innerObject = new InnerObjectDto();
29+
$dto->innerObject->property = 'foo';
30+
$dto->innerObject->furtherInnerObject = new FurtherInnerObjectDto();
31+
$dto->innerObject->furtherInnerObject->property = 'bar';
32+
33+
$object = $this->mapper->map($dto, RootObject::class);
34+
35+
$this->assertSame('id', $object->getId());
36+
$this->assertSame('foo', $object->getInnerObject()->getProperty());
37+
$this->assertSame('bar', $object->getInnerObject()->getFurtherInnerObject()->getProperty());
38+
}
39+
40+
public function testObjectWithExistingValueDtoToObjectPreinitialized(): void
41+
{
42+
$dto = new RootObjectDto();
43+
$dto->id = 'id';
44+
$dto->innerObject = new InnerObjectDto();
45+
$dto->innerObject->property = 'foo';
46+
$dto->innerObject->furtherInnerObject = new FurtherInnerObjectDto();
47+
$dto->innerObject->furtherInnerObject->property = 'bar';
48+
49+
$object = new RootObject();
50+
51+
$this->mapper->map($dto, $object);
52+
53+
$this->assertSame('id', $object->getId());
54+
$this->assertSame('foo', $object->getInnerObject()->getProperty());
55+
$this->assertSame('bar', $object->getInnerObject()->getFurtherInnerObject()->getProperty());
56+
}
57+
}

0 commit comments

Comments
 (0)