1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2017 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\RDMBundle\ValueResolver\ArrayValueResolver; |
15
|
|
|
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface; |
16
|
|
|
use Addiks\RDMBundle\Mapping\ArrayMappingInterface; |
17
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
18
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
19
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface; |
20
|
|
|
|
21
|
|
|
final class ArrayValueResolverTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ArrayValueResolver |
26
|
|
|
*/ |
27
|
|
|
private $arrayValueResolver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ValueResolverInterface |
31
|
|
|
*/ |
32
|
|
|
private $entryValueResolver; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->entryValueResolver = $this->createMock(ValueResolverInterface::class); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$this->arrayValueResolver = new ArrayValueResolver($this->entryValueResolver); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
*/ |
44
|
|
|
public function shouldResolveValue() |
45
|
|
|
{ |
46
|
|
|
/** @var ArrayMappingInterface $arrayMapping */ |
47
|
|
|
$arrayMapping = $this->createMock(ArrayMappingInterface::class); |
48
|
|
|
|
49
|
|
|
/** @var EntityExample $entity */ |
50
|
|
|
$entity = $this->createMock(EntityExample::class); |
51
|
|
|
|
52
|
|
|
/** @var MappingInterface $fooMapping */ |
53
|
|
|
$fooMapping = $this->createMock(MappingInterface::class); |
54
|
|
|
|
55
|
|
|
/** @var MappingInterface $bazMapping */ |
56
|
|
|
$bazMapping = $this->createMock(MappingInterface::class); |
57
|
|
|
|
58
|
|
|
/** @var mixed $dataFromAdditionalColumns */ |
59
|
|
|
$dataFromAdditionalColumns = array( |
60
|
|
|
'lorem' => 'ipsum', |
61
|
|
|
'dolor' => 'sit', |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
/** @var array<mixed> $expectedResult */ |
65
|
|
|
$expectedResult = [ |
66
|
|
|
'foo' => 'bar', |
67
|
|
|
'baz' => 3.1415 |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$arrayMapping->method('getEntryMappings')->willReturn([ |
|
|
|
|
71
|
|
|
'foo' => $fooMapping, |
72
|
|
|
'baz' => $bazMapping, |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$this->entryValueResolver->method('resolveValue')->will($this->returnValueMap([ |
|
|
|
|
76
|
|
|
[$fooMapping, $entity, $dataFromAdditionalColumns, 'bar'], |
77
|
|
|
[$bazMapping, $entity, $dataFromAdditionalColumns, 3.1415], |
78
|
|
|
])); |
79
|
|
|
|
80
|
|
|
/** @var mixed $actualResult */ |
81
|
|
|
$actualResult = $this->arrayValueResolver->resolveValue($arrayMapping, $entity, $dataFromAdditionalColumns); |
82
|
|
|
|
83
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
*/ |
89
|
|
|
public function shouldRevertValue() |
90
|
|
|
{ |
91
|
|
|
/** @var ArrayMappingInterface $arrayMapping */ |
92
|
|
|
$arrayMapping = $this->createMock(ArrayMappingInterface::class); |
93
|
|
|
|
94
|
|
|
/** @var EntityExample $entity */ |
95
|
|
|
$entity = $this->createMock(EntityExample::class); |
96
|
|
|
|
97
|
|
|
/** @var MappingInterface $fooMapping */ |
98
|
|
|
$fooMapping = $this->createMock(MappingInterface::class); |
99
|
|
|
|
100
|
|
|
/** @var MappingInterface $bazMapping */ |
101
|
|
|
$bazMapping = $this->createMock(MappingInterface::class); |
102
|
|
|
|
103
|
|
|
/** @var mixed $valueFromEntityField */ |
104
|
|
|
$valueFromEntityField = array( |
105
|
|
|
'foo' => 'bar', |
106
|
|
|
'baz' => 3.1415 |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
/** @var array<string, string> $expectedResult */ |
110
|
|
|
$expectedResult = array( |
111
|
|
|
'lorem' => 'ipsum', |
112
|
|
|
'dolor' => 'sit', |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$arrayMapping->method('getEntryMappings')->willReturn([ |
|
|
|
|
116
|
|
|
'foo' => $fooMapping, |
117
|
|
|
'baz' => $bazMapping, |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
$this->entryValueResolver->method('revertValue')->will($this->returnValueMap([ |
|
|
|
|
121
|
|
|
[$fooMapping, $entity, 'bar', ['lorem' => 'ipsum']], |
122
|
|
|
[$bazMapping, $entity, 3.1415, ['dolor' => 'sit']], |
123
|
|
|
])); |
124
|
|
|
|
125
|
|
|
/** @var mixed $actualResult */ |
126
|
|
|
$actualResult = $this->arrayValueResolver->revertValue($arrayMapping, $entity, $valueFromEntityField); |
127
|
|
|
|
128
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @test |
133
|
|
|
*/ |
134
|
|
|
public function shouldNotRevertForNonArray() |
135
|
|
|
{ |
136
|
|
|
/** @var ArrayMappingInterface $arrayMapping */ |
137
|
|
|
$arrayMapping = $this->createMock(ArrayMappingInterface::class); |
138
|
|
|
|
139
|
|
|
/** @var EntityExample $entity */ |
140
|
|
|
$entity = $this->createMock(EntityExample::class); |
141
|
|
|
|
142
|
|
|
/** @var MappingInterface $fooMapping */ |
143
|
|
|
$fooMapping = $this->createMock(MappingInterface::class); |
144
|
|
|
|
145
|
|
|
/** @var MappingInterface $bazMapping */ |
146
|
|
|
$bazMapping = $this->createMock(MappingInterface::class); |
147
|
|
|
|
148
|
|
|
$arrayMapping->method('getEntryMappings')->willReturn([ |
|
|
|
|
149
|
|
|
'foo' => $fooMapping, |
150
|
|
|
'baz' => $bazMapping, |
151
|
|
|
]); |
152
|
|
|
|
153
|
|
|
$this->entryValueResolver->method('revertValue')->will($this->returnValueMap([ |
|
|
|
|
154
|
|
|
[$fooMapping, $entity, 'bar', ['lorem' => 'ipsum']], |
155
|
|
|
[$bazMapping, $entity, 3.1415, ['dolor' => 'sit']], |
156
|
|
|
])); |
157
|
|
|
|
158
|
|
|
/** @var mixed $actualResult */ |
159
|
|
|
$actualResult = $this->arrayValueResolver->revertValue($arrayMapping, $entity, "a non-array"); |
160
|
|
|
|
161
|
|
|
$this->assertEquals([], $actualResult); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @test |
166
|
|
|
*/ |
167
|
|
|
public function shouldAssertValue() |
168
|
|
|
{ |
169
|
|
|
$this->expectException(FailedRDMAssertionExceptionInterface::class); |
170
|
|
|
|
171
|
|
|
/** @var ArrayMappingInterface $arrayMapping */ |
172
|
|
|
$arrayMapping = $this->createMock(ArrayMappingInterface::class); |
173
|
|
|
|
174
|
|
|
/** @var EntityExample $entity */ |
175
|
|
|
$entity = $this->createMock(EntityExample::class); |
176
|
|
|
|
177
|
|
|
$this->arrayValueResolver->assertValue($arrayMapping, $entity, [], "foo"); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..