-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make MapFrom ignore the null property option
- Loading branch information
1 parent
6d1f30f
commit ac466cc
Showing
3 changed files
with
73 additions
and
29 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
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
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,58 @@ | ||
<?php | ||
|
||
namespace AutoMapperPlus\Test\Scearios; | ||
|
||
use AutoMapperPlus\AutoMapper; | ||
use AutoMapperPlus\Configuration\AutoMapperConfig; | ||
use AutoMapperPlus\MappingOperation\Operation; | ||
use AutoMapperPlus\Test\Models\SimpleProperties\Destination; | ||
use AutoMapperPlus\Test\Models\SimpleProperties\Source; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class IgnoreNullPropertiesTest extends TestCase | ||
{ | ||
public function testItMapsToNullIfSourceIsNull() | ||
{ | ||
$config = new AutoMapperConfig(); | ||
$config->registerMapping(Source::class, Destination::class); | ||
$mapper = new AutoMapper($config); | ||
|
||
$source = new Source(null); | ||
$destination = new Destination(); | ||
$destination->name = 'Hello, world'; | ||
$mapper->mapToObject($source, $destination); | ||
|
||
$this->assertNull($destination->name); | ||
} | ||
|
||
public function testItDoesntMapToNullIfOptionIsSet() | ||
{ | ||
$config = new AutoMapperConfig(); | ||
$config->getOptions()->ignoreNullProperties(); | ||
$config->registerMapping(Source::class, Destination::class); | ||
$mapper = new AutoMapper($config); | ||
|
||
$source = new Source(null); | ||
$destination = new Destination(); | ||
$destination->name = 'Hello, world'; | ||
$mapper->mapToObject($source, $destination); | ||
|
||
$this->assertEquals('Hello, world', $destination->name); | ||
} | ||
|
||
public function testMapFromIsntAffectedByIgnoreNullOption() | ||
{ | ||
$config = new AutoMapperConfig(); | ||
$config->getOptions()->ignoreNullProperties(); | ||
$config->registerMapping(Source::class, Destination::class) | ||
->forMember('name', Operation::mapFrom(function () { return null; })); | ||
$mapper = new AutoMapper($config); | ||
|
||
$source = new Source(null); | ||
$destination = new Destination(); | ||
$destination->name = 'Hello, world'; | ||
$mapper->mapToObject($source, $destination); | ||
|
||
$this->assertEquals(null, $destination->name); | ||
} | ||
} |