|
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\Mapping\Drivers; |
|
12
|
|
|
|
|
13
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\CachedMappingDriver; |
|
14
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
15
|
|
|
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface; |
|
16
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
|
17
|
|
|
use Psr\Cache\CacheItemInterface; |
|
18
|
|
|
use PHPUnit\Framework\TestCase; |
|
19
|
|
|
use Addiks\RDMBundle\Mapping\EntityMapping; |
|
20
|
|
|
use Addiks\RDMBundle\Tests\Hydration\ServiceExample; |
|
21
|
|
|
use Addiks\RDMBundle\Tests\ValueObjectExample; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
23
|
|
|
use Addiks\RDMBundle\Mapping\EntityMappingInterface; |
|
24
|
|
|
use Addiks\RDMBundle\Mapping\ServiceMapping; |
|
25
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContext; |
|
26
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
|
27
|
|
|
|
|
28
|
|
|
final class CachedMappingDriverTest extends TestCase |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var CachedMappingDriver |
|
33
|
|
|
*/ |
|
34
|
|
|
private $mappingDriver; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var MappingDriverInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $innerMappingDriver; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var CacheItemPoolInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $cacheItemPool; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var ContainerInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
private $container; |
|
50
|
|
|
|
|
51
|
|
|
public function setUp(): void |
|
52
|
|
|
{ |
|
53
|
|
|
$this->innerMappingDriver = $this->createMock(MappingDriverInterface::class); |
|
|
|
|
|
|
54
|
|
|
$this->cacheItemPool = $this->createMock(CacheItemPoolInterface::class); |
|
|
|
|
|
|
55
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$this->mappingDriver = new CachedMappingDriver( |
|
58
|
|
|
$this->innerMappingDriver, |
|
59
|
|
|
$this->container, |
|
60
|
|
|
$this->cacheItemPool, |
|
61
|
|
|
2 |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @test |
|
67
|
|
|
*/ |
|
68
|
|
|
public function shouldUseCachedMappingData() |
|
69
|
|
|
{ |
|
70
|
|
|
/** @var CacheItemInterface $cacheItem */ |
|
71
|
|
|
$cacheItem = $this->createMock(CacheItemInterface::class); |
|
72
|
|
|
|
|
73
|
|
|
$this->cacheItemPool->method('getItem')->will($this->returnValueMap([ |
|
|
|
|
|
|
74
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_Hydration_EntityExample', $cacheItem] |
|
75
|
|
|
])); |
|
76
|
|
|
|
|
77
|
|
|
$serviceMapping = new ServiceMapping( |
|
78
|
|
|
$this->createMock(ContainerInterface::class), |
|
79
|
|
|
"some_service" |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$expectedMapping = new EntityMapping(EntityExample::class, [ |
|
83
|
|
|
'foo' => $serviceMapping |
|
84
|
|
|
]); |
|
85
|
|
|
|
|
86
|
|
|
$cacheItem->method('isHit')->willReturn(true); |
|
|
|
|
|
|
87
|
|
|
$cacheItem->method('get')->willReturn(serialize($expectedMapping)); |
|
88
|
|
|
|
|
89
|
|
|
$this->container->expects($this->once())->method("has")->with($this->equalTo("some_service"))->willReturn(true); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** @var EntityMapping $actualMapping */ |
|
92
|
|
|
$actualMapping = $this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); |
|
93
|
|
|
|
|
94
|
|
|
$actualMapping->getFieldMappings()['foo']->resolveValue( |
|
95
|
|
|
$this->createMock(HydrationContextInterface::class), |
|
96
|
|
|
[] |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals($expectedMapping, $actualMapping); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @test |
|
104
|
|
|
*/ |
|
105
|
|
|
public function shouldCacheMappingData() |
|
106
|
|
|
{ |
|
107
|
|
|
/** @var CacheItemInterface $cacheItem */ |
|
108
|
|
|
$cacheItem = $this->createMock(CacheItemInterface::class); |
|
109
|
|
|
|
|
110
|
|
|
$this->cacheItemPool->method('getItem')->will($this->returnValueMap([ |
|
111
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_Hydration_EntityExample', $cacheItem] |
|
112
|
|
|
])); |
|
113
|
|
|
|
|
114
|
|
|
/** @var EntityMapping $expectedAnnotations */ |
|
115
|
|
|
$expectedAnnotations = new EntityMapping(EntityExample::class, []); |
|
116
|
|
|
|
|
117
|
|
|
$cacheItem->method('isHit')->willReturn(false); |
|
118
|
|
|
$cacheItem->expects($this->once())->method('set')->with(serialize($expectedAnnotations)); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$this->innerMappingDriver->method('loadRDMMetadataForClass')->will($this->returnValueMap([ |
|
|
|
|
|
|
121
|
|
|
[EntityExample::class, $expectedAnnotations] |
|
122
|
|
|
])); |
|
123
|
|
|
|
|
124
|
|
|
/** @var array<mixed> $actualAnnotations */ |
|
125
|
|
|
$actualAnnotations = $this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals($expectedAnnotations, $actualAnnotations); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @test |
|
132
|
|
|
*/ |
|
133
|
|
|
public function shouldHoldMappingsInInternalCache() |
|
134
|
|
|
{ |
|
135
|
|
|
/** @var CacheItemInterface $cacheItem */ |
|
136
|
|
|
$cacheItem = $this->createMock(CacheItemInterface::class); |
|
137
|
|
|
|
|
138
|
|
|
$this->cacheItemPool->expects($this->exactly(4))->method('getItem')->will($this->returnValueMap([ |
|
|
|
|
|
|
139
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_Hydration_EntityExample', $cacheItem], |
|
140
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_Hydration_ServiceExample', $cacheItem], |
|
141
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_ValueObjectExample', $cacheItem], |
|
142
|
|
|
])); |
|
143
|
|
|
|
|
144
|
|
|
# For the following remember that only the last two classes will be held in internal cache. |
|
145
|
|
|
# (as defined in the setUp method above) |
|
146
|
|
|
|
|
147
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); # FIRST FETCH |
|
148
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); # (cached) |
|
149
|
|
|
|
|
150
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ServiceExample::class); # SECOND FETCH |
|
151
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ServiceExample::class); # (cached) |
|
152
|
|
|
|
|
153
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ValueObjectExample::class); # THIRD FETCH (removes EntityExample) |
|
154
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ValueObjectExample::class); # (cached) |
|
155
|
|
|
|
|
156
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ServiceExample::class); # (cached) |
|
157
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ServiceExample::class); # (cached) |
|
158
|
|
|
|
|
159
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); # FOURTH FETCH |
|
160
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); # (cached) |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
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..