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
|
|
|
|
23
|
|
|
final class CachedMappingDriverTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CachedMappingDriver |
28
|
|
|
*/ |
29
|
|
|
private $mappingDriver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var MappingDriverInterface |
33
|
|
|
*/ |
34
|
|
|
private $innerMappingDriver; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var CacheItemPoolInterface |
38
|
|
|
*/ |
39
|
|
|
private $cacheItemPool; |
40
|
|
|
|
41
|
|
|
public function setUp() |
42
|
|
|
{ |
43
|
|
|
$this->innerMappingDriver = $this->createMock(MappingDriverInterface::class); |
|
|
|
|
44
|
|
|
$this->cacheItemPool = $this->createMock(CacheItemPoolInterface::class); |
|
|
|
|
45
|
|
|
|
46
|
|
|
$this->mappingDriver = new CachedMappingDriver( |
47
|
|
|
$this->innerMappingDriver, |
|
|
|
|
48
|
|
|
$this->cacheItemPool, |
|
|
|
|
49
|
|
|
2 |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
|
public function shouldUseCachedMappingData() |
57
|
|
|
{ |
58
|
|
|
/** @var CacheItemInterface $cacheItem */ |
59
|
|
|
$cacheItem = $this->createMock(CacheItemInterface::class); |
60
|
|
|
|
61
|
|
|
$this->cacheItemPool->method('getItem')->will($this->returnValueMap([ |
|
|
|
|
62
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_Hydration_EntityExample', $cacheItem] |
63
|
|
|
])); |
64
|
|
|
|
65
|
|
|
/** @var EntityMapping $expectedAnnotations */ |
66
|
|
|
$expectedAnnotations = new EntityMapping(EntityExample::class, []); |
67
|
|
|
|
68
|
|
|
$cacheItem->method('isHit')->willReturn(true); |
|
|
|
|
69
|
|
|
$cacheItem->method('get')->willReturn(serialize($expectedAnnotations)); |
|
|
|
|
70
|
|
|
|
71
|
|
|
/** @var array<mixed> $actualAnnotations */ |
72
|
|
|
$actualAnnotations = $this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); |
73
|
|
|
|
74
|
|
|
$this->assertEquals($expectedAnnotations, $actualAnnotations); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @test |
79
|
|
|
*/ |
80
|
|
|
public function shouldCacheMappingData() |
81
|
|
|
{ |
82
|
|
|
/** @var CacheItemInterface $cacheItem */ |
83
|
|
|
$cacheItem = $this->createMock(CacheItemInterface::class); |
84
|
|
|
|
85
|
|
|
$this->cacheItemPool->method('getItem')->will($this->returnValueMap([ |
|
|
|
|
86
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_Hydration_EntityExample', $cacheItem] |
87
|
|
|
])); |
88
|
|
|
|
89
|
|
|
/** @var EntityMapping $expectedAnnotations */ |
90
|
|
|
$expectedAnnotations = new EntityMapping(EntityExample::class, []); |
91
|
|
|
|
92
|
|
|
$cacheItem->method('isHit')->willReturn(false); |
|
|
|
|
93
|
|
|
$cacheItem->expects($this->once())->method('set')->with(serialize($expectedAnnotations)); |
|
|
|
|
94
|
|
|
|
95
|
|
|
$this->innerMappingDriver->method('loadRDMMetadataForClass')->will($this->returnValueMap([ |
|
|
|
|
96
|
|
|
[EntityExample::class, $expectedAnnotations] |
97
|
|
|
])); |
98
|
|
|
|
99
|
|
|
/** @var array<mixed> $actualAnnotations */ |
100
|
|
|
$actualAnnotations = $this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); |
101
|
|
|
|
102
|
|
|
$this->assertEquals($expectedAnnotations, $actualAnnotations); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @test |
107
|
|
|
*/ |
108
|
|
|
public function shouldHoldMappingsInInternalCache() |
109
|
|
|
{ |
110
|
|
|
/** @var CacheItemInterface $cacheItem */ |
111
|
|
|
$cacheItem = $this->createMock(CacheItemInterface::class); |
112
|
|
|
|
113
|
|
|
$this->cacheItemPool->expects($this->exactly(4))->method('getItem')->will($this->returnValueMap([ |
|
|
|
|
114
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_Hydration_EntityExample', $cacheItem], |
115
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_Hydration_ServiceExample', $cacheItem], |
116
|
|
|
['addiks_rdm_mapping__Addiks_RDMBundle_Tests_ValueObjectExample', $cacheItem], |
117
|
|
|
])); |
118
|
|
|
|
119
|
|
|
# For the following remember that only the last two classes will be held in internal cache. |
120
|
|
|
# (as defined in the setUp method above) |
121
|
|
|
|
122
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); # FIRST FETCH |
123
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); # (cached) |
124
|
|
|
|
125
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ServiceExample::class); # SECOND FETCH |
126
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ServiceExample::class); # (cached) |
127
|
|
|
|
128
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ValueObjectExample::class); # THIRD FETCH (removes EntityExample) |
129
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ValueObjectExample::class); # (cached) |
130
|
|
|
|
131
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ServiceExample::class); # (cached) |
132
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(ServiceExample::class); # (cached) |
133
|
|
|
|
134
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); # FOURTH FETCH |
135
|
|
|
$this->mappingDriver->loadRDMMetadataForClass(EntityExample::class); # (cached) |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
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..