|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\SnsQs\Tests; |
| 4 | + |
| 5 | +use Enqueue\Sns\SnsContext; |
| 6 | +use Enqueue\Sns\SnsProducer; |
| 7 | +use Enqueue\SnsQs\SnsQsMessage; |
| 8 | +use Enqueue\SnsQs\SnsQsProducer; |
| 9 | +use Enqueue\SnsQs\SnsQsQueue; |
| 10 | +use Enqueue\SnsQs\SnsQsTopic; |
| 11 | +use Enqueue\Sqs\SqsContext; |
| 12 | +use Enqueue\Sqs\SqsProducer; |
| 13 | +use Enqueue\Test\ClassExtensionTrait; |
| 14 | +use Interop\Queue\Destination; |
| 15 | +use Interop\Queue\Exception\InvalidDestinationException; |
| 16 | +use Interop\Queue\Exception\InvalidMessageException; |
| 17 | +use Interop\Queue\Message; |
| 18 | +use Interop\Queue\Producer; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Prophecy\Argument; |
| 21 | + |
| 22 | +class SnsQsProducerTest extends TestCase |
| 23 | +{ |
| 24 | + use ClassExtensionTrait; |
| 25 | + |
| 26 | + public function testShouldImplementProducerInterface() |
| 27 | + { |
| 28 | + $this->assertClassImplements(Producer::class, SnsQsProducer::class); |
| 29 | + } |
| 30 | + |
| 31 | + public function testCouldBeConstructedWithRequiredArguments() |
| 32 | + { |
| 33 | + new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock()); |
| 34 | + } |
| 35 | + |
| 36 | + public function testShouldThrowIfMessageIsInvalidType() |
| 37 | + { |
| 38 | + $this->expectException(InvalidMessageException::class); |
| 39 | + $this->expectExceptionMessage('The message must be an instance of Enqueue\SnsQs\SnsQsMessage but it is Double\Message\P1'); |
| 40 | + |
| 41 | + $producer = new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock()); |
| 42 | + |
| 43 | + $message = $this->prophesize(Message::class)->reveal(); |
| 44 | + |
| 45 | + $producer->send(new SnsQsTopic(''), $message); |
| 46 | + } |
| 47 | + |
| 48 | + public function testShouldThrowIfDestinationOfInvalidType() |
| 49 | + { |
| 50 | + $this->expectException(InvalidDestinationException::class); |
| 51 | + |
| 52 | + $producer = new SnsQsProducer($this->createSnsContextMock(), $this->createSqsContextMock()); |
| 53 | + |
| 54 | + $destination = $this->prophesize(Destination::class)->reveal(); |
| 55 | + |
| 56 | + $producer->send($destination, new SnsQsMessage()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testShouldSetDeliveryDelayToSQSProducer() |
| 60 | + { |
| 61 | + $delay = 10; |
| 62 | + |
| 63 | + $sqsProducerStub = $this->prophesize(SqsProducer::class); |
| 64 | + $sqsProducerStub->setDeliveryDelay(Argument::is($delay))->shouldBeCalledTimes(1); |
| 65 | + |
| 66 | + $sqsMock = $this->createSqsContextMock(); |
| 67 | + $sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal()); |
| 68 | + |
| 69 | + $producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock); |
| 70 | + |
| 71 | + $producer->setDeliveryDelay($delay); |
| 72 | + } |
| 73 | + |
| 74 | + public function testShouldGetDeliveryDelayFromSQSProducer() |
| 75 | + { |
| 76 | + $delay = 10; |
| 77 | + |
| 78 | + $sqsProducerStub = $this->prophesize(SqsProducer::class); |
| 79 | + $sqsProducerStub->getDeliveryDelay()->willReturn($delay); |
| 80 | + |
| 81 | + $sqsMock = $this->createSqsContextMock(); |
| 82 | + $sqsMock->method('createProducer')->willReturn($sqsProducerStub->reveal()); |
| 83 | + |
| 84 | + $producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock); |
| 85 | + |
| 86 | + $this->assertEquals($delay, $producer->getDeliveryDelay()); |
| 87 | + } |
| 88 | + |
| 89 | + public function testShouldSendSnsTopicMessageToSnsProducer() |
| 90 | + { |
| 91 | + $snsMock = $this->createSnsContextMock(); |
| 92 | + $destination = new SnsQsTopic(''); |
| 93 | + |
| 94 | + $snsProducerStub = $this->prophesize(SnsProducer::class); |
| 95 | + $snsProducerStub->send($destination, Argument::any())->shouldBeCalledOnce(); |
| 96 | + |
| 97 | + $snsMock->method('createProducer')->willReturn($snsProducerStub->reveal()); |
| 98 | + |
| 99 | + $producer = new SnsQsProducer($snsMock, $this->createSqsContextMock()); |
| 100 | + $producer->send($destination, new SnsQsMessage()); |
| 101 | + } |
| 102 | + |
| 103 | + public function testShouldSendSqsMessageToSqsProducer() |
| 104 | + { |
| 105 | + $sqsMock = $this->createSqsContextMock(); |
| 106 | + $destination = new SnsQsQueue(''); |
| 107 | + |
| 108 | + $snsProducerStub = $this->prophesize(SqsProducer::class); |
| 109 | + $snsProducerStub->send($destination, Argument::any())->shouldBeCalledOnce(); |
| 110 | + |
| 111 | + $sqsMock->method('createProducer')->willReturn($snsProducerStub->reveal()); |
| 112 | + |
| 113 | + $producer = new SnsQsProducer($this->createSnsContextMock(), $sqsMock); |
| 114 | + $producer->send($destination, new SnsQsMessage()); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @return \PHPUnit_Framework_MockObject_MockObject|SnsContext |
| 119 | + */ |
| 120 | + private function createSnsContextMock(): SnsContext |
| 121 | + { |
| 122 | + return $this->createMock(SnsContext::class); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @return \PHPUnit_Framework_MockObject_MockObject|SqsContext |
| 127 | + */ |
| 128 | + private function createSqsContextMock(): SqsContext |
| 129 | + { |
| 130 | + return $this->createMock(SqsContext::class); |
| 131 | + } |
| 132 | +} |
0 commit comments