Passed
Push — master ( a635c7...cccea0 )
by Sergey
25:40 queued 07:31
created

StreamTest::testReadStream()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 9
c 1
b 1
f 1
nc 1
nop 0
dl 0
loc 22
rs 9.9666
1
<?php
2
3
4
namespace Asiries335\redisSteamPhp\Tests;
5
6
use Asiries335\redisSteamPhp\Data\Collection;
7
use Asiries335\redisSteamPhp\Stream;
8
use PHPUnit\Framework\TestCase;
9
10
class StreamTest extends TestCase
11
{
12
    private $client;
13
14
    private const TEST_NAME_STREAM = 'test_stream';
15
16
    /**
17
     * setUp
18
     *
19
     * @return void
20
     */
21
    public function setUp() : void
22
    {
23
        $this->client = \Mockery::mock(\Redis::class);
24
    }
25
26
    /**
27
     * Read empty stream
28
     *
29
     * @throws \Exception
30
     *
31
     * @return void
32
     */
33
    public function testReadEmptyStream() : void
34
    {
35
        $this->client->shouldReceive('rawCommand')->andReturn([]);
36
37
        $stream = new Stream($this->client, self::TEST_NAME_STREAM);
38
39
        $collection = $stream->get();
40
41
        $this->assertEquals(new Collection, $collection);
42
    }
43
44
    /**
45
     * Add data to stream
46
     *
47
     * @throws \Exception
48
     *
49
     * @return void
50
     */
51
    public function testAddDataToStream() : void
52
    {
53
        $key = 'name';
54
55
        $values = [
56
            'id'   => 123,
57
            'name' => 'Barney',
58
            'age'  => 25,
59
        ];
60
61
        $this->client->shouldReceive('rawCommand')->andReturn($key);
62
63
        $stream = new Stream($this->client, self::TEST_NAME_STREAM);
64
65
        $result = $stream->add($key, $values);
66
67
        $this->assertEquals($key, $result);
68
    }
69
70
    /**
71
     * Read empty stream
72
     *
73
     * @throws \Exception
74
     *
75
     * @return void
76
     */
77
    public function testReadStream() : void
78
    {
79
        $data = [
80
            [
81
                'name',
82
                [
83
                    [
84
                        'id',
85
                        'key',
86
                        'body'
87
                    ]
88
                ]
89
            ]
90
        ];
91
92
        $this->client->shouldReceive('rawCommand')->andReturn($data);
93
94
        $stream = new Stream($this->client, self::TEST_NAME_STREAM);
95
96
        $collectionStream = $stream->get();
97
98
        $this->assertEquals(Collection::create($data), $collectionStream);
99
100
    }
101
102
    /**
103
     * Delete message
104
     *
105
     * @throws \Exception
106
     *
107
     * @return void
108
     */
109
    public function testDeleteMessage() : void
110
    {
111
        $key = 'name';
112
113
        $values = [
114
            'id'   => 123,
115
            'name' => 'Barney',
116
            'age'  => 25,
117
        ];
118
119
        $this->client->shouldReceive('rawCommand')->andReturn($key);
120
121
        $stream = new Stream($this->client, self::TEST_NAME_STREAM);
122
123
        $stream->add($key, $values);
124
125
        $result = $stream->delete($key);
126
127
        $this->assertIsInt($result);
128
    }
129
}