Passed
Push — master ( 064bed...d815ea )
by Gerrit
04:18
created

shouldUseCachedMappingData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 32
rs 9.7666
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...DriverInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\Mapping...\MappingDriverInterface of property $innerMappingDriver.

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..

Loading history...
54
        $this->cacheItemPool = $this->createMock(CacheItemPoolInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ca...emPoolInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Psr\Cache\CacheItemPoolInterface of property $cacheItemPool.

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..

Loading history...
55
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Symfon...tainerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\Depend...tion\ContainerInterface of property $container.

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..

Loading history...
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([
0 ignored issues
show
Bug introduced by
The method method() does not exist on Psr\Cache\CacheItemPoolInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        $this->cacheItemPool->/** @scrutinizer ignore-call */ 
74
                              method('getItem')->will($this->returnValueMap([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Psr\Cache\CacheItemInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
        $cacheItem->/** @scrutinizer ignore-call */ 
87
                    method('isHit')->willReturn(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
        $cacheItem->method('get')->willReturn(serialize($expectedMapping));
88
89
        $this->container->expects($this->once())->method("has")->with($this->equalTo("some_service"))->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Symfony\Component\Depend...tion\ContainerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        $this->container->/** @scrutinizer ignore-call */ 
90
                          expects($this->once())->method("has")->with($this->equalTo("some_service"))->willReturn(true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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));
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Cache\CacheItemInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

118
        $cacheItem->/** @scrutinizer ignore-call */ 
119
                    expects($this->once())->method('set')->with(serialize($expectedAnnotations));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
120
        $this->innerMappingDriver->method('loadRDMMetadataForClass')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not exist on Addiks\RDMBundle\Mapping...\MappingDriverInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
        $this->innerMappingDriver->/** @scrutinizer ignore-call */ 
121
                                   method('loadRDMMetadataForClass')->will($this->returnValueMap([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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([
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Cache\CacheItemPoolInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
        $this->cacheItemPool->/** @scrutinizer ignore-call */ 
139
                              expects($this->exactly(4))->method('getItem')->will($this->returnValueMap([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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