Failed Conditions
Pull Request — 2.6 (#7778)
by Luís
07:03
created

DefaultRegionTest::testGetters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Cache;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\Common\Cache\Cache;
7
use Doctrine\Common\Cache\CacheProvider;
8
use Doctrine\ORM\Cache\CollectionCacheEntry;
9
use Doctrine\ORM\Cache\Region\DefaultRegion;
10
use Doctrine\Tests\Mocks\CacheEntryMock;
11
use Doctrine\Tests\Mocks\CacheKeyMock;
12
13
/**
14
 * @group DDC-2183
15
 */
16
class DefaultRegionTest extends AbstractRegionTest
17
{
18
    protected function createRegion()
19
    {
20
        return new DefaultRegion('default.region.test', $this->cache);
21
    }
22
23
    public function testGetters()
24
    {
25
        $this->assertEquals('default.region.test', $this->region->getName());
26
        $this->assertSame($this->cache, $this->region->getCache());
0 ignored issues
show
Bug introduced by
The method getCache() does not exist on Doctrine\ORM\Cache\Region. It seems like you code against a sub-type of Doctrine\ORM\Cache\Region such as Doctrine\ORM\Cache\Region\DefaultRegion or Doctrine\ORM\Cache\Region\UpdateTimestampCache. ( Ignorable by Annotation )

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

26
        $this->assertSame($this->cache, $this->region->/** @scrutinizer ignore-call */ getCache());
Loading history...
27
    }
28
29
    public function testSharedRegion()
30
    {
31
        $cache   = new SharedArrayCache();
32
        $key     = new CacheKeyMock('key');
33
        $entry   = new CacheEntryMock(['value' => 'foo']);
34
        $region1 = new DefaultRegion('region1', $cache->createChild());
35
        $region2 = new DefaultRegion('region2', $cache->createChild());
36
37
        $this->assertFalse($region1->contains($key));
38
        $this->assertFalse($region2->contains($key));
39
40
        $region1->put($key, $entry);
41
        $region2->put($key, $entry);
42
43
        $this->assertTrue($region1->contains($key));
44
        $this->assertTrue($region2->contains($key));
45
46
        $region1->evictAll();
47
48
        $this->assertFalse($region1->contains($key));
49
        $this->assertTrue($region2->contains($key));
50
    }
51
52
    public function testDoesNotModifyCacheNamespace()
53
    {
54
        $cache = new ArrayCache();
55
56
        $cache->setNamespace('foo');
57
58
        new DefaultRegion('bar', $cache);
59
        new DefaultRegion('baz', $cache);
60
61
        $this->assertSame('foo', $cache->getNamespace());
62
    }
63
64
    public function testEvictAllWithGenericCacheThrowsUnsupportedException()
65
    {
66
        /* @var $cache \Doctrine\Common\Cache\Cache */
67
        $cache = $this->createMock(Cache::class);
68
69
        $region = new DefaultRegion('foo', $cache);
70
71
        $this->expectException(\BadMethodCallException::class);
72
73
        $region->evictAll();
74
    }
75
76
    public function testGetMulti()
77
    {
78
        $key1 = new CacheKeyMock('key.1');
79
        $value1 = new CacheEntryMock(['id' => 1, 'name' => 'bar']);
80
81
        $key2 = new CacheKeyMock('key.2');
82
        $value2 = new CacheEntryMock(['id' => 2, 'name' => 'bar']);
83
84
        $this->assertFalse($this->region->contains($key1));
85
        $this->assertFalse($this->region->contains($key2));
86
87
        $this->region->put($key1, $value1);
88
        $this->region->put($key2, $value2);
89
90
        $this->assertTrue($this->region->contains($key1));
91
        $this->assertTrue($this->region->contains($key2));
92
93
        $actual = $this->region->getMultiple(new CollectionCacheEntry([$key1, $key2]));
94
95
        $this->assertEquals($value1, $actual[0]);
96
        $this->assertEquals($value2, $actual[1]);
97
    }
98
99
    /**
100
     * @test
101
     * @group GH7266
102
     */
103
    public function corruptedDataDoesNotLeakIntoApplicationWhenGettingSingleEntry() : void
104
    {
105
        $key1 = new CacheKeyMock('key.1');
106
        $this->cache->save($this->region->getName() . '_' . $key1->hash, 'a-very-invalid-value');
107
108
        self::assertTrue($this->region->contains($key1));
109
        self::assertNull($this->region->get($key1));
110
    }
111
112
    /**
113
     * @test
114
     * @group GH7266
115
     */
116
    public function corruptedDataDoesNotLeakIntoApplicationWhenGettingMultipleEntries() : void
117
    {
118
        $key1 = new CacheKeyMock('key.1');
119
        $this->cache->save($this->region->getName() . '_' . $key1->hash, 'a-very-invalid-value');
120
121
        self::assertTrue($this->region->contains($key1));
122
        self::assertNull($this->region->getMultiple(new CollectionCacheEntry([$key1])));
123
    }
124
}
125
126
/**
127
 * Cache provider that offers child cache items (sharing the same array)
128
 *
129
 * Declared as a different class for readability purposes and kept in this file
130
 * to keep its monstrosity contained.
131
 *
132
 * @internal
133
 */
134
final class SharedArrayCache extends ArrayCache
135
{
136
    public function createChild(): Cache
137
    {
138
        return new class ($this) extends CacheProvider
139
        {
140
            /**
141
             * @var ArrayCache
142
             */
143
            private $parent;
144
145
            public function __construct(ArrayCache $parent)
146
            {
147
                $this->parent = $parent;
148
            }
149
150
            protected function doFetch($id)
151
            {
152
                return $this->parent->doFetch($id);
153
            }
154
155
            protected function doContains($id)
156
            {
157
                return $this->parent->doContains($id);
158
            }
159
160
            protected function doSave($id, $data, $lifeTime = 0)
161
            {
162
                return $this->parent->doSave($id, $data, $lifeTime);
163
            }
164
165
            protected function doDelete($id)
166
            {
167
                return $this->parent->doDelete($id);
168
            }
169
170
            protected function doFlush()
171
            {
172
                return $this->parent->doFlush();
173
            }
174
175
            protected function doGetStats()
176
            {
177
                return $this->parent->doGetStats();
178
            }
179
        };
180
    }
181
}
182