Skip to content

Commit 9a26e60

Browse files
committed
Added tests for Flatten and Collection strategies.
Added ignore keys option to Flatten strategy. Added docblocks to Mapping.
1 parent ba6889f commit 9a26e60

4 files changed

Lines changed: 103 additions & 1 deletion

File tree

src/Mapper/Mapping.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
<?php
22
namespace ScriptFUSION\Mapper;
33

4+
/**
5+
* Represents a mapping of keys and mappable values.
6+
*/
47
abstract class Mapping extends \ArrayObject
58
{
9+
/**
10+
* Initializes this mapping.
11+
*/
612
public function __construct()
713
{
814
parent::__construct($this->createMap());
915
}
1016

17+
/**
18+
* Creates a mapping of key names and mappable values.
19+
*
20+
* @return array Mapping.
21+
*/
1122
abstract protected function createMap();
1223
}

src/Mapper/Strategy/Flatten.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33

44
class Flatten extends Delegate
55
{
6+
private $ignoreKeys = false;
7+
8+
public function ignoreKeys($ignore = true)
9+
{
10+
$this->ignoreKeys = $ignore;
11+
12+
return $this;
13+
}
14+
615
public function __invoke($data, $context = null)
716
{
8-
return iterator_to_array($this->flatten(parent::__invoke($data, $context)));
17+
return iterator_to_array($this->flatten(parent::__invoke($data, $context)), !$this->ignoreKeys);
918
}
1019

1120
private function flatten($data)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace ScriptFUSIONTest\Integration\Mapper\Strategy;
3+
4+
use ScriptFUSION\Mapper\Mapper;
5+
use ScriptFUSION\Mapper\Strategy\Collection;
6+
use ScriptFUSION\Mapper\Strategy\CopyContext;
7+
use ScriptFUSIONTest\MockFactory;
8+
9+
final class CollectionTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testNull()
12+
{
13+
/** @var Collection $collection */
14+
$collection = (new Collection(null, null))->setMapper(MockFactory::mockMapper(null));
15+
16+
self::assertNull($collection([]));
17+
}
18+
19+
public function testCollection()
20+
{
21+
/** @var Collection $collection */
22+
$collection = (new Collection(
23+
$this->createCollection('foo'),
24+
[
25+
'bar' => new CopyContext('foo'),
26+
]
27+
))->setMapper(new Mapper);
28+
29+
self::assertSame($this->createCollection('bar'), $collection([]));
30+
}
31+
32+
private function createCollection($key)
33+
{
34+
return array_map(
35+
function ($number) use ($key) {
36+
return [$key => $number];
37+
},
38+
range(1, 10)
39+
);
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace ScriptFUSIONTest\Unit\Mapper\Strategy;
3+
4+
use ScriptFUSION\Mapper\Strategy\Flatten;
5+
use ScriptFUSIONTest\MockFactory;
6+
7+
final class FlattenTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testValues()
10+
{
11+
/** @var Flatten $flatten */
12+
$flatten = (new Flatten(null))
13+
->ignoreKeys()
14+
->setMapper(MockFactory::mockMapper(['foo', ['bar', ['baz']]]))
15+
;
16+
17+
self::assertSame(['foo', 'bar', 'baz'], $flatten([]));
18+
}
19+
20+
public function testUniqueKeys()
21+
{
22+
/** @var Flatten $flatten */
23+
$flatten = (new Flatten(null))
24+
->setMapper(MockFactory::mockMapper(['foo' => 'foo', ['bar' => 'bar', ['baz' => 'baz']]]))
25+
;
26+
27+
self::assertSame(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], $flatten([]));
28+
}
29+
30+
public function testDuplicateKeys()
31+
{
32+
/** @var Flatten $flatten */
33+
$flatten = (new Flatten(null))
34+
->setMapper(MockFactory::mockMapper(['foo' => 'foo', ['bar' => 'bar', ['bar' => 'baz']]]))
35+
;
36+
self::assertSame(['foo' => 'foo', 'bar' => 'baz'], $flatten([]));
37+
38+
$flatten->setMapper(MockFactory::mockMapper(['foo' => 'foo', ['foo' => 'bar']]));
39+
self::assertSame(['foo' => 'bar'], $flatten([]));
40+
}
41+
}

0 commit comments

Comments
 (0)