Completed
Push — master ( ef5aaf...988ea5 )
by Luís
03:52
created

testInvalidNamespaceVersionCacheEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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