Skip to content

Commit da266a0

Browse files
author
Paul
committed
Fixed key context propagation for delegate strategies.
N.B. Key propagation is still broken for Copy* strategies until they implement KeyAware.
1 parent 202338a commit da266a0

File tree

8 files changed

+68
-46
lines changed

8 files changed

+68
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ Limitations
812812
Testing
813813
-------
814814

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

818818

src/Strategy/Copy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Copy implements Strategy
1515
/**
1616
* Initializes this instance with the specified path.
1717
*
18-
* @param array|string $path Array of path components or string of `->`-delimited components.
18+
* @param array|string $path Array of path components or string of `->`-delimited components.
1919
*/
2020
public function __construct($path)
2121
{

src/Strategy/CopyContext.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class CopyContext extends Copy
1111
/**
1212
* {@inheritdoc}
1313
*
14-
* @param array|string $path Array of path components or string of `->`-delimited components.
14+
* @param array|string $path Array of path components or string of `->`-delimited components.
1515
*/
1616
public function __construct($path = null)
1717
{
1818
parent::__construct($path);
1919

20-
$this->walk = !!$path;
20+
$this->walk = (bool)$path;
2121
}
2222

2323
public function __invoke($data, $context = null)

src/Strategy/Delegate.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22
namespace ScriptFUSION\Mapper\Strategy;
33

4+
use ScriptFUSION\Mapper\KeyAware;
5+
use ScriptFUSION\Mapper\KeyAwareTrait;
46
use ScriptFUSION\Mapper\MapperAware;
57
use ScriptFUSION\Mapper\MapperAwareTrait;
68
use ScriptFUSION\Mapper\Mapping;
79

8-
abstract class Delegate implements Strategy, MapperAware
10+
abstract class Delegate implements Strategy, MapperAware, KeyAware
911
{
10-
use MapperAwareTrait;
12+
use MapperAwareTrait, KeyAwareTrait;
1113

1214
private $expression;
1315

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

2729
protected function delegate($strategy, $data, $context, $key = null)
2830
{
29-
return $this->mapper->map($data, $strategy, $context, $key);
31+
return $this->mapper->map($data, $strategy, $context, $key !== null ? $key : $this->key);
3032
}
3133
}

src/Strategy/Walk.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Walk extends Copy implements MapperAware
1616

1717
/**
1818
* @param Strategy|Mapping|array|mixed $expression Expression.
19-
* @param array|string $path Array of path components or string of `->`-delimited components.
19+
* @param array|string $path Array of path components or string of `->`-delimited components.
2020
*/
2121
public function __construct($expression, $path)
2222
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace ScriptFUSIONTest\Functional;
3+
4+
use ScriptFUSION\Mapper\Mapper;
5+
use ScriptFUSION\Mapper\Strategy\Collection;
6+
use ScriptFUSION\Mapper\Strategy\Context;
7+
use ScriptFUSION\Mapper\Strategy\Copy;
8+
use ScriptFUSION\Mapper\Strategy\CopyContext;
9+
use ScriptFUSION\Mapper\Strategy\CopyKey;
10+
11+
/**
12+
* Tests that the key context is correctly propagated through different expression types.
13+
*/
14+
final class KeyPropagationTest extends \PHPUnit_Framework_TestCase
15+
{
16+
public function testFragmentPropagation()
17+
{
18+
$mapped = (new Mapper)->map(
19+
[
20+
'foo' => [
21+
'bar' => [],
22+
],
23+
],
24+
new Collection(
25+
new Copy('foo'),
26+
['foo' => new CopyKey]
27+
)
28+
);
29+
30+
self::assertSame(['bar' => ['foo' => 'bar']], $mapped);
31+
}
32+
33+
public function testStrategyPropagation()
34+
{
35+
$mapped = (new Mapper)->map(
36+
[
37+
'foo' => [
38+
'bar' => [],
39+
],
40+
],
41+
new Collection(
42+
new Copy('foo'),
43+
new Context(
44+
new CopyContext,
45+
new CopyKey
46+
)
47+
)
48+
);
49+
50+
self::assertSame(['bar' => 'bar'], $mapped);
51+
}
52+
}

test/Integration/Mapper/Strategy/CopyKeyTest.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

test/Unit/Mapper/Strategy/CopyKeyTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
final class CopyKeyTest extends \PHPUnit_Framework_TestCase
77
{
8+
public function testDefault()
9+
{
10+
$copyKey = new CopyKey;
11+
self::assertNull($copyKey([]));
12+
}
13+
814
public function testRoundTrip()
915
{
1016
$copyKey = new CopyKey;

0 commit comments

Comments
 (0)