Skip to content

[pheanstalk] Add unit tests for PheanstalkConsumer #726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/pheanstalk/PheanstalkConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ public function acknowledge(Message $message): void
*/
public function reject(Message $message, bool $requeue = false): void
{
InvalidMessageException::assertMessageInstanceOf($message, PheanstalkMessage::class);

if (false == $message->getJob()) {
throw new \LogicException(sprintf(
'The message could not be %s because it does not have job set.',
$requeue ? 'requeued' : 'rejected'
));
}

if ($requeue) {
$this->pheanstalk->release($message->getJob(), $message->getPriority(), $message->getDelay());

Expand Down
105 changes: 104 additions & 1 deletion pkg/pheanstalk/Tests/PheanstalkConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testShouldReceiveNoWaitFromQueueAndReturnNullIfNoMessageInQueue(
public function testShouldReceiveNoWaitFromQueueAndReturnMessageIfMessageInQueue()
{
$destination = new PheanstalkDestination('theQueueName');
$message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
$message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);

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

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

public function testShouldAcknowledgeMessage()
{
$destination = new PheanstalkDestination('theQueueName');
$message = new PheanstalkMessage();

$job = new Job('theJobId', json_encode($message));
$message->setJob($job);

$pheanstalk = $this->createPheanstalkMock();
$pheanstalk
->expects($this->once())
->method('delete')
->with($job)
;

$consumer = new PheanstalkConsumer($destination, $pheanstalk);

$consumer->acknowledge($message);
}

public function testAcknowledgeShouldThrowExceptionIfMessageHasNoJob()
{
$destination = new PheanstalkDestination('theQueueName');
$pheanstalk = $this->createPheanstalkMock();

$consumer = new PheanstalkConsumer($destination, $pheanstalk);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The message could not be acknowledged because it does not have job set.');

$consumer->acknowledge(new PheanstalkMessage());
}

public function testShouldRejectMessage()
{
$destination = new PheanstalkDestination('theQueueName');
$message = new PheanstalkMessage();

$job = new Job('theJobId', json_encode($message));
$message->setJob($job);

$pheanstalk = $this->createPheanstalkMock();
$pheanstalk
->expects($this->once())
->method('delete')
->with($job)
;

$consumer = new PheanstalkConsumer($destination, $pheanstalk);

$consumer->reject($message);
}

public function testRejectShouldThrowExceptionIfMessageHasNoJob()
{
$destination = new PheanstalkDestination('theQueueName');
$pheanstalk = $this->createPheanstalkMock();

$consumer = new PheanstalkConsumer($destination, $pheanstalk);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The message could not be rejected because it does not have job set.');

$consumer->reject(new PheanstalkMessage());
}

public function testShouldRequeueMessage()
{
$destination = new PheanstalkDestination('theQueueName');
$message = new PheanstalkMessage();

$job = new Job('theJobId', json_encode($message));
$message->setJob($job);

$pheanstalk = $this->createPheanstalkMock();
$pheanstalk
->expects($this->once())
->method('release')
->with($job, Pheanstalk::DEFAULT_PRIORITY, Pheanstalk::DEFAULT_DELAY)
;
$pheanstalk
->expects($this->never())
->method('delete')
;

$consumer = new PheanstalkConsumer($destination, $pheanstalk);

$consumer->reject($message, true);
}

public function testRequeueShouldThrowExceptionIfMessageHasNoJob()
{
$destination = new PheanstalkDestination('theQueueName');
$pheanstalk = $this->createPheanstalkMock();

$consumer = new PheanstalkConsumer($destination, $pheanstalk);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The message could not be requeued because it does not have job set.');

$consumer->reject(new PheanstalkMessage(), true);
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|Pheanstalk
*/
Expand Down