forked from php-enqueue/enqueue-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace Enqueue\SnsQs\Tests; | ||
|
||
use Enqueue\Sns\SnsContext; | ||
use Enqueue\Sns\SnsProducer; | ||
use Enqueue\SnsQs\SnsQsMessage; | ||
use Enqueue\SnsQs\SnsQsProducer; | ||
use Enqueue\SnsQs\SnsQsQueue; | ||
use Enqueue\SnsQs\SnsQsTopic; | ||
use Enqueue\Sqs\SqsContext; | ||
use Enqueue\Sqs\SqsProducer; | ||
use Enqueue\Test\ClassExtensionTrait; | ||
use Interop\Queue\Destination; | ||
use Interop\Queue\Exception\InvalidDestinationException; | ||
use Interop\Queue\Exception\InvalidMessageException; | ||
use Interop\Queue\Message; | ||
use Interop\Queue\Producer; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
|
||
class SnsQsProducerTest extends TestCase | ||
{ | ||
use ClassExtensionTrait; | ||
|
||
public function testShouldImplementProducerInterface() | ||
{ | ||
$this->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); | ||
} | ||
} |