Skip to content

Commit

Permalink
Make MapFrom ignore the null property option
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-gerarts committed Nov 10, 2018
1 parent 6d1f30f commit ac466cc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 29 deletions.
15 changes: 15 additions & 0 deletions src/MappingOperation/Implementations/MapFrom.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,19 @@ protected function canMapProperty(string $propertyName, $source): bool
// properties.
return true;
}

/**
* @inheritdoc
*/
protected function setDestinationValue(
$destination,
string $propertyName,
$value
): void {
$this->getPropertyWriter()->setProperty(
$destination,
$propertyName,
$value
);
}
}
29 changes: 0 additions & 29 deletions test/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,33 +639,4 @@ public function testitMapsPrivatePropertiesToStdClass()
$this->assertEquals($result->username, 'AzureDiamond');
$this->assertEquals($result->password, 'hunter2');
}

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);
}
}
58 changes: 58 additions & 0 deletions test/Scenarios/IgnoreNullPropertiesTest.php
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);
}
}

0 comments on commit ac466cc

Please sign in to comment.