1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of graze/queue. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @license https://github.com/graze/queue/blob/master/LICENSE MIT |
12
|
|
|
* |
13
|
|
|
* @link https://github.com/graze/queue |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Graze\Queue\Adapter; |
17
|
|
|
|
18
|
|
|
use Graze\Queue\Message\MessageFactoryInterface; |
19
|
|
|
use Graze\Queue\Message\MessageInterface; |
20
|
|
|
use Mockery as m; |
21
|
|
|
use Mockery\MockInterface; |
22
|
|
|
use Graze\Queue\Test\TestCase; |
23
|
|
|
|
24
|
|
|
class ArrayAdapterTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
/** @var MessageFactoryInterface|MockInterface */ |
27
|
|
|
private $factory; |
28
|
|
|
/** @var MessageInterface|MockInterface */ |
29
|
|
|
private $messageA; |
30
|
|
|
/** @var MessageInterface|MockInterface */ |
31
|
|
|
private $messageB; |
32
|
|
|
/** @var MessageInterface|MockInterface */ |
33
|
|
|
private $messageC; |
34
|
|
|
/** @var MessageInterface[]|MockInterface[] */ |
35
|
|
|
private $messages; |
36
|
|
|
/** @var ArrayAdapter */ |
37
|
|
|
private $adapter; |
38
|
|
|
|
39
|
|
|
public function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->factory = m::mock(MessageFactoryInterface::class); |
42
|
|
|
|
43
|
|
|
$this->messageA = $a = m::mock(MessageInterface::class); |
44
|
|
|
$this->messageB = $b = m::mock(MessageInterface::class); |
45
|
|
|
$this->messageC = $c = m::mock(MessageInterface::class); |
46
|
|
|
$this->messages = [$a, $b, $c]; |
47
|
|
|
|
48
|
|
|
$this->adapter = new ArrayAdapter($this->messages); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testInterface() |
52
|
|
|
{ |
53
|
|
|
assertThat($this->adapter, is(anInstanceOf(AdapterInterface::class))); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testAcknowledge() |
57
|
|
|
{ |
58
|
|
|
$this->adapter->acknowledge($this->messages); |
59
|
|
|
|
60
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
|
|
|
|
61
|
|
|
|
62
|
|
|
assertThat(iterator_to_array($iterator), is(identicalTo([]))); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testAcknowledgeOne() |
66
|
|
|
{ |
67
|
|
|
$this->adapter->acknowledge([$this->messageB]); |
68
|
|
|
|
69
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
|
|
|
|
70
|
|
|
|
71
|
|
|
assertThat(iterator_to_array($iterator), is(identicalTo([$this->messageA, $this->messageC]))); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testReject() |
75
|
|
|
{ |
76
|
|
|
$this->adapter->reject($this->messages); |
77
|
|
|
|
78
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
|
|
|
|
79
|
|
|
|
80
|
|
|
assertThat(iterator_to_array($iterator), is(identicalTo($this->messages))); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testRejectOne() |
84
|
|
|
{ |
85
|
|
|
$this->adapter->reject([$this->messageA]); |
86
|
|
|
|
87
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
|
|
|
|
88
|
|
|
|
89
|
|
|
assertThat(iterator_to_array($iterator), is(identicalTo($this->messages))); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testDequeue() |
93
|
|
|
{ |
94
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
|
|
|
|
95
|
|
|
|
96
|
|
|
assertThat(iterator_to_array($iterator), is(identicalTo($this->messages))); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testDequeueWithLimit() |
100
|
|
|
{ |
101
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 1); |
|
|
|
|
102
|
|
|
|
103
|
|
|
assertThat(iterator_to_array($iterator), is(identicalTo([$this->messageA]))); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testDequeueWithPollingLimit() |
107
|
|
|
{ |
108
|
|
|
$iterator = $this->adapter->dequeue($this->factory, null); |
|
|
|
|
109
|
|
|
|
110
|
|
|
assertThat(iterator_to_array($iterator), is(identicalTo($this->messages))); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testDequeueWithNoMessages() |
114
|
|
|
{ |
115
|
|
|
$adapter = new ArrayAdapter(); |
116
|
|
|
|
117
|
|
|
$iterator = $adapter->dequeue($this->factory, null); |
|
|
|
|
118
|
|
|
|
119
|
|
|
assertThat(iterator_to_array($iterator), is(emptyArray())); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testDequeueWithLimitAndNoMessages() |
123
|
|
|
{ |
124
|
|
|
$adapter = new ArrayAdapter(); |
125
|
|
|
|
126
|
|
|
$iterator = $adapter->dequeue($this->factory, 10); |
|
|
|
|
127
|
|
|
|
128
|
|
|
assertThat(iterator_to_array($iterator), is(emptyArray())); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testEnqueue() |
132
|
|
|
{ |
133
|
|
|
$messageA = m::mock(MessageInterface::class); |
134
|
|
|
$messageB = m::mock(MessageInterface::class); |
135
|
|
|
$messageC = m::mock(MessageInterface::class); |
136
|
|
|
$messages = [$messageA, $messageB, $messageC]; |
137
|
|
|
$merged = array_merge($this->messages, $messages); |
138
|
|
|
|
139
|
|
|
$this->adapter->enqueue($messages); |
140
|
|
|
|
141
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
|
|
|
|
142
|
|
|
|
143
|
|
|
assertThat(iterator_to_array($iterator), is(identicalTo($merged))); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testPurge() |
147
|
|
|
{ |
148
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
|
|
|
|
149
|
|
|
|
150
|
|
|
assertThat(iterator_to_array($iterator), is(nonEmptyArray())); |
151
|
|
|
|
152
|
|
|
$this->adapter->purge(); |
153
|
|
|
|
154
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
155
|
|
|
|
156
|
|
|
assertThat(iterator_to_array($iterator), is(emptyArray())); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testDelete() |
160
|
|
|
{ |
161
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
|
|
|
|
162
|
|
|
|
163
|
|
|
assertThat(iterator_to_array($iterator), is(nonEmptyArray())); |
164
|
|
|
|
165
|
|
|
$this->adapter->delete(); |
166
|
|
|
|
167
|
|
|
$iterator = $this->adapter->dequeue($this->factory, 10); |
168
|
|
|
|
169
|
|
|
assertThat(iterator_to_array($iterator), is(emptyArray())); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|