Test Failed
Pull Request — master (#2)
by Joao
02:20
created

CachePSR6Test   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 156
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B testGetOneItem() 0 30 2
B testGetMultipleItems() 0 38 2
B testTtl() 0 35 2
B testCacheObject() 0 31 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 7.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Test;
4
5
use ByJG\Cache\Psr6\CachePool;
6
7
require_once 'BaseCacheTest.php';
8
9
class CachePSR6Test extends BaseCacheTest
10
{
11
    /**
12
     * @dataProvider CachePoolProvider
13
     * @param \ByJG\Cache\Psr16\BaseCacheEngine $cacheEngine
14
     */
15
    public function testGetOneItem(\ByJG\Cache\Psr16\BaseCacheEngine $cacheEngine)
16
    {
17
        $this->cacheEngine = $cacheEngine;
18
19
        $object = new CachePool($cacheEngine);
20
        if ($object->isAvailable()) {
21
            // First time
22
            $item = $object->getItem('chave');
23
            $this->assertFalse($item->isHit());
24
25
            // Set object
26
            $item->set('valor');
27
            $object->save($item);
28
            $this->assertTrue($item->isHit());
29
30
            // Get Object
31
            $item2 = $object->getItem('chave');
32
            $this->assertTrue($item2->isHit());
33
            $this->assertEquals('valor', $item2->get());
34
35
            // Remove
36
            $object->deleteItem('chave');
37
38
            // Check Removed
39
            $item = $object->getItem('chave');
40
            $this->assertFalse($item->isHit());
41
        } else {
42
            $this->markTestIncomplete('Object is not fully functional');
43
        }
44
    }
45
46
    /**
47
     * @dataProvider CachePoolProvider
48
     * @param \ByJG\Cache\Psr16\BaseCacheEngine $cacheEngine
49
     */
50
    public function testGetMultipleItems(\ByJG\Cache\Psr16\BaseCacheEngine $cacheEngine)
51
    {
52
        $this->cacheEngine = $cacheEngine;
53
54
        $object = new CachePool($cacheEngine);
55
        if ($object->isAvailable()) {
56
            // First time
57
            $items = $object->getItems(['chave1', 'chave2']);
58
            $this->assertFalse($items[0]->isHit());
59
            $this->assertFalse($items[1]->isHit());
60
61
            // Set object
62
            $items[0]->set('valor1');
63
            $items[1]->set('valor2');
64
            $object->saveDeferred($items[0]);
65
            $object->saveDeferred($items[1]);
66
            $object->commit();
67
            $this->assertTrue($items[0]->isHit());
68
            $this->assertTrue($items[1]->isHit());
69
70
            // Get Object
71
            $item2 = $object->getItems(['chave1', 'chave2']);
72
            $this->assertTrue($item2[0]->isHit());
73
            $this->assertTrue($item2[1]->isHit());
74
            $this->assertEquals('valor1', $item2[0]->get());
75
            $this->assertEquals('valor2', $item2[1]->get());
76
77
            // Remove
78
            $object->deleteItems(['chave1', 'chave2']);
79
80
            // Check Removed
81
            $items = $object->getItems(['chave1', 'chave2']);
82
            $this->assertFalse($items[0]->isHit());
83
            $this->assertFalse($items[1]->isHit());
84
        } else {
85
            $this->markTestIncomplete('Object is not fully functional');
86
        }
87
    }
88
89
    /**
90
     * @dataProvider CachePoolProvider
91
     * @param \ByJG\Cache\Psr16\BaseCacheEngine $cacheEngine
92
     */
93
    public function testTtl(\ByJG\Cache\Psr16\BaseCacheEngine $cacheEngine)
94
    {
95
        $this->cacheEngine = $cacheEngine;
96
97
        $object = new CachePool($cacheEngine);
98
        if ($object->isAvailable()) {
99
            // First time
100
            $item = $object->getItem('chave');
101
            $this->assertFalse($item->isHit());
102
103
            // Set object
104
            $item->set('valor');
105
            $item->expiresAfter(2);
106
            $object->save($item);
107
            $this->assertTrue($item->isHit());
108
109
            // Get Object
110
            $item2 = $object->getItem('chave');
111
            $this->assertTrue($item2->isHit());
112
            $this->assertEquals('valor', $item2->get());
113
            sleep(3);
114
            $item3 = $object->getItem('chave');
115
            $this->assertFalse($item3->isHit());
116
            $this->assertEquals(null, $item3->get());
117
118
            // Remove
119
            $object->deleteItem('chave');
120
121
            // Check Removed
122
            $item = $object->getItem('chave');
123
            $this->assertFalse($item->isHit());
124
        } else {
125
            $this->markTestIncomplete('Object is not fully functional');
126
        }
127
    }
128
129
    /**
130
     * @dataProvider CachePoolProvider
131
     * @param \ByJG\Cache\Psr16\BaseCacheEngine $cacheEngine
132
     */
133
    public function testCacheObject(\ByJG\Cache\Psr16\BaseCacheEngine $cacheEngine)
134
    {
135
        $this->cacheEngine = $cacheEngine;
136
137
        $object = new CachePool($cacheEngine);
138
        if ($object->isAvailable()) {
139
            // First time
140
            $item = $object->getItem('chave');
141
            $this->assertFalse($item->isHit());
142
143
            // Set object
144
            $model = new Model(10, 20);
145
            $item->set($model);
146
            $object->save($item);
147
            $this->assertTrue($item->isHit());
148
149
            // Get Object
150
            $item2 = $object->getItem('chave');
151
            $this->assertTrue($item2->isHit());
152
            $this->assertEquals($model, $item2->get());
153
154
            // Remove
155
            $object->deleteItem('chave');
156
157
            // Check Removed
158
            $item = $object->getItem('chave');
159
            $this->assertFalse($item->isHit());
160
        } else {
161
            $this->markTestIncomplete('Object is not fully functional');
162
        }
163
    }
164
}
165