diff --git a/pkg/snsqs/SnsQsProducer.php b/pkg/snsqs/SnsQsProducer.php index 1baeef307..bdce7f895 100644 --- a/pkg/snsqs/SnsQsProducer.php +++ b/pkg/snsqs/SnsQsProducer.php @@ -77,17 +77,28 @@ public function send(Destination $destination, Message $message): void } } + /** + * Delivery delay is supported by SQSProducer. + * + * @param int|null $deliveryDelay + * + * @return Producer + */ public function setDeliveryDelay(int $deliveryDelay = null): Producer { - $this->getSnsProducer()->setDeliveryDelay($deliveryDelay); $this->getSqsProducer()->setDeliveryDelay($deliveryDelay); return $this; } + /** + * Delivery delay is supported by SQSProducer. + * + * @return int|null + */ public function getDeliveryDelay(): ?int { - return $this->getSnsProducer()->getDeliveryDelay(); + return $this->getSqsProducer()->getDeliveryDelay(); } public function setPriority(int $priority = null): Producer diff --git a/pkg/snsqs/Tests/SnsQsProducerTest.php b/pkg/snsqs/Tests/SnsQsProducerTest.php new file mode 100644 index 000000000..336e32033 --- /dev/null +++ b/pkg/snsqs/Tests/SnsQsProducerTest.php @@ -0,0 +1,132 @@ +assertClassImplements(Producer::class, SnsQsProducer::class); + } + + public function testCouldBeConstructedWithRequiredArguments() + { + new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock()); + } + + public function testShouldThrowIfMessageIsInvalidType() + { + $this->expectException(InvalidMessageException::class); + $this->expectExceptionMessage('The message must be an instance of Enqueue\SnsQs\SnsQsMessage but it is Double\Message\P1'); + + $producer = new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock()); + + $message = $this->prophesize(Message::class)->reveal(); + + $producer->send(new SnsQsTopic(''), $message); + } + + public function testShouldThrowIfDestinationOfInvalidType() + { + $this->expectException(InvalidDestinationException::class); + + $producer = new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock()); + + $destination = $this->prophesize(Destination::class)->reveal(); + + $producer->send($destination, new SnsQsMessage()); + } + + public function testShouldSetDeliveryDelayToSQSProducer() + { + $delay = 10; + + $sqsProducerStub = $this->prophesize(SqsProducer::class); + $sqsProducerStub->setDeliveryDelay(Argument::is($delay))->shouldBeCalledTimes(1); + + $sqsMock = $this->createSqsContextMock(); + $sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal()); + + $producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock); + + $producer->setDeliveryDelay($delay); + } + + public function testShouldGetDeliveryDelayFromSQSProducer() + { + $delay = 10; + + $sqsProducerStub = $this->prophesize(SqsProducer::class); + $sqsProducerStub->getDeliveryDelay()->willReturn($delay); + + $sqsMock = $this->createSqsContextMock(); + $sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal()); + + $producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock); + + $this->assertEquals($delay, $producer->getDeliveryDelay()); + } + + public function testShouldSendSnsTopicMessageToSnsProducer() + { + $snsMock = $this->createSnsContextMock(); + $destination = new SnsQsTopic(''); + + $snsProducerStub = $this->prophesize(SnsProducer::class); + $snsProducerStub->send($destination, Argument::any())->shouldBeCalledOnce(); + + $snsMock->method('createProducer')->willReturn($snsProducerStub->reveal()); + + $producer = new SnsQsProducer($snsMock, $this->createSqsContextMock()); + $producer->send($destination, new SnsQsMessage()); + } + + public function testShouldSendSqsMessageToSqsProducer() + { + $sqsMock = $this->createSqsContextMock(); + $destination = new SnsQsQueue(''); + + $snsProducerStub = $this->prophesize(SqsProducer::class); + $snsProducerStub->send($destination, Argument::any())->shouldBeCalledOnce(); + + $sqsMock->method('createProducer')->willReturn($snsProducerStub->reveal()); + + $producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock); + $producer->send($destination, new SnsQsMessage()); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|SnsContext + */ + private function createSnsContextMock(): SnsContext + { + return $this->createMock(SnsContext::class); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|SqsContext + */ + private function createSqsContextMock(): SqsContext + { + return $this->createMock(SqsContext::class); + } +}