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\Hydration; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\RDMBundle\Hydration\EntityHydrator; |
15
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface; |
16
|
|
|
use Addiks\RDMBundle\Mapping\Annotation\Service; |
17
|
|
|
use Addiks\RDMBundle\Tests\Hydration\ServiceExample; |
18
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
19
|
|
|
use ErrorException; |
20
|
|
|
use Addiks\RDMBundle\Mapping\ServiceMapping; |
21
|
|
|
use Addiks\RDMBundle\Mapping\EntityMapping; |
22
|
|
|
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface; |
23
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface; |
24
|
|
|
use Addiks\RDMBundle\DataLoader\DataLoaderInterface; |
25
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
26
|
|
|
use Addiks\RDMBundle\Mapping\EntityMappingInterface; |
27
|
|
|
use Addiks\RDMBundle\Mapping\MappingInterface; |
28
|
|
|
|
29
|
|
|
final class EntityHydratorTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var EntityHydrator |
34
|
|
|
*/ |
35
|
|
|
private $hydrator; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ValueResolverInterface |
39
|
|
|
*/ |
40
|
|
|
private $valueResolver; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var MappingDriverInterface |
44
|
|
|
*/ |
45
|
|
|
private $mappingDriver; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var DataLoaderInterface |
49
|
|
|
*/ |
50
|
|
|
private $dbalDataLoader; |
51
|
|
|
|
52
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$this->valueResolver = $this->createMock(ValueResolverInterface::class); |
|
|
|
|
55
|
|
|
$this->mappingDriver = $this->createMock(MappingDriverInterface::class); |
|
|
|
|
56
|
|
|
$this->dbalDataLoader = $this->createMock(DataLoaderInterface::class); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->hydrator = new EntityHydrator( |
59
|
|
|
$this->valueResolver, |
|
|
|
|
60
|
|
|
$this->mappingDriver, |
|
|
|
|
61
|
|
|
$this->dbalDataLoader |
|
|
|
|
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function shouldHydrateAnEntityWithServices() |
69
|
|
|
{ |
70
|
|
|
$fooMapping = new ServiceMapping("the_foo_service"); |
71
|
|
|
$barMapping = new ServiceMapping("another_bar_service"); |
72
|
|
|
$fazMapping = new ServiceMapping("a_private_property_service"); |
73
|
|
|
|
74
|
|
|
$this->mappingDriver->method("loadRDMMetadataForClass")->willReturn( |
|
|
|
|
75
|
|
|
new EntityMapping(EntityExample::class, [ |
76
|
|
|
'foo' => $fooMapping, |
77
|
|
|
'bar' => $barMapping, |
78
|
|
|
'faz' => $fazMapping |
79
|
|
|
]) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$serviceA = new ServiceExample("SomeService", 123); |
83
|
|
|
$serviceB = new ServiceExample("AnotherService", 456); |
84
|
|
|
$serviceC = new ServiceExample("PrivateService", 789); |
85
|
|
|
|
86
|
|
|
$entity = new EntityExample(); |
87
|
|
|
|
88
|
|
|
$this->valueResolver->method("resolveValue")->will($this->returnValueMap([ |
|
|
|
|
89
|
|
|
[$fooMapping, $entity, [], $serviceA], |
90
|
|
|
[$barMapping, $entity, [], $serviceB], |
91
|
|
|
[$fazMapping, $entity, [], $serviceC], |
92
|
|
|
])); |
93
|
|
|
|
94
|
|
|
$this->hydrator->hydrateEntity($entity, $this->createMock(EntityManagerInterface::class)); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->assertEquals($serviceA, $entity->foo); |
97
|
|
|
$this->assertEquals($serviceB, $entity->bar); |
98
|
|
|
$this->assertEquals($serviceC, $entity->getFaz()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @test |
103
|
|
|
*/ |
104
|
|
|
public function shouldAssertHydrationUsingValueResolvers() |
105
|
|
|
{ |
106
|
|
|
$fazMapping = new ServiceMapping("the_faz_service"); |
107
|
|
|
|
108
|
|
|
/** @var ServiceExample $fooService */ |
109
|
|
|
$fazService = $this->createMock(ServiceExample::class); |
110
|
|
|
|
111
|
|
|
$entity = new EntityExample(null, null, null, $fazService); |
|
|
|
|
112
|
|
|
|
113
|
|
|
$this->mappingDriver->method("loadRDMMetadataForClass")->willReturn( |
|
|
|
|
114
|
|
|
new EntityMapping(EntityExample::class, [ |
115
|
|
|
'faz' => $fazMapping, |
116
|
|
|
]) |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$this->valueResolver->expects($this->once())->method("assertValue")->with( |
|
|
|
|
120
|
|
|
$this->equalTo($fazMapping), |
121
|
|
|
$this->equalTo($entity), |
122
|
|
|
$this->equalTo([]), |
123
|
|
|
$this->equalTo($fazService) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$this->hydrator->assertHydrationOnEntity($entity, $this->createMock(EntityManagerInterface::class)); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.