From a74b074d99a5483139ea9e52fd94fdaf9623cd03 Mon Sep 17 00:00:00 2001 From: Mark Gerarts Date: Thu, 27 Feb 2020 17:24:32 +0100 Subject: [PATCH] Fix SetTo not setting properties that are not present on the source object See #53 --- src/MappingOperation/Implementations/SetTo.php | 8 ++++++++ .../MappingOperation/Implementations/SetToTest.php | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/MappingOperation/Implementations/SetTo.php b/src/MappingOperation/Implementations/SetTo.php index 80aa54e..486f9e3 100644 --- a/src/MappingOperation/Implementations/SetTo.php +++ b/src/MappingOperation/Implementations/SetTo.php @@ -33,4 +33,12 @@ protected function getSourceValue($source, string $propertyName) { return $this->value; } + + /** + * @inheritdoc + */ + protected function canMapProperty(string $propertyName, $source): bool + { + return true; + } } diff --git a/test/MappingOperation/Implementations/SetToTest.php b/test/MappingOperation/Implementations/SetToTest.php index f5c4447..973f69c 100644 --- a/test/MappingOperation/Implementations/SetToTest.php +++ b/test/MappingOperation/Implementations/SetToTest.php @@ -4,6 +4,7 @@ use AutoMapperPlus\Configuration\Options; use AutoMapperPlus\Test\Models\SimpleProperties\Destination; +use AutoMapperPlus\Test\Models\SimpleProperties\DestinationAlt; use AutoMapperPlus\Test\Models\SimpleProperties\Source; use PHPUnit\Framework\TestCase; @@ -25,4 +26,17 @@ public function testItMapsAProperty() $this->assertEquals('always the same value', $destination->name); } + + public function testItDoesntNeedTheSourceProperty() + { + $operation = new SetTo('always the same value'); + $operation->setOptions(Options::default()); + $source = new Source(); + $destination = new DestinationAlt(); + + // anotherProperty is not present on the source object. + $operation->mapProperty('anotherProperty', $source, $destination); + + $this->assertEquals('always the same value', $destination->anotherProperty); + } }