/
ImplicitPassNotMatchedOptionsMapperTest.php
118 lines (111 loc) · 3.93 KB
/
ImplicitPassNotMatchedOptionsMapperTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Apantle\HashMapper\Test;
use PHPUnit\Framework\TestCase;
use Apantle\HashMapper\HashmapMapper as HM;
class ImplicitPassNotMatchedOptionsMapperTest extends TestCase
{
/**
* @dataProvider provideExamplesExplicitAndImplicitPass
*/
public function testNotMatchedOptionsSimplyPassToTargetHashmap($source, $expected, $passNotMatchedKeys)
{
$mapper = new HM([
'matched' => ['matchedReverse', function ($array) {
return array_reverse($array);
}],
'matchedMap' => ['matchedToLowercase', function ($array) {
return array_map(function ($letter) {
return strtolower($letter);
}, $array);
}],
], ['passNotMatchedKeys' => $passNotMatchedKeys]);
$this->assertEquals($expected, $mapper->apply($source));
}
public function provideExamplesExplicitAndImplicitPass()
{
$date = new \DateTimeImmutable();
return [
[
[
'matched' => [1, 2, 3],
'matchedMap' => ['A', 'B', 'C'],
'notMatched' => 'passes simply',
'date' => $date,
],
[
'matchedReverse' => [3, 2, 1],
'matchedToLowercase' => ['a', 'b', 'c'],
'notMatched' => 'passes simply',
'date' => $date,
],
true,
],
[
[
'matched' => [1, 2, 3],
'matchedMap' => ['A', 'B', 'C'],
'notMatched' => 'passes simply',
'date' => $date,
],
[
'matchedReverse' => [3, 2, 1],
'matchedToLowercase' => ['a', 'b', 'c'],
],
false,
],
];
}
public function testGetMapperForCollectionOfSameShapedHashmaps()
{
$mapper = new HM([
'date' => ['date_string', function ($date) {
return $date->format(DATE_ISO8601);
}],
'title' => ['title', function ($title) {
return $title['rendered'];
}],
'link' => ['url', function ($slug) {
return "http://example.com/{$slug}/";
}],
]);
$source = [
'1' => [
'date' => new \DateTime('2000-03-06'),
'title' => ['rendered' => 'PFP attacks UNAM'],
'link' => 'pfp-attacks-unam',
],
'2' => [
'date' => new \DateTime('2006-05-04'),
'title' => ['rendered' => 'PFP attacks Atenco'],
'link' => 'pfp-attacks-atenco',
],
'3' => [
'date' => new \DateTime('2015-09-27'),
'title' => ['rendered' => 'Iguala mass kidnapping'],
'link' => 'iguala-mass-kidnapping',
],
];
$expected = [
'1' => [
'date_string' => '2000-03-06T00:00:00+0000',
'title' => 'PFP attacks UNAM',
'url' => 'http://example.com/pfp-attacks-unam/',
],
'2' => [
'date_string' => '2006-05-04T00:00:00+0000',
'title' => 'PFP attacks Atenco',
'url' => 'http://example.com/pfp-attacks-atenco/',
],
'3' => [
'date_string' => '2015-09-27T00:00:00+0000',
'title' => 'Iguala mass kidnapping',
'url' => 'http://example.com/iguala-mass-kidnapping/',
],
];
foreach ($source as $key => $item) {
$this->assertEquals($expected[$key], $mapper->apply($item));
}
$collectionMapper = $mapper->getCollectionMapper();
$this->assertEquals($expected, $collectionMapper->apply($source));
}
}