Completed
Pull Request — master (#6481)
by Luís
17:57 queued 05:46
created

DefaultRegionTest::testSharedRegion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 15
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\ORM\Cache;
4
5
use Doctrine\Common\Cache\ApcCache;
6
use Doctrine\Common\Cache\ApcuCache;
7
use Doctrine\Common\Cache\ArrayCache;
8
use Doctrine\Common\Cache\Cache;
9
use Doctrine\ORM\Cache\CollectionCacheEntry;
10
use Doctrine\ORM\Cache\Region\DefaultRegion;
11
use Doctrine\Tests\Mocks\CacheEntryMock;
12
use Doctrine\Tests\Mocks\CacheKeyMock;
13
14
/**
15
 * @group DDC-2183
16
 */
17
class DefaultRegionTest extends AbstractRegionTest
18
{
19
    protected function createRegion()
20
    {
21
        return new DefaultRegion('default.region.test', $this->cache);
22
    }
23
24
    public function testGetters()
25
    {
26
        $this->assertEquals('default.region.test', $this->region->getName());
27
        $this->assertSame($this->cache, $this->region->getCache());
28
    }
29
30
    public function testSharedRegion()
31
    {
32
        $regionCache = $this->createRegionCache();
33
34
        $key     = new CacheKeyMock('key');
35
        $entry   = new CacheEntryMock(['value' => 'foo']);
36
        $region1 = new DefaultRegion('region1', $regionCache);
37
        $region2 = new DefaultRegion('region2', clone $regionCache);
38
39
        $this->assertFalse($region1->contains($key));
40
        $this->assertFalse($region2->contains($key));
41
42
        $region1->put($key, $entry);
43
        $region2->put($key, $entry);
44
45
        $this->assertTrue($region1->contains($key));
46
        $this->assertTrue($region2->contains($key));
47
48
        $region1->evictAll();
49
50
        $this->assertFalse($region1->contains($key));
51
        $this->assertTrue($region2->contains($key));
52
    }
53
54
    /**
55
     * @return Cache|null
56
     */
57
    private function createRegionCache()
58
    {
59
        if (extension_loaded('apc') && false !== @apc_cache_info()) {
60
            return new ApcCache();
61
        }
62
63
        if ( ! extension_loaded('apcu')) {
64
            $this->markTestSkipped('The ' . __CLASS__ .' requires the use of APC');
65
            return null;
66
        }
67
68
        return new ApcuCache();
69
    }
70
71
    public function testDoesNotModifyCacheNamespace()
72
    {
73
        $cache = new ArrayCache();
74
75
        $cache->setNamespace('foo');
76
77
        new DefaultRegion('bar', $cache);
78
        new DefaultRegion('baz', $cache);
79
80
        $this->assertSame('foo', $cache->getNamespace());
81
    }
82
83
    public function testEvictAllWithGenericCacheThrowsUnsupportedException()
84
    {
85
        /* @var $cache \Doctrine\Common\Cache\Cache */
86
        $cache = $this->createMock(Cache::class);
87
88
        $region = new DefaultRegion('foo', $cache);
89
90
        $this->expectException(\BadMethodCallException::class);
91
92
        $region->evictAll();
93
    }
94
95
    public function testGetMulti()
96
    {
97
        $key1 = new CacheKeyMock('key.1');
98
        $value1 = new CacheEntryMock(['id' => 1, 'name' => 'bar']);
99
100
        $key2 = new CacheKeyMock('key.2');
101
        $value2 = new CacheEntryMock(['id' => 2, 'name' => 'bar']);
102
103
        $this->assertFalse($this->region->contains($key1));
104
        $this->assertFalse($this->region->contains($key2));
105
106
        $this->region->put($key1, $value1);
107
        $this->region->put($key2, $value2);
108
109
        $this->assertTrue($this->region->contains($key1));
110
        $this->assertTrue($this->region->contains($key2));
111
112
        $actual = $this->region->getMultiple(new CollectionCacheEntry([$key1, $key2]));
113
114
        $this->assertEquals($value1, $actual[0]);
115
        $this->assertEquals($value2, $actual[1]);
116
    }
117
}
118