1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 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\ServiceValueResolver; |
15
|
|
|
use Addiks\RDMBundle\Tests\Hydration\ServiceExample; |
16
|
|
|
use Addiks\RDMBundle\Mapping\ServiceMappingInterface; |
17
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
19
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface; |
20
|
|
|
|
21
|
|
|
final class ServiceValueResolverTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ServiceValueResolver |
26
|
|
|
*/ |
27
|
|
|
private $valueResolver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ContainerInterface |
31
|
|
|
*/ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$this->valueResolver = new ServiceValueResolver($this->container); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @test |
43
|
|
|
*/ |
44
|
|
|
public function shouldResolveAValue() |
45
|
|
|
{ |
46
|
|
|
$expectedService = new ServiceExample("lorem", 123); |
47
|
|
|
|
48
|
|
|
$this->container->method('has')->will($this->returnValueMap([ |
|
|
|
|
49
|
|
|
['some_service', true], |
50
|
|
|
])); |
51
|
|
|
|
52
|
|
|
$this->container->method('get')->will($this->returnValueMap([ |
|
|
|
|
53
|
|
|
['some_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $expectedService], |
54
|
|
|
])); |
55
|
|
|
|
56
|
|
|
/** @var ServiceMappingInterface $fieldMapping */ |
57
|
|
|
$fieldMapping = $this->createMock(ServiceMappingInterface::class); |
58
|
|
|
|
59
|
|
|
$fieldMapping->method('getServiceId')->willReturn("some_service"); |
|
|
|
|
60
|
|
|
|
61
|
|
|
/** @var mixed $actualValue */ |
62
|
|
|
$actualService = $this->valueResolver->resolveValue( |
63
|
|
|
$fieldMapping, |
64
|
|
|
$this->createMock(EntityExample::class), |
65
|
|
|
[] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->assertSame($expectedService, $actualService); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
|
public function shouldRevertValue() |
75
|
|
|
{ |
76
|
|
|
/** @var ServiceMappingInterface $fieldMapping */ |
77
|
|
|
$fieldMapping = $this->createMock(ServiceMappingInterface::class); |
78
|
|
|
|
79
|
|
|
$this->assertEquals([], $this->valueResolver->revertValue( |
80
|
|
|
$fieldMapping, |
81
|
|
|
$this->createMock(EntityExample::class), |
82
|
|
|
null |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @test |
88
|
|
|
*/ |
89
|
|
|
public function shouldAssertThatCorrectServiceWasSet() |
90
|
|
|
{ |
91
|
|
|
/** @var ServiceMappingInterface $fieldMapping */ |
92
|
|
|
$fieldMapping = $this->createMock(ServiceMappingInterface::class); |
93
|
|
|
|
94
|
|
|
$fieldMapping->method('isLax')->willReturn(false); |
|
|
|
|
95
|
|
|
$fieldMapping->method('getServiceId')->willReturn("some_service"); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$entity = new EntityExample(); |
98
|
|
|
|
99
|
|
|
$service = new ServiceExample("lorem", 123); |
100
|
|
|
$otherService = new ServiceExample("ipsum", 456); |
101
|
|
|
|
102
|
|
|
$this->container->method('has')->will($this->returnValueMap([ |
|
|
|
|
103
|
|
|
['some_service', true], |
104
|
|
|
])); |
105
|
|
|
|
106
|
|
|
$this->container->method('get')->will($this->returnValueMap([ |
|
|
|
|
107
|
|
|
['some_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $service], |
108
|
|
|
])); |
109
|
|
|
|
110
|
|
|
$this->expectException(FailedRDMAssertionExceptionInterface::class); |
111
|
|
|
|
112
|
|
|
$this->valueResolver->assertValue($fieldMapping, $entity, [], $otherService); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @test |
117
|
|
|
*/ |
118
|
|
|
public function shouldNotAssertLaxFieldMappings() |
119
|
|
|
{ |
120
|
|
|
/** @var ServiceMappingInterface $fieldMapping */ |
121
|
|
|
$fieldMapping = $this->createMock(ServiceMappingInterface::class); |
122
|
|
|
|
123
|
|
|
$fieldMapping->method('isLax')->willReturn(true); |
|
|
|
|
124
|
|
|
|
125
|
|
|
$fieldMapping->expects($this->never())->method('getServiceId'); |
|
|
|
|
126
|
|
|
|
127
|
|
|
$entity = new EntityExample(); |
128
|
|
|
|
129
|
|
|
$service = new ServiceExample("lorem", 123); |
130
|
|
|
|
131
|
|
|
$this->valueResolver->assertValue($fieldMapping, $entity, [], $service); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
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..