Completed
Branch master (910c19)
by Dmitri
01:43
created

RedisStorageTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Tests\Key\Storage;
6
7
use Damax\Bundle\ApiAuthBundle\Key\Key;
8
use Damax\Bundle\ApiAuthBundle\Key\Storage\KeyNotFound;
9
use Damax\Bundle\ApiAuthBundle\Key\Storage\RedisStorage;
10
use PHPUnit\Framework\MockObject\MockObject;
11
use PHPUnit\Framework\TestCase;
12
use Predis\ClientInterface;
13
14
class RedisStorageTest extends TestCase
15
{
16
    /**
17
     * @var ClientInterface|MockObject
18
     */
19
    private $client;
20
21
    /**
22
     * @var RedisStorage
23
     */
24
    private $storage;
25
26
    protected function setUp()
27
    {
28
        $this->client = $this->createMock(ClientInterface::class);
29
        $this->storage = new RedisStorage($this->client, 'api_');
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function it_checks_key_in_storage()
36
    {
37
        $this->client
38
            ->expects($this->exactly(2))
39
            ->method('__call')
40
            ->withConsecutive(['exists', ['api_foo']], ['exists', ['api_bar']])
41
            ->willReturn(true, false)
42
        ;
43
44
        $this->assertTrue($this->storage->has('foo'));
45
        $this->assertFalse($this->storage->has('bar'));
46
    }
47
48
    /**
49
     * @test
50
     */
51
    public function it_removes_key_from_storage()
52
    {
53
        $this->client
54
            ->expects($this->exactly(2))
55
            ->method('__call')
56
            ->withConsecutive(
57
                ['del', [['api_foo']]],
58
                ['del', [['api_bar']]]
59
            )
60
        ;
61
62
        $this->storage->remove('foo');
63
        $this->storage->remove('bar');
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function it_adds_key_to_storage()
70
    {
71
        $this->client
72
            ->expects($this->once())
73
            ->method('__call')
74
            ->with('setex', ['api_XYZ', 60, 'john.doe'])
75
        ;
76
77
        $this->storage->add(new Key('XYZ', 'john.doe', 60));
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function it_retrieves_key_from_storage()
84
    {
85
        $this->client
86
            ->expects($this->exactly(2))
87
            ->method('__call')
88
            ->withConsecutive(
89
                ['get', ['api_XYZ']],
90
                ['ttl', ['api_XYZ']]
91
            )
92
            ->willReturnOnConsecutiveCalls('john.doe', 60)
93
        ;
94
95
        $key = $this->storage->get('XYZ');
96
97
        $this->assertEquals('XYZ', $key->key());
98
        $this->assertEquals('john.doe', $key->identity());
99
        $this->assertEquals(60, $key->ttl());
100
    }
101
102
    /**
103
     * @test
104
     */
105
    public function it_fails_retrieving_missing_key()
106
    {
107
        $this->client
108
            ->expects($this->once())
109
            ->method('__call')
110
            ->with('get', ['api_XYZ'])
111
        ;
112
113
        $this->expectException(KeyNotFound::class);
114
115
        $this->storage->get('XYZ');
116
    }
117
}
118