1
|
|
|
<?php |
2
|
|
|
namespace Da\Mailer\Test\Queue\Backend\Redis; |
3
|
|
|
|
4
|
|
|
use Da\Mailer\Model\MailMessage; |
5
|
|
|
use Da\Mailer\Queue\Backend\Beanstalk\BeanstalkdQueueStoreAdapter; |
6
|
|
|
use Da\Mailer\Queue\Backend\Beanstalk\BeanstalkdQueueStoreConnection; |
7
|
|
|
use Da\Mailer\Test\Fixture\FixtureHelper; |
8
|
|
|
use Mockery; |
9
|
|
|
use Pheanstalk\Job; |
10
|
|
|
use Pheanstalk\Response\ArrayResponse; |
11
|
|
|
use PHPUnit_Framework_TestCase; |
12
|
|
|
|
13
|
|
|
class BeanstalkdQueueStoreAdapterTest extends PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
private $mailJob; |
16
|
|
|
private $payload; |
17
|
|
|
|
18
|
|
|
protected function setUp() |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
$this->mailJob = FixtureHelper::getBeanstalkdMailJob(); |
22
|
|
|
$this->payload = json_encode( |
23
|
|
|
[ |
24
|
|
|
'id' => '123456789', |
25
|
|
|
'attempt' => $this->mailJob->getAttempt(), |
26
|
|
|
'message' => $this->mailJob->getMessage() |
27
|
|
|
] |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testEnqueueDequeueAndAcknowledge() |
32
|
|
|
{ |
33
|
|
|
$statsTubeResponse2 = new ArrayResponse( |
34
|
|
|
'test', [ |
35
|
|
|
'current-jobs-delayed' => 0, |
36
|
|
|
'current-jobs-urgent' => 0, |
37
|
|
|
'current-jobs-ready' => 0 |
38
|
|
|
] |
39
|
|
|
); |
40
|
|
|
$statsTubeResponse1= new ArrayResponse('test', ['current-jobs-delayed' => 1,]); |
41
|
|
|
$payload = json_decode($this->payload, true); |
42
|
|
|
$payload['job'] = new Job(1, 'demo'); |
43
|
|
|
$btJob2 = Mockery::mock('\Pheanstalk\Job') |
44
|
|
|
->shouldReceive('getData') |
45
|
|
|
->andReturn(json_encode($payload)) |
46
|
|
|
->getMock(); |
47
|
|
|
$btClient = Mockery::mock('\Pheanstalk\Pheanstalk') |
48
|
|
|
->shouldReceive('useTube') |
49
|
|
|
->with('mail_queue') |
50
|
|
|
->andReturnSelf() |
51
|
|
|
->shouldReceive('put') |
52
|
|
|
->andReturn(1) |
53
|
|
|
->shouldReceive('watchOnly') |
54
|
|
|
->andReturnSelf() |
55
|
|
|
->shouldReceive('statsTube') |
56
|
|
|
->twice() |
57
|
|
|
->andReturn($statsTubeResponse1, $statsTubeResponse2) |
58
|
|
|
->shouldReceive('reserve') |
59
|
|
|
->with(0) |
60
|
|
|
->andReturn($btJob2, null) |
61
|
|
|
->shouldReceive('delete') |
62
|
|
|
->andReturn(1) |
63
|
|
|
->getMock(); |
64
|
|
|
|
65
|
|
|
$btStoreConnection = Mockery::mock('\Da\Mailer\Queue\Backend\Beanstalk\BeanstalkdQueueStoreConnection') |
66
|
|
|
->shouldReceive('connect') |
67
|
|
|
->andReturnSelf() |
68
|
|
|
->shouldReceive('getInstance') |
69
|
|
|
->andReturn($btClient) |
70
|
|
|
->getMock(); |
71
|
|
|
|
72
|
|
|
$btQueueStore = new BeanstalkdQueueStoreAdapter($btStoreConnection); |
73
|
|
|
|
74
|
|
|
$this->assertSame($btQueueStore, $btQueueStore->init()); |
75
|
|
|
$this->assertTrue($btQueueStore->enqueue($this->mailJob) > 0); |
76
|
|
|
|
77
|
|
|
$this->assertTrue($btQueueStore->isEmpty() === false); |
78
|
|
|
|
79
|
|
|
$mailJob = $btQueueStore->dequeue(); |
80
|
|
|
|
81
|
|
|
$this->assertTrue($btQueueStore->isEmpty() === true); |
82
|
|
|
|
83
|
|
|
$this->assertTrue(!empty($mailJob->getMessage())); |
84
|
|
|
|
85
|
|
|
$dequeuedMailMessage = MailMessage::fromArray(json_decode($mailJob->getMessage(), true)); |
86
|
|
|
|
87
|
|
|
$this->assertEquals(FixtureHelper::getMailMessage(), $dequeuedMailMessage); |
88
|
|
|
|
89
|
|
|
$mailJob->markAsCompleted(); |
90
|
|
|
$btQueueStore->ack($mailJob); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$this->assertTrue($btQueueStore->dequeue() === null); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testEnqueDequeueWithDelay() |
96
|
|
|
{ |
97
|
|
|
$time = time() + 2; |
98
|
|
|
$payload = json_decode($this->payload, true); |
99
|
|
|
$payload['job'] = new Job(1, 'demo'); |
100
|
|
|
$btJob2 = Mockery::mock('\Pheanstalk\Job') |
101
|
|
|
->shouldReceive('getData') |
102
|
|
|
->andReturn(json_encode($payload)) |
103
|
|
|
->getMock(); |
104
|
|
|
$btClient = Mockery::mock('\Pheanstalk\Pheanstalk') |
105
|
|
|
->shouldReceive('useTube') |
106
|
|
|
->with(Mockery::mustBe('mail_queue')) |
107
|
|
|
->andReturnSelf() |
108
|
|
|
->shouldReceive('put') |
109
|
|
|
->withAnyArgs() |
110
|
|
|
->andReturn(1) |
111
|
|
|
->shouldReceive('watchOnly') |
112
|
|
|
->with(Mockery::mustBe('mail_queue')) |
113
|
|
|
->andReturnSelf() |
114
|
|
|
->shouldReceive('reserve') |
115
|
|
|
->with(0) |
116
|
|
|
->andReturn(null, $btJob2) |
117
|
|
|
->shouldReceive('delete') |
118
|
|
|
->andReturn(1) |
119
|
|
|
->getMock(); |
120
|
|
|
|
121
|
|
|
$btConnection = Mockery::mock('\Da\Mailer\Queue\Backend\Beanstalk\BeanstalkdQueueStoreConnection') |
122
|
|
|
->shouldReceive('connect') |
123
|
|
|
->andReturnSelf() |
124
|
|
|
->shouldReceive('getInstance') |
125
|
|
|
->andReturn($btClient) |
126
|
|
|
->getMock(); |
127
|
|
|
|
128
|
|
|
$btQueueStore = new BeanstalkdQueueStoreAdapter($btConnection); |
129
|
|
|
|
130
|
|
|
$mailJob = $this->mailJob; |
131
|
|
|
$mailJob->setTimeToSend($time); |
132
|
|
|
$this->assertTrue($btQueueStore->enqueue($mailJob) > 0); |
133
|
|
|
$this->assertTrue($btQueueStore->dequeue() === null); |
134
|
|
|
sleep(3); // sleep three seconds to expire in delayed |
135
|
|
|
$mailJob = $btQueueStore->dequeue(); // now it should have migrated |
136
|
|
|
|
137
|
|
|
$this->assertTrue(!empty($mailJob->getMessage())); |
138
|
|
|
|
139
|
|
|
$mailJob->markAsCompleted(); |
140
|
|
|
$btQueueStore->ack($mailJob); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @expectedException \Da\Mailer\Exception\InvalidCallException |
146
|
|
|
*/ |
147
|
|
|
public function testBadMethodCallExceptionOnAck() |
148
|
|
|
{ |
149
|
|
|
$mailJob = FixtureHelper::getBeanstalkdMailJob(); |
150
|
|
|
$connection = new BeanstalkdQueueStoreConnection([]); |
151
|
|
|
$btQueueStore = new BeanstalkdQueueStoreAdapter($connection); |
152
|
|
|
$btQueueStore->ack($mailJob); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testNonCompletedAck() |
156
|
|
|
{ |
157
|
|
|
$statsTubeResponse1 = new ArrayResponse('test', ['current-jobs-delayed' => 1,]); |
158
|
|
|
$statsTubeResponse2 = new ArrayResponse( |
159
|
|
|
'test', [ |
160
|
|
|
'current-jobs-delayed' => 0, |
161
|
|
|
'current-jobs-urgent' => 0, |
162
|
|
|
'current-jobs-ready' => 0 |
163
|
|
|
] |
164
|
|
|
); |
165
|
|
|
$payload = json_decode($this->payload, true); |
166
|
|
|
$payload['job'] = new Job(1, 'demo'); |
167
|
|
|
$btJob2 = Mockery::mock('\Pheanstalk\Job') |
168
|
|
|
->shouldReceive('getData') |
169
|
|
|
->andReturn(json_encode($payload)) |
170
|
|
|
->getMock(); |
171
|
|
|
$btClient = Mockery::mock('\Pheanstalk\Pheanstalk') |
|
|
|
|
172
|
|
|
->shouldReceive('useTube') |
173
|
|
|
->with(Mockery::mustBe('mail_queue')) |
174
|
|
|
->andReturnSelf() |
175
|
|
|
->shouldReceive('put') |
176
|
|
|
->withAnyArgs() |
177
|
|
|
->andReturn(3) |
178
|
|
|
->shouldReceive('statsTube') |
179
|
|
|
->twice() |
180
|
|
|
->andReturn($statsTubeResponse1, $statsTubeResponse2) |
181
|
|
|
->shouldReceive('watchOnly') |
182
|
|
|
->with(Mockery::mustBe('mail_queue')) |
183
|
|
|
->andReturnSelf() |
184
|
|
|
->shouldReceive('reserve') |
185
|
|
|
->with(0) |
186
|
|
|
->andReturn($btJob2) |
187
|
|
|
->shouldReceive('release') |
188
|
|
|
->andReturn(1) |
189
|
|
|
->shouldReceive('delete') |
190
|
|
|
->andReturn(1) |
191
|
|
|
->getMock(); |
192
|
|
|
|
193
|
|
|
$btConnection = Mockery::mock('\Da\Mailer\Queue\Backend\Beanstalk\BeanstalkdQueueStoreConnection') |
|
|
|
|
194
|
|
|
->shouldReceive('connect') |
195
|
|
|
->andReturnSelf() |
196
|
|
|
->shouldReceive('getInstance') |
197
|
|
|
->andReturn($btClient) |
198
|
|
|
->getMock(); |
199
|
|
|
|
200
|
|
|
$btQueueStore = new BeanstalkdQueueStoreAdapter($btConnection); |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
$this->assertSame($btQueueStore, $btQueueStore->init()); |
204
|
|
|
$this->assertTrue($btQueueStore->enqueue($this->mailJob) > 1); |
205
|
|
|
|
206
|
|
|
$this->assertTrue($btQueueStore->isEmpty() === false); |
207
|
|
|
|
208
|
|
|
$mailJob = $btQueueStore->dequeue(); |
209
|
|
|
|
210
|
|
|
$this->assertTrue($btQueueStore->isEmpty() === true); |
211
|
|
|
|
212
|
|
|
$this->assertTrue(!empty($mailJob->getMessage())); |
213
|
|
|
|
214
|
|
|
$dequeuedMailMessage = MailMessage::fromArray(json_decode($mailJob->getMessage(), true)); |
215
|
|
|
|
216
|
|
|
$this->assertEquals(FixtureHelper::getMailMessage(), $dequeuedMailMessage); |
217
|
|
|
$btQueueStore->ack($mailJob); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: