Skip to content

Commit 8cb976b

Browse files
committed
Add unit tests for PheanstalkConsumer
1 parent 1eb6b85 commit 8cb976b

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

Diff for: pkg/pheanstalk/PheanstalkConsumer.php

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ public function acknowledge(Message $message): void
8888
*/
8989
public function reject(Message $message, bool $requeue = false): void
9090
{
91+
InvalidMessageException::assertMessageInstanceOf($message, PheanstalkMessage::class);
92+
93+
if (false == $message->getJob()) {
94+
throw new \LogicException(sprintf(
95+
'The message could not be %s because it does not have job set.',
96+
$requeue ? 'requeued' : 'rejected'
97+
));
98+
}
99+
91100
if ($requeue) {
92101
$this->pheanstalk->release($message->getJob(), $message->getPriority(), $message->getDelay());
93102

Diff for: pkg/pheanstalk/Tests/PheanstalkConsumerTest.php

+104-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testShouldReceiveNoWaitFromQueueAndReturnNullIfNoMessageInQueue(
9696
public function testShouldReceiveNoWaitFromQueueAndReturnMessageIfMessageInQueue()
9797
{
9898
$destination = new PheanstalkDestination('theQueueName');
99-
$message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
99+
$message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
100100

101101
$job = new Job('theJobId', json_encode($message));
102102

@@ -118,6 +118,109 @@ public function testShouldReceiveNoWaitFromQueueAndReturnMessageIfMessageInQueue
118118
$this->assertSame($job, $actualMessage->getJob());
119119
}
120120

121+
public function testShouldAcknowledgeMessage()
122+
{
123+
$destination = new PheanstalkDestination('theQueueName');
124+
$message = new PheanstalkMessage();
125+
126+
$job = new Job('theJobId', json_encode($message));
127+
$message->setJob($job);
128+
129+
$pheanstalk = $this->createPheanstalkMock();
130+
$pheanstalk
131+
->expects($this->once())
132+
->method('delete')
133+
->with($job)
134+
;
135+
136+
$consumer = new PheanstalkConsumer($destination, $pheanstalk);
137+
138+
$consumer->acknowledge($message);
139+
}
140+
141+
public function testAcknowledgeShouldThrowExceptionIfMessageHasNoJob()
142+
{
143+
$destination = new PheanstalkDestination('theQueueName');
144+
$pheanstalk = $this->createPheanstalkMock();
145+
146+
$consumer = new PheanstalkConsumer($destination, $pheanstalk);
147+
148+
$this->expectException(\LogicException::class);
149+
$this->expectExceptionMessage('The message could not be acknowledged because it does not have job set.');
150+
151+
$consumer->acknowledge(new PheanstalkMessage());
152+
}
153+
154+
public function testShouldRejectMessage()
155+
{
156+
$destination = new PheanstalkDestination('theQueueName');
157+
$message = new PheanstalkMessage();
158+
159+
$job = new Job('theJobId', json_encode($message));
160+
$message->setJob($job);
161+
162+
$pheanstalk = $this->createPheanstalkMock();
163+
$pheanstalk
164+
->expects($this->once())
165+
->method('delete')
166+
->with($job)
167+
;
168+
169+
$consumer = new PheanstalkConsumer($destination, $pheanstalk);
170+
171+
$consumer->reject($message);
172+
}
173+
174+
public function testRejectShouldThrowExceptionIfMessageHasNoJob()
175+
{
176+
$destination = new PheanstalkDestination('theQueueName');
177+
$pheanstalk = $this->createPheanstalkMock();
178+
179+
$consumer = new PheanstalkConsumer($destination, $pheanstalk);
180+
181+
$this->expectException(\LogicException::class);
182+
$this->expectExceptionMessage('The message could not be rejected because it does not have job set.');
183+
184+
$consumer->reject(new PheanstalkMessage());
185+
}
186+
187+
public function testShouldRequeueMessage()
188+
{
189+
$destination = new PheanstalkDestination('theQueueName');
190+
$message = new PheanstalkMessage();
191+
192+
$job = new Job('theJobId', json_encode($message));
193+
$message->setJob($job);
194+
195+
$pheanstalk = $this->createPheanstalkMock();
196+
$pheanstalk
197+
->expects($this->once())
198+
->method('release')
199+
->with($job, Pheanstalk::DEFAULT_PRIORITY, Pheanstalk::DEFAULT_DELAY)
200+
;
201+
$pheanstalk
202+
->expects($this->never())
203+
->method('delete')
204+
;
205+
206+
$consumer = new PheanstalkConsumer($destination, $pheanstalk);
207+
208+
$consumer->reject($message, true);
209+
}
210+
211+
public function testRequeueShouldThrowExceptionIfMessageHasNoJob()
212+
{
213+
$destination = new PheanstalkDestination('theQueueName');
214+
$pheanstalk = $this->createPheanstalkMock();
215+
216+
$consumer = new PheanstalkConsumer($destination, $pheanstalk);
217+
218+
$this->expectException(\LogicException::class);
219+
$this->expectExceptionMessage('The message could not be requeued because it does not have job set.');
220+
221+
$consumer->reject(new PheanstalkMessage(), true);
222+
}
223+
121224
/**
122225
* @return \PHPUnit_Framework_MockObject_MockObject|Pheanstalk
123226
*/

0 commit comments

Comments
 (0)