Skip to content

Commit

Permalink
#85 Pass context when calling mapTo in FromProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-gerarts committed Jan 31, 2023
1 parent 44ed317 commit 2c14575
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/MappingOperation/Implementations/FromProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use AutoMapperPlus\MappingOperation\MappingOperationInterface;
use AutoMapperPlus\MappingOperation\Reversible;
use AutoMapperPlus\NameResolver\CallbackNameResolver;
use AutoMapperPlus\MappingOperation\ContextAwareOperation;
use AutoMapperPlus\MappingOperation\ContextAwareTrait;

/**
* Class FromProperty
Expand All @@ -19,10 +21,13 @@
class FromProperty extends DefaultMappingOperation implements
AlternativePropertyProvider,
Reversible,
ContextAwareOperation,
// We need to be mapper aware to be able to pass the mapper to a chained
// operation.
MapperAwareOperation
{
use ContextAwareTrait;

/**
* @var MappingOperationInterface|null
*/
Expand Down Expand Up @@ -134,6 +139,10 @@ protected function mapPropertyWithNextOperation(
}));
$this->nextOperation->setOptions($options);

if ($this->nextOperation instanceof ContextAwareOperation) {
$this->nextOperation->setContext($this->context);
}

// The chained operation will now use the property name assigned to
// FromProperty, so we can go ahead and call it.
$this->nextOperation->mapProperty($propertyName, $source, $destination);
Expand Down
60 changes: 60 additions & 0 deletions test/MappingOperation/Implementations/FromPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
use PHPUnit\Framework\TestCase;
use AutoMapperPlus\Test\Models\SimpleProperties\Destination;
use AutoMapperPlus\Test\Models\Visibility\Visibility;
use AutoMapperPlus\Test\Models\Nested\Person;
use AutoMapperPlus\Test\Models\Nested\PersonDto;
use AutoMapperPlus\Test\Models\Nested\Address;
use AutoMapperPlus\AutoMapperInterface;
use AutoMapperPlus\Configuration\AutoMapperConfigInterface;
use AutoMapperPLus\Configuration\AutoMapperConfig;

/**
* Class FromPropertyTest
Expand All @@ -26,4 +32,58 @@ public function testItMapsAProperty(): void

$this->assertTrue($destination->name);
}

public function testItPassesContext(): void
{
// Create a dummy mapper that can be passed to the operation and can be
// used to check if context is set.
$dummyMapper = new class($this) implements AutoMapperInterface {

private FromPropertyTest $testCase;

public function __construct(FromPropertyTest $testCase)
{
$this->testCase = $testCase;
}

public function map($source, string $targetClass, array $context = [])
{
$this->testCase->assertArrayHasKey('some', $context);
}

public function mapToObject($source, $destination, array $context = [])
{
return null;
}

public function mapMultiple(
$sourceCollection,
string $targetClass,
array $context = []
): array {
return [];
}

public static function initialize(callable $configurator): AutoMapperInterface
{
return $this;
}

public function getConfiguration(): AutoMapperConfigInterface
{
return new AutoMapperConfig();
}
};

$operation = new FromProperty('address');
$operation->mapTo(Address::class);
$operation->setOptions(Options::default());
$operation->setMapper($dummyMapper);
$operation->setContext(['some' => 'context']);

$source = new PersonDto();
$destination = new Person();

$operation->mapProperty('name', $source, $destination);
}
}

0 comments on commit 2c14575

Please sign in to comment.