Skip to content

Commit

Permalink
Fixed key context propagation for delegate strategies.
Browse files Browse the repository at this point in the history
N.B. Key propagation is still broken for Copy* strategies until they implement KeyAware.
  • Loading branch information
Paul committed Jan 9, 2017
1 parent 202338a commit da266a0
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ Limitations
Testing
-------

Mapper is fully unit tested. Run the tests with `bin/test` from a shell. All examples
Mapper is fully unit tested. Run the tests with the `composer test` command. All examples
in this document can be found in `DocumentationTest`.


Expand Down
2 changes: 1 addition & 1 deletion src/Strategy/Copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Copy implements Strategy
/**
* Initializes this instance with the specified path.
*
* @param array|string $path Array of path components or string of `->`-delimited components.
* @param array|string $path Array of path components or string of `->`-delimited components.
*/
public function __construct($path)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Strategy/CopyContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class CopyContext extends Copy
/**
* {@inheritdoc}
*
* @param array|string $path Array of path components or string of `->`-delimited components.
* @param array|string $path Array of path components or string of `->`-delimited components.
*/
public function __construct($path = null)
{
parent::__construct($path);

$this->walk = !!$path;
$this->walk = (bool)$path;
}

public function __invoke($data, $context = null)
Expand Down
8 changes: 5 additions & 3 deletions src/Strategy/Delegate.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
namespace ScriptFUSION\Mapper\Strategy;

use ScriptFUSION\Mapper\KeyAware;
use ScriptFUSION\Mapper\KeyAwareTrait;
use ScriptFUSION\Mapper\MapperAware;
use ScriptFUSION\Mapper\MapperAwareTrait;
use ScriptFUSION\Mapper\Mapping;

abstract class Delegate implements Strategy, MapperAware
abstract class Delegate implements Strategy, MapperAware, KeyAware
{
use MapperAwareTrait;
use MapperAwareTrait, KeyAwareTrait;

private $expression;

Expand All @@ -26,6 +28,6 @@ public function __invoke($data, $context = null)

protected function delegate($strategy, $data, $context, $key = null)
{
return $this->mapper->map($data, $strategy, $context, $key);
return $this->mapper->map($data, $strategy, $context, $key !== null ? $key : $this->key);
}
}
2 changes: 1 addition & 1 deletion src/Strategy/Walk.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Walk extends Copy implements MapperAware

/**
* @param Strategy|Mapping|array|mixed $expression Expression.
* @param array|string $path Array of path components or string of `->`-delimited components.
* @param array|string $path Array of path components or string of `->`-delimited components.
*/
public function __construct($expression, $path)
{
Expand Down
52 changes: 52 additions & 0 deletions test/Functional/KeyPropagationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
namespace ScriptFUSIONTest\Functional;

use ScriptFUSION\Mapper\Mapper;
use ScriptFUSION\Mapper\Strategy\Collection;
use ScriptFUSION\Mapper\Strategy\Context;
use ScriptFUSION\Mapper\Strategy\Copy;
use ScriptFUSION\Mapper\Strategy\CopyContext;
use ScriptFUSION\Mapper\Strategy\CopyKey;

/**
* Tests that the key context is correctly propagated through different expression types.
*/
final class KeyPropagationTest extends \PHPUnit_Framework_TestCase
{
public function testFragmentPropagation()
{
$mapped = (new Mapper)->map(
[
'foo' => [
'bar' => [],
],
],
new Collection(
new Copy('foo'),
['foo' => new CopyKey]
)
);

self::assertSame(['bar' => ['foo' => 'bar']], $mapped);
}

public function testStrategyPropagation()
{
$mapped = (new Mapper)->map(
[
'foo' => [
'bar' => [],
],
],
new Collection(
new Copy('foo'),
new Context(
new CopyContext,
new CopyKey
)
)
);

self::assertSame(['bar' => 'bar'], $mapped);
}
}
38 changes: 0 additions & 38 deletions test/Integration/Mapper/Strategy/CopyKeyTest.php

This file was deleted.

6 changes: 6 additions & 0 deletions test/Unit/Mapper/Strategy/CopyKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

final class CopyKeyTest extends \PHPUnit_Framework_TestCase
{
public function testDefault()
{
$copyKey = new CopyKey;
self::assertNull($copyKey([]));
}

public function testRoundTrip()
{
$copyKey = new CopyKey;
Expand Down

0 comments on commit da266a0

Please sign in to comment.