Passed
Branch master (e8fd46)
by Alexey
03:15
created

CacheTest::canGetSetHasDelete()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
5
class CacheTest extends TestCase
6
{
7
    public function tearDown()
8
    {
9
        Mockery::close();
10
    }
11
12
    /**
13
     * @todo: test each scenario individually
14
     * @test
15
     */
16
    public function canGetSetHasDelete()
17
    {
18
        $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class);
19
        $cache = new \Venta\Cache\Cache($pool);
20
        $pool->shouldReceive('hasItem')->with('key')->andReturn(false, true, false)->times(3);
21
22
        $this->assertFalse($cache->has('key'));
23
24
        $pool->shouldReceive('save')->with(Mockery::type(\Psr\Cache\CacheItemInterface::class))->andReturn(true);
25
        $this->assertTrue($cache->set('key', 'value'));
26
27
        $this->assertTrue($cache->has('key'));
28
29
        $pool->shouldReceive('getItem')->with('key')->andReturn(
30
            Mockery::mock(\Psr\Cache\CacheItemInterface::class)
31
                   ->shouldReceive('get')
32
                   ->withNoArgs()
33
                   ->andReturn('value')
34
                   ->getMock()
35
        );
36
        $this->assertSame('value', $cache->get('key'));
37
38
        $pool->shouldReceive('deleteItem')->with('key')->andReturn(true);
39
        $this->assertTrue($cache->delete('key'));
40
41
        $this->assertFalse($cache->has('key'));
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function canPutWithDateTimeExpire()
48
    {
49
        $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class);
50
        $cache = new \Venta\Cache\Cache($pool);
51
52
        $pool->shouldReceive('save')->with(Mockery::on(function (\Cache\Adapter\Common\CacheItem $cacheItem) {
53
            $this->assertSame('key', $cacheItem->getKey());
54
            $this->assertEquals('2030-01-01 00:00:00', $cacheItem->getExpirationDate()->format('Y-m-d H:i:s'));
55
56
            return true;
57
        }))->andReturn(true);
58
59
        $this->assertTrue($cache->put('key', 'value', new DateTime('2030-01-01 00:00:00')));
60
    }
61
62
    /**
63
     * @test
64
     */
65
    public function canPutWithIntExpire()
66
    {
67
        $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class);
68
        $cache = new \Venta\Cache\Cache($pool);
69
70
        $pool->shouldReceive('save')->with(Mockery::on(function (\Cache\Adapter\Common\CacheItem $cacheItem) {
71
            $this->assertSame('key', $cacheItem->getKey());
72
            $this->assertEquals(time() + 10, $cacheItem->getExpirationDate()->getTimestamp());
73
74
            return true;
75
        }))->andReturn(true);
76
77
        $this->assertTrue($cache->put('key', 'value', 10));
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function canPutWithIntervalExpire()
84
    {
85
        $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class);
86
        $cache = new \Venta\Cache\Cache($pool);
87
88
        $interval = new DateInterval('P1M');
89
90
        $pool->shouldReceive('save')->with(Mockery::on(function (\Cache\Adapter\Common\CacheItem $cacheItem) use (
91
            $interval
92
        ) {
93
            $this->assertSame('key', $cacheItem->getKey());
94
            $this->assertEquals(
95
                (new \DateTime())->add($interval)->getTimestamp(),
96
                $cacheItem->getExpirationDate()->getTimestamp()
97
            );
98
99
            return true;
100
        }))->andReturn(true);
101
102
        $this->assertTrue($cache->put('key', 'value', $interval));
103
    }
104
105
    /**
106
     * @test
107
     */
108
    public function canPutWithoutExpire()
109
    {
110
        $pool = Mockery::mock(\Psr\Cache\CacheItemPoolInterface::class);
111
        $cache = new \Venta\Cache\Cache($pool);
112
113
        $pool->shouldReceive('save')->with(Mockery::on(function (\Cache\Adapter\Common\CacheItem $cacheItem) {
114
            $this->assertSame('key', $cacheItem->getKey());
115
            $this->assertNull($cacheItem->getExpirationDate());
116
117
            return true;
118
        }))->andReturn(true);
119
120
        $this->assertTrue($cache->put('key', 'value', 'invalid time'));
121
    }
122
123
}
124