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