Passed
Push — master ( 2cdd54...356755 )
by Gerrit
03:27
created

shouldHoldMappingsInInternalCache()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...DriverInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\RDMBundle\...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...
44
        $this->cacheItemPool = $this->createMock(CacheItemPoolInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Psr\C...emPoolInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<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...
45
46
        $this->mappingDriver = new CachedMappingDriver(
47
            $this->innerMappingDriver,
0 ignored issues
show
Documentation introduced by
$this->innerMappingDriver is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...MappingDriverInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
            $this->cacheItemPool,
0 ignored issues
show
Documentation introduced by
$this->cacheItemPool is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Cache\CacheItemPoolInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Psr\Cache\CacheItemPoolInterface>.

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

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...
69
        $cacheItem->method('get')->willReturn(serialize($expectedAnnotations));
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Psr\Cache\CacheItemInterface>.

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

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

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...
93
        $cacheItem->expects($this->once())->method('set')->with(serialize($expectedAnnotations));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Psr\Cache\CacheItemInterface>.

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...
94
95
        $this->innerMappingDriver->method('loadRDMMetadataForClass')->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...MappingDriverInterface>.

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

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