Passed
Pull Request — master (#251)
by Gabriel
10:21
created

CacheProviderTest::testSaveMultipleNoFail()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\Common\Cache;
4
5
use Doctrine\Common\Cache\CacheProvider;
6
use Doctrine\Tests\DoctrineTestCase;
7
8
class CacheProviderTest extends DoctrineTestCase
9
{
10
    public function testFetchMultiWillFilterNonRequestedKeys() : void
11
    {
12
        /* @var $cache \Doctrine\Common\Cache\CacheProvider|\PHPUnit_Framework_MockObject_MockObject */
13
        $cache = $this->getMockForAbstractClass(
14
            CacheProvider::class,
15
            [],
16
            '',
17
            true,
18
            true,
19
            true,
20
            ['doFetchMultiple']
21
        );
22
23
        $cache
24
            ->expects($this->once())
25
            ->method('doFetchMultiple')
26
            ->will($this->returnValue([
27
                '[foo][1]' => 'bar',
28
                '[bar][1]' => 'baz',
29
                '[baz][1]' => 'tab',
30
            ]));
31
32
        self::assertEquals(
33
            ['foo' => 'bar', 'bar' => 'baz'],
34
            $cache->fetchMultiple(['foo', 'bar'])
35
        );
36
    }
37
38
    public function testFailedDeleteAllDoesNotChangeNamespaceVersion() : void
39
    {
40
        /* @var $cache \Doctrine\Common\Cache\CacheProvider|\PHPUnit_Framework_MockObject_MockObject */
41
        $cache = $this->getMockForAbstractClass(
42
            CacheProvider::class,
43
            [],
44
            '',
45
            true,
46
            true,
47
            true,
48
            ['doFetch', 'doSave', 'doContains']
49
        );
50
51
        $cache
52
            ->expects($this->once())
53
            ->method('doFetch')
54
            ->with('DoctrineNamespaceCacheKey[]')
55
            ->will($this->returnValue(false));
56
57
        // doSave is only called once from deleteAll as we do not need to persist the default version in getNamespaceVersion()
58
        $cache
59
            ->expects($this->once())
60
            ->method('doSave')
61
            ->with('DoctrineNamespaceCacheKey[]')
62
            ->will($this->returnValue(false));
63
64
        // After a failed deleteAll() the local namespace version is not increased (still 1). Otherwise all data written afterwards
65
        // would be lost outside the current instance.
66
        $cache
67
            ->expects($this->once())
68
            ->method('doContains')
69
            ->with('[key][1]')
70
            ->will($this->returnValue(true));
71
72
        self::assertFalse($cache->deleteAll(), 'deleteAll() returns false when saving the namespace version fails');
73
        $cache->contains('key');
74
    }
75
76
    public function testSaveMultipleNoFail() : void
77
    {
78
        /* @var $cache \Doctrine\Common\Cache\CacheProvider|\PHPUnit_Framework_MockObject_MockObject */
79
        $cache = $this->getMockForAbstractClass(
80
            CacheProvider::class,
81
            [],
82
            '',
83
            true,
84
            true,
85
            true,
86
            ['doSave']
87
        );
88
89
        $cache
90
            ->expects($this->at(1))
91
            ->method('doSave')
92
            ->with('[kerr][1]', 'verr', 0)
93
            ->will($this->returnValue(false));
94
95
        $cache
96
            ->expects($this->at(2))
97
            ->method('doSave')
98
            ->with('[kok][1]', 'vok', 0)
99
            ->will($this->returnValue(true));
100
101
        $cache->saveMultiple([
102
            'kerr'  => 'verr',
103
            'kok'   => 'vok',
104
        ]);
105
    }
106
107
    public function testDeleteMultipleNoFail() : void
108
    {
109
        /* @var $cache \Doctrine\Common\Cache\CacheProvider|\PHPUnit_Framework_MockObject_MockObject */
110
        $cache = $this
111
            ->getMockBuilder(CacheProvider::class)
112
            ->setMethods(['doDelete'])
113
            ->getMockForAbstractClass();
114
115
        $cache
116
            ->expects($this->at(1))
117
            ->method('doDelete')
118
            ->with('[kerr][1]')
119
            ->will($this->returnValue(false));
120
121
        $cache
122
            ->expects($this->at(2))
123
            ->method('doDelete')
124
            ->with('[kok][1]')
125
            ->will($this->returnValue(true));
126
127
        $cache->deleteMultiple(['kerr', 'kok']);
128
    }
129
130
    public function testInvalidNamespaceVersionCacheEntry() : void
131
    {
132
        /* @var $cache \Doctrine\Common\Cache\CacheProvider|\PHPUnit_Framework_MockObject_MockObject */
133
        $cache = $this->getMockForAbstractClass(CacheProvider::class);
134
135
        $cache->expects($this->once())
136
              ->method('doFetch')
137
              ->with('DoctrineNamespaceCacheKey[]')
138
              ->willReturn('corruptedStringKey');
139
140
        $cache->expects($this->once())
141
              ->method('doSave')
142
              ->with('DoctrineNamespaceCacheKey[]', 2, 0)
143
              ->willReturn(true);
144
145
        self::assertTrue($cache->deleteAll());
146
    }
147
}
148