|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Pheanstalk\Tests; |
| 4 | + |
| 5 | +use Enqueue\Pheanstalk\PheanstalkConsumer; |
| 6 | +use Enqueue\Pheanstalk\PheanstalkDestination; |
| 7 | +use Enqueue\Pheanstalk\PheanstalkMessage; |
| 8 | +use Enqueue\Test\ClassExtensionTrait; |
| 9 | +use Pheanstalk\Job; |
| 10 | +use Pheanstalk\Pheanstalk; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | + |
| 13 | +class PheanstalkConsumerTest extends TestCase |
| 14 | +{ |
| 15 | + use ClassExtensionTrait; |
| 16 | + |
| 17 | + public function testCouldBeConstructedWithDestinationAndPheanstalkAsArguments() |
| 18 | + { |
| 19 | + new PheanstalkConsumer( |
| 20 | + new PheanstalkDestination('aQueueName'), |
| 21 | + $this->createPheanstalkMock() |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + public function testShouldReturnQueueSetInConstructor() |
| 26 | + { |
| 27 | + $destination = new PheanstalkDestination('aQueueName'); |
| 28 | + |
| 29 | + $consumer = new PheanstalkConsumer( |
| 30 | + $destination, |
| 31 | + $this->createPheanstalkMock() |
| 32 | + ); |
| 33 | + |
| 34 | + $this->assertSame($destination, $consumer->getQueue()); |
| 35 | + } |
| 36 | + |
| 37 | + public function testShouldReceiveFromQueueAndReturnNullIfNoMessageInQueue() |
| 38 | + { |
| 39 | + $destination = new PheanstalkDestination('theQueueName'); |
| 40 | + |
| 41 | + $pheanstalk = $this->createPheanstalkMock(); |
| 42 | + $pheanstalk |
| 43 | + ->expects($this->once()) |
| 44 | + ->method('reserveFromTube') |
| 45 | + ->with('theQueueName', 1) |
| 46 | + ->willReturn(null) |
| 47 | + ; |
| 48 | + |
| 49 | + $consumer = new PheanstalkConsumer($destination, $pheanstalk); |
| 50 | + |
| 51 | + $this->assertNull($consumer->receive(1000)); |
| 52 | + } |
| 53 | + |
| 54 | + public function testShouldReceiveFromQueueAndReturnMessageIfMessageInQueue() |
| 55 | + { |
| 56 | + $destination = new PheanstalkDestination('theQueueName'); |
| 57 | + $message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']); |
| 58 | + |
| 59 | + $job = new Job('theJobId', json_encode($message)); |
| 60 | + |
| 61 | + $pheanstalk = $this->createPheanstalkMock(); |
| 62 | + $pheanstalk |
| 63 | + ->expects($this->once()) |
| 64 | + ->method('reserveFromTube') |
| 65 | + ->with('theQueueName', 1) |
| 66 | + ->willReturn($job) |
| 67 | + ; |
| 68 | + |
| 69 | + $consumer = new PheanstalkConsumer($destination, $pheanstalk); |
| 70 | + |
| 71 | + $actualMessage = $consumer->receive(1000); |
| 72 | + |
| 73 | + $this->assertSame('theBody', $actualMessage->getBody()); |
| 74 | + $this->assertSame(['foo' => 'fooVal'], $actualMessage->getProperties()); |
| 75 | + $this->assertSame(['bar' => 'barVal'], $actualMessage->getHeaders()); |
| 76 | + $this->assertSame($job, $actualMessage->getJob()); |
| 77 | + } |
| 78 | + |
| 79 | + public function testShouldReceiveNoWaitFromQueueAndReturnNullIfNoMessageInQueue() |
| 80 | + { |
| 81 | + $destination = new PheanstalkDestination('theQueueName'); |
| 82 | + |
| 83 | + $pheanstalk = $this->createPheanstalkMock(); |
| 84 | + $pheanstalk |
| 85 | + ->expects($this->once()) |
| 86 | + ->method('reserveFromTube') |
| 87 | + ->with('theQueueName', 0) |
| 88 | + ->willReturn(null) |
| 89 | + ; |
| 90 | + |
| 91 | + $consumer = new PheanstalkConsumer($destination, $pheanstalk); |
| 92 | + |
| 93 | + $this->assertNull($consumer->receiveNoWait()); |
| 94 | + } |
| 95 | + |
| 96 | + public function testShouldReceiveNoWaitFromQueueAndReturnMessageIfMessageInQueue() |
| 97 | + { |
| 98 | + $destination = new PheanstalkDestination('theQueueName'); |
| 99 | + $message = new PheanstalkMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']); |
| 100 | + |
| 101 | + $job = new Job('theJobId', json_encode($message)); |
| 102 | + |
| 103 | + $pheanstalk = $this->createPheanstalkMock(); |
| 104 | + $pheanstalk |
| 105 | + ->expects($this->once()) |
| 106 | + ->method('reserveFromTube') |
| 107 | + ->with('theQueueName', 0) |
| 108 | + ->willReturn($job) |
| 109 | + ; |
| 110 | + |
| 111 | + $consumer = new PheanstalkConsumer($destination, $pheanstalk); |
| 112 | + |
| 113 | + $actualMessage = $consumer->receiveNoWait(); |
| 114 | + |
| 115 | + $this->assertSame('theBody', $actualMessage->getBody()); |
| 116 | + $this->assertSame(['foo' => 'fooVal'], $actualMessage->getProperties()); |
| 117 | + $this->assertSame(['bar' => 'barVal'], $actualMessage->getHeaders()); |
| 118 | + $this->assertSame($job, $actualMessage->getJob()); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return \PHPUnit_Framework_MockObject_MockObject|Pheanstalk |
| 123 | + */ |
| 124 | + private function createPheanstalkMock() |
| 125 | + { |
| 126 | + return $this->createMock(Pheanstalk::class); |
| 127 | + } |
| 128 | +} |
0 commit comments