|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Gearman\Tests; |
| 4 | + |
| 5 | +use Enqueue\Gearman\GearmanDestination; |
| 6 | +use Enqueue\Gearman\GearmanMessage; |
| 7 | +use Enqueue\Gearman\GearmanProducer; |
| 8 | +use Enqueue\Null\NullMessage; |
| 9 | +use Enqueue\Null\NullQueue; |
| 10 | +use Enqueue\Psr\InvalidDestinationException; |
| 11 | +use Enqueue\Psr\InvalidMessageException; |
| 12 | +use Enqueue\Test\ClassExtensionTrait; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class GearmanProducerTest extends TestCase |
| 16 | +{ |
| 17 | + use ClassExtensionTrait; |
| 18 | + |
| 19 | + public function testCouldBeConstructedWithGearmanClientAsFirstArgument() |
| 20 | + { |
| 21 | + new GearmanProducer($this->createGearmanClientMock()); |
| 22 | + } |
| 23 | + |
| 24 | + public function testThrowIfDestinationInvalid() |
| 25 | + { |
| 26 | + $producer = new GearmanProducer($this->createGearmanClientMock()); |
| 27 | + |
| 28 | + $this->expectException(InvalidDestinationException::class); |
| 29 | + $this->expectExceptionMessage('The destination must be an instance of Enqueue\Gearman\GearmanDestination but got Enqueue\Null\NullQueue.'); |
| 30 | + $producer->send(new NullQueue('aQueue'), new GearmanMessage()); |
| 31 | + } |
| 32 | + |
| 33 | + public function testThrowIfMessageInvalid() |
| 34 | + { |
| 35 | + $producer = new GearmanProducer($this->createGearmanClientMock()); |
| 36 | + |
| 37 | + $this->expectException(InvalidMessageException::class); |
| 38 | + $this->expectExceptionMessage('The message must be an instance of Enqueue\Gearman\GearmanMessage but it is Enqueue\Null\NullMessage.'); |
| 39 | + $producer->send(new GearmanDestination('aQueue'), new NullMessage()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testShouldJsonEncodeMessageAndPutToExpectedTube() |
| 43 | + { |
| 44 | + $message = new GearmanMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']); |
| 45 | + |
| 46 | + $gearman = $this->createGearmanClientMock(); |
| 47 | + $gearman |
| 48 | + ->expects($this->once()) |
| 49 | + ->method('doBackground') |
| 50 | + ->with( |
| 51 | + 'theQueueName', |
| 52 | + '{"body":"theBody","properties":{"foo":"fooVal"},"headers":{"bar":"barVal"}}' |
| 53 | + ) |
| 54 | + ; |
| 55 | + $gearman |
| 56 | + ->expects($this->once()) |
| 57 | + ->method('returnCode') |
| 58 | + ->willReturn(\GEARMAN_SUCCESS) |
| 59 | + ; |
| 60 | + |
| 61 | + $producer = new GearmanProducer($gearman); |
| 62 | + |
| 63 | + $producer->send( |
| 64 | + new GearmanDestination('theQueueName'), |
| 65 | + $message |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return \PHPUnit_Framework_MockObject_MockObject|\GearmanClient |
| 71 | + */ |
| 72 | + private function createGearmanClientMock() |
| 73 | + { |
| 74 | + return $this->createMock(\GearmanClient::class); |
| 75 | + } |
| 76 | +} |
0 commit comments