testGetDocumentReturnsEmptyIfCacheBridgeThrowsException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Template;
6
7
use Opulence\Cache\ICacheBridge;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
11
class CacheManagerTest extends TestCase
12
{
13
    /** @var CacheManager - System Under Test */
14
    protected CacheManager $sut;
15
16
    /** @var ICacheBridge|MockObject */
17
    protected $cacheBridgeMock;
18
19
    public function setUp(): void
20
    {
21
        parent::setUp();
22
23
        $this->cacheBridgeMock = $this->createMock(ICacheBridge::class);
24
25
        $this->sut = new CacheManager($this->cacheBridgeMock);
26
    }
27
28
    public function testGetCacheDataReturnsNullIfCacheBridgeThrowsException(): void
29
    {
30
        $cacheId = 'foo';
31
32
        $this->cacheBridgeMock->expects($this->any())->method('get')->willThrowException(new \Exception());
33
34
        $actualResult = $this->sut->getCacheData($cacheId);
35
36
        $this->assertNull($actualResult);
37
    }
38
39
    /**
40
     * @return array[]
41
     */
42
    public function getCacheDataProvider(): array
43
    {
44
        return [
45
            'simple'                       => [null, null],
46
            'bool-payload'                 => [true, null],
47
            'int-payload'                  => [1, null],
48
            'float-payload'                => [1.1, null],
49
            'object-payload'               => [new \stdClass(), null],
50
            'array-payload'                => [[], null],
51
            'string-json-payload'          => ['""', null],
52
            'empty-array-json-payload'     => ['[]', new CacheData()],
53
            'empty-object-json-payload'    => ['{}', new CacheData()],
54
            'no-subtemplates-json-payload' => [
55
                '{"date":"2019-03-01 13:59:59","subTemplates":[]}',
56
                new CacheData(),
57
            ],
58
            'full-json-payload'            => [
59
                '{"date":"2019-03-01 13:59:59","subTemplates":{"A":"A","B":"B"}}',
60
                (new CacheData())->setSubTemplates(['A' => 'A', 'B' => 'B']),
61
            ],
62
        ];
63
    }
64
65
    /**
66
     * @dataProvider getCacheDataProvider
67
     *
68
     * @param mixed          $payload
69
     * @param CacheData|null $expectedResult
70
     */
71
    public function testGetCacheData($payload, ?CacheData $expectedResult): void
72
    {
73
        $cacheId = 'foo';
74
75
        $this->cacheBridgeMock->expects($this->any())->method('get')->willReturn($payload);
76
77
        $actualResult = $this->sut->getCacheData($cacheId);
78
79
        $this->assertEquals(gettype($expectedResult), gettype($actualResult));
80
81
        if (!($actualResult instanceof CacheData) || !($expectedResult instanceof CacheData)) {
82
            return;
83
        }
84
85
        $this->assertEquals($expectedResult->getSubTemplates(), $actualResult->getSubTemplates());
86
    }
87
88
    public function testStoreCacheDataCallsCacheBridge(): void
89
    {
90
        $cacheId = 'foo';
91
        $blocks  = ['A' => 'A', 'B' => 'B'];
92
93
        $cacheData = (new CacheData())->setSubTemplates($blocks);
94
95
        $payload = json_encode(
96
            [
97
                CacheData::PAYLOAD_KEY_DATE         => $cacheData->getDate(),
98
                CacheData::PAYLOAD_KEY_SUBTEMPLATES => $cacheData->getSubTemplates(),
99
            ]
100
        );
101
        $key     = sprintf(CacheManager::CACHE_KEY_TEMPLATES, $cacheId);
102
103
        $this->cacheBridgeMock->expects($this->once())->method('set')->with($key, $payload, PHP_INT_MAX);
104
        $this->cacheBridgeMock->expects($this->once())->method('has')->with($key);
105
106
        $this->sut->storeCacheData($cacheId, $blocks);
107
    }
108
109
    public function testGetDocumentCallsCacheBridge(): void
110
    {
111
        $cacheId        = 'foo';
112
        $expectedResult = 'bar';
113
114
        $key = sprintf(CacheManager::CACHE_KEY_DOCUMENT, $cacheId);
115
116
        $this->cacheBridgeMock->expects($this->once())->method('get')->with($key)->willReturn($expectedResult);
117
118
        $actualResult = $this->sut->getDocument($cacheId);
119
120
        $this->assertSame($expectedResult, $actualResult);
121
    }
122
123
    public function testGetDocumentReturnsEmptyIfCacheBridgeThrowsException(): void
124
    {
125
        $cacheId = 'foo';
126
127
        $this->cacheBridgeMock->expects($this->once())->method('get')->willThrowException(new \Exception());
128
129
        $actualResult = $this->sut->getDocument($cacheId);
130
131
        $this->assertSame('', $actualResult);
132
    }
133
134
    public function testStoreDocumentCallsCacheBridge(): void
135
    {
136
        $cacheId = 'foo';
137
138
        $payload = 'abc';
139
        $key     = sprintf(CacheManager::CACHE_KEY_DOCUMENT, $cacheId);
140
141
        $this->cacheBridgeMock->expects($this->once())->method('set')->with($key, $payload, PHP_INT_MAX);
142
        $this->cacheBridgeMock->expects($this->once())->method('has')->with($key);
143
144
        $this->sut->storeDocument($cacheId, $payload);
145
    }
146
147
    public function testFlushCallsCacheBridge(): void
148
    {
149
        $this->cacheBridgeMock->expects($this->once())->method('flush');
150
151
        $this->sut->flush();
152
    }
153
}
154