diff --git a/.gitignore b/.gitignore index 0bafb8d..f51e2d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ coverage .idea vendor -build \ No newline at end of file +build +.phpunit.result.cache \ No newline at end of file diff --git a/tests/Functional/Connector/SQSTest.php b/tests/Functional/Connector/SQSTest.php index 8300d71..cff6fbb 100644 --- a/tests/Functional/Connector/SQSTest.php +++ b/tests/Functional/Connector/SQSTest.php @@ -4,15 +4,21 @@ namespace Tests\Functional\Connector; +use Mockery; use SykesCottages\Qu\Connector\SQS; use SykesCottages\Qu\Message\Contract\Message; use SykesCottages\Qu\Message\SQSMessage; +use Tests\Functional\Connector\Stubs\SQSCallable; use Tests\Functional\FunctionalTestCase; class SQSTest extends FunctionalTestCase { private const DEFAULT_NUMBER_OF_URLS = 2; + private const MAX_NUMBER_OF_MESSAGES_PER_POLL = 10; + + private const MIN_NUMBER_OF_MESSAGES_PER_POLL = 1; + private const QUEUE_NAME = 'test'; /** @@ -118,6 +124,59 @@ function () { ); } + public function retrieveMinAndMaxDataProvider() : array + { + return [ + 'Test if we specify more than the MAX it will cap to the MAX value allowed by SQS' => [ + 100, + self::MAX_NUMBER_OF_MESSAGES_PER_POLL, + ], + 'Test if we specify less than the MIN value it will cap to the MIN value' => [ + 0, + self::MIN_NUMBER_OF_MESSAGES_PER_POLL, + ], + ]; + } + + /** + * @param int $numberOfMessagesToConsumeAtATime + * @param int $expectedNumberOfCallbackCalls + * + * @dataProvider retrieveMinAndMaxDataProvider + */ + public function testWeCanOnlyRetrieveMessagesBetweenTheMaxAndMin( + int $numberOfMessagesToConsumeAtATime, + int $expectedNumberOfCallbackCalls + ): void { + $this->addMultipleMessagesToQueue(100); + + $this->sqs->setQueueOptions([ + 'maxNumberOfMessagesPerConsume' => $numberOfMessagesToConsumeAtATime, + ]); + + $mock = Mockery::mock(SQSCallable::class); + + $mock->shouldReceive('__invoke') + ->times($expectedNumberOfCallbackCalls); + + $this->sqs->consume( + $this->testingQueueUrl, + $mock, + function () { + $this->assertFunctionIsNotCalled(); + } + ); + } + + private function addMultipleMessagesToQueue(int $messagesToSend) : void + { + $countOfMessages = 0; + while ($countOfMessages < $messagesToSend) { + $this->addMessageToQueue(); + $countOfMessages++; + } + } + private function addMessageToQueue(): void { $this->sqs->queueMessage($this->testingQueueUrl, ['example' => 'test']); diff --git a/tests/Functional/Connector/Stubs/SQSCallable.php b/tests/Functional/Connector/Stubs/SQSCallable.php new file mode 100644 index 0000000..814d350 --- /dev/null +++ b/tests/Functional/Connector/Stubs/SQSCallable.php @@ -0,0 +1,12 @@ +assertTrue(true); } + + public function tearDown() : void + { + $container = \Mockery::getContainer(); + + if ($container) { + $this->addToAssertionCount($container->mockery_getExpectationCount()); + } + + parent::tearDown(); + } }