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\RDMBundle\Tests\ValueResolver; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\RDMBundle\ValueResolver\ObjectValueResolver; |
15
|
|
|
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
use Addiks\RDMBundle\Tests\ValueObjectExample; |
18
|
|
|
use Addiks\RDMBundle\Mapping\ObjectMappingInterface; |
19
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
20
|
|
|
use Addiks\RDMBundle\ValueResolver\CallDefinitionExecuterInterface; |
21
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface; |
22
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
23
|
|
|
|
24
|
|
|
final class ObjectValueResolverTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ObjectValueResolver |
29
|
|
|
*/ |
30
|
|
|
private $valueResolver; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ContainerInterface |
34
|
|
|
*/ |
35
|
|
|
private $container; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ValueResolverInterface |
39
|
|
|
*/ |
40
|
|
|
private $fieldValueResolver; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var CallDefinitionExecuterInterface |
44
|
|
|
*/ |
45
|
|
|
private $callDefinitionExecuter; |
46
|
|
|
|
47
|
|
|
public function setUp() |
48
|
|
|
{ |
49
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
50
|
|
|
$this->fieldValueResolver = $this->createMock(ValueResolverInterface::class); |
|
|
|
|
51
|
|
|
$this->callDefinitionExecuter = $this->createMock(CallDefinitionExecuterInterface::class); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$this->valueResolver = new ObjectValueResolver( |
54
|
|
|
$this->container, |
|
|
|
|
55
|
|
|
$this->fieldValueResolver, |
|
|
|
|
56
|
|
|
$this->callDefinitionExecuter |
|
|
|
|
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @test |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function shouldResolveValue() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
/** @var ObjectMappingInterface $objectMapping */ |
66
|
|
|
$objectMapping = $this->createMock(ObjectMappingInterface::class); |
67
|
|
|
|
68
|
|
|
/** @var MappingInterface $fieldMapping */ |
69
|
|
|
$fieldMapping = $this->createMock(MappingInterface::class); |
70
|
|
|
|
71
|
|
|
/** @var EntityExample $entity */ |
72
|
|
|
$entity = $this->createMock(EntityExample::class); |
73
|
|
|
|
74
|
|
|
/** @var mixed $dataFromAdditionalColumns */ |
75
|
|
|
$dataFromAdditionalColumns = array( |
76
|
|
|
'lorem' => 'ipsum', |
77
|
|
|
'dolor' => 'sit amet', |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$objectMapping->method('getClassName')->willReturn(ValueObjectExample::class); |
|
|
|
|
81
|
|
|
$objectMapping->method('getFieldMappings')->willReturn([ |
|
|
|
|
82
|
|
|
'amet' => $fieldMapping |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$this->fieldValueResolver->method('resolveValue')->will($this->returnValueMap([ |
|
|
|
|
86
|
|
|
[$fieldMapping, $entity, $dataFromAdditionalColumns, "FOO BAR BAZ"], |
87
|
|
|
])); |
88
|
|
|
|
89
|
|
|
/** @var mixed $actualObject */ |
90
|
|
|
$actualObject = $this->valueResolver->resolveValue($objectMapping, $entity, $dataFromAdditionalColumns); |
91
|
|
|
|
92
|
|
|
$this->assertTrue($actualObject instanceof ValueObjectExample); |
93
|
|
|
$this->assertEquals("FOO BAR BAZ", $actualObject->getAmet()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function shouldRevertValue() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
/** @var ObjectMappingInterface $objectMapping */ |
102
|
|
|
$objectMapping = $this->createMock(ObjectMappingInterface::class); |
103
|
|
|
|
104
|
|
|
/** @var MappingInterface $fieldMapping */ |
105
|
|
|
$fieldMapping = $this->createMock(MappingInterface::class); |
106
|
|
|
|
107
|
|
|
/** @var EntityExample $entity */ |
108
|
|
|
$entity = $this->createMock(EntityExample::class); |
109
|
|
|
|
110
|
|
|
$actualObject = new ValueObjectExample("foo"); |
111
|
|
|
$actualObject->setAmet('sit amet'); |
112
|
|
|
|
113
|
|
|
$objectMapping->method('getClassName')->willReturn(ValueObjectExample::class); |
|
|
|
|
114
|
|
|
$objectMapping->method('getFieldMappings')->willReturn([ |
|
|
|
|
115
|
|
|
'amet' => $fieldMapping |
116
|
|
|
]); |
117
|
|
|
|
118
|
|
|
$this->fieldValueResolver->method('revertValue')->will($this->returnValueMap([ |
|
|
|
|
119
|
|
|
[$fieldMapping, $entity, 'sit amet', ['amet' => "FOO BAR BAZ"]], |
120
|
|
|
])); |
121
|
|
|
|
122
|
|
|
/** @var array $actualData */ |
123
|
|
|
$actualData = $this->valueResolver->revertValue($objectMapping, $entity, $actualObject); |
124
|
|
|
|
125
|
|
|
$this->assertEquals(['amet' => "FOO BAR BAZ"], $actualData); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @test |
130
|
|
|
*/ |
131
|
|
|
public function shouldFailAssertionOnWrongObject() |
132
|
|
|
{ |
133
|
|
|
/** @var ObjectMappingInterface $objectMapping */ |
134
|
|
|
$objectMapping = $this->createMock(ObjectMappingInterface::class); |
135
|
|
|
|
136
|
|
|
/** @var EntityExample $entity */ |
137
|
|
|
$entity = $this->createMock(EntityExample::class); |
138
|
|
|
|
139
|
|
|
$dataFromAdditionalColumns = array( |
140
|
|
|
'lorem' => 'ipsum', |
141
|
|
|
'dolor' => 'sit amet', |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
/** @var mixed $actualValue */ |
145
|
|
|
$actualValue = $this->createMock(EntityExample::class); |
146
|
|
|
|
147
|
|
|
$objectMapping->method('getClassName')->willReturn(ValueObjectExample::class); |
|
|
|
|
148
|
|
|
|
149
|
|
|
$this->expectException(FailedRDMAssertionExceptionInterface::class); |
150
|
|
|
|
151
|
|
|
$this->valueResolver->assertValue($objectMapping, $entity, $dataFromAdditionalColumns, $actualValue); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
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..