Skip to content

Commit

Permalink
Merge pull request #3 from ndthuan/refactor-tests
Browse files Browse the repository at this point in the history
Refactor tests
  • Loading branch information
ndthuan authored Nov 25, 2018
2 parents abc3f58 + ee3530e commit fff48bb
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 57 deletions.
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"require": {
"php": "^7.0",
"aws/aws-sdk-php": "^3.71",
"psr/log": "^1.0"
"psr/log": "^1.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.5.13",
Expand All @@ -22,9 +23,11 @@
],
"autoload": {
"psr-4": {
"Ndthuan\\AwsSqsWrapper\\": "src/",
"Ndthuan\\Tests\\AwsSqsWrapper\\Unit\\": "tests/unit/",
"Ndthuan\\Tests\\AwsSqsWrapper\\Integration\\": "tests/integration/"
"Ndthuan\\AwsSqsWrapper\\": "src/"
}
},
"scripts": {
"test": "phpunit --testsuite unit",
"analyze": "phpcs --standard=PSR2 src/"
}
}
5 changes: 5 additions & 0 deletions install-xdebug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

pecl list | grep -i xdebug > /dev/null || yes | pecl install xdebug

echo 'zend_extension=xdebug.so' > /usr/local/etc/php/conf.d/xdebug.ini
5 changes: 2 additions & 3 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ trap cleanup EXIT
docker-compose up -d

docker-compose exec -T composer composer install --ignore-platform-reqs --no-scripts
docker-compose exec -T composer composer analyze

docker-compose exec -T netcat sh wait_for.sh localstack:4576 -t 60

docker-compose exec -T awscli aws --no-sign-request --endpoint-url http://localstack:4576 --region fake sqs create-queue --queue-name test-queue
docker-compose exec -T awscli aws --no-sign-request --endpoint-url http://localstack:4576 --region fake sqs purge-queue --queue-url http://localstack:4576/queue/test-queue

for phpver in php70 php71 php72; do
docker-compose exec -T ${phpver} sh -c 'pecl list | grep -i xdebug > /dev/null || yes | pecl install xdebug; echo "zend_extension=xdebug.so" > /usr/local/etc/php/conf.d/xdebug.ini'
docker-compose exec -T ${phpver} vendor/bin/phpcs --standard=PSR2 src/
docker-compose exec -T ${phpver} vendor/bin/phpunit --coverage-html=tests/report/${phpver}-coverage
docker-compose exec -T ${phpver} vendor/bin/phpunit
done
24 changes: 24 additions & 0 deletions run_tests_with_coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

set -ex

function cleanup() {
docker-compose down
}

trap cleanup EXIT

docker-compose up -d

docker-compose exec -T composer composer install --ignore-platform-reqs --no-scripts
docker-compose exec -T composer composer analyze

docker-compose exec -T netcat sh wait_for.sh localstack:4576 -t 60

docker-compose exec -T awscli aws --no-sign-request --endpoint-url http://localstack:4576 --region fake sqs create-queue --queue-name test-queue
docker-compose exec -T awscli aws --no-sign-request --endpoint-url http://localstack:4576 --region fake sqs purge-queue --queue-url http://localstack:4576/queue/test-queue

for phpver in php70 php71 php72; do
docker-compose exec -T ${phpver} sh install-xdebug.sh
docker-compose exec -T ${phpver} vendor/bin/phpunit --coverage-html=tests/report/${phpver}-coverage
done
13 changes: 7 additions & 6 deletions src/Subscribing/AbstractSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Ndthuan\AwsSqsWrapper\Queue\Connector;
use Ndthuan\AwsSqsWrapper\Queue\ReceivedMessage;
use Ndthuan\AwsSqsWrapper\Queue\ResultMetadata;
use Ndthuan\AwsSqsWrapper\Subscribing\Callbacks\NullCallbacks;
use Ndthuan\AwsSqsWrapper\Subscribing\Callbacks\SubscriberCallbacksInterface;
use Ndthuan\AwsSqsWrapper\Subscribing\Exception\FatalException;
use Ndthuan\AwsSqsWrapper\Subscribing\Exception\LogicException;
Expand Down Expand Up @@ -34,18 +35,18 @@ abstract class AbstractSubscriber implements SubscriberInterface, MessageProcess
/**
* AbstractSubscriber constructor.
*
* @param Connector $queueConnector
* @param SubscriberCallbacksInterface $callbacks
* @param array $receiveMessageOptions
* @param Connector $queueConnector
* @param array $receiveMessageOptions
* @param SubscriberCallbacksInterface|null $callbacks
*/
public function __construct(
Connector $queueConnector,
SubscriberCallbacksInterface $callbacks,
array $receiveMessageOptions = []
array $receiveMessageOptions = [],
SubscriberCallbacksInterface $callbacks = null
) {
$this->queueConnector = $queueConnector;
$this->callbacks = $callbacks;
$this->receiveMessageOptions = $receiveMessageOptions;
$this->callbacks = $callbacks ?? new NullCallbacks();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Subscribing/Callbacks/LoggingCallbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function onMessageProcessed(ReceivedMessage $message)
*/
public function onLogicException(ReceivedMessage $message, LogicException $exception)
{
$this->logger->info('Deleted SQS message due to logical exception', [
$this->logger->debug('Deleted SQS message due to logical exception', [
'messageId' => $message->getId(),
'exception' => $exception,
]);
Expand Down
14 changes: 7 additions & 7 deletions src/Subscribing/DelegatorSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ class DelegatorSubscriber extends AbstractSubscriber
/**
* DelegatorSubscriber constructor.
*
* @param MessageProcessorInterface $messageProcessor
* @param Connector $queueConnector
* @param SubscriberCallbacksInterface $callbacks
* @param array $receiveMessageOptions
* @param MessageProcessorInterface $messageProcessor
* @param Connector $queueConnector
* @param array $receiveMessageOptions
* @param SubscriberCallbacksInterface|null $callbacks
*/
public function __construct(
MessageProcessorInterface $messageProcessor,
Connector $queueConnector,
SubscriberCallbacksInterface $callbacks,
array $receiveMessageOptions = []
array $receiveMessageOptions = [],
SubscriberCallbacksInterface $callbacks = null
) {
parent::__construct($queueConnector, $callbacks, $receiveMessageOptions);
parent::__construct($queueConnector, $receiveMessageOptions, $callbacks);

$this->messageProcessor = $messageProcessor;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/ConnectorTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Ndthuan\Tests\AwsSqsWrapper\Integration;
namespace Ndthuan\AwsSqsWrapper;

use Aws\Sqs\SqsClient;
use Ndthuan\AwsSqsWrapper\Queue\Connector;
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/Publishing/PublisherTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php
declare(strict_types=1);

namespace Ndthuan\Tests\AwsSqsWrapper\Unit\Publishing;
namespace Ndthuan\AwsSqsWrapper\Publishing;

use Ndthuan\AwsSqsWrapper\Queue\Connector;
use Ndthuan\AwsSqsWrapper\Publishing\Publisher;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down
3 changes: 1 addition & 2 deletions tests/unit/Queue/MessageAttributesTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php
declare(strict_types=1);

namespace Ndthuan\Tests\AwsSqsWrapper\Queue;
namespace Ndthuan\AwsSqsWrapper\Queue;

use Ndthuan\AwsSqsWrapper\Queue\MessageAttributes;
use PHPUnit\Framework\TestCase;

class MessageAttributesTest extends TestCase
Expand Down
103 changes: 103 additions & 0 deletions tests/unit/Subscribing/Callbacks/LoggingCallbacksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);

namespace Ndthuan\AwsSqsWrapper\Subscribing\Callbacks;

use Exception;
use Ndthuan\AwsSqsWrapper\Queue\ReceivedMessage;
use Ndthuan\AwsSqsWrapper\Subscribing\Exception\FatalException;
use Ndthuan\AwsSqsWrapper\Subscribing\Exception\LogicException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class LoggingCallbacksTest extends TestCase
{
/**
* @var LoggerInterface|MockObject
*/
private $loggerMock;

/**
* @var LoggingCallbacks
*/
private $loggingCallbacks;

protected function setUp()
{
parent::setUp();

$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
->getMock();

$this->loggingCallbacks = new LoggingCallbacks($this->loggerMock);
}

public function testOnMessageProcessed()
{
$this->loggerMock->expects($this->once())
->method('info')
->with('Successfully processed SQS message', ['messageId' => 'example-id']);

$this->loggingCallbacks->onMessageProcessed($this->createMessage());
}

public function testOnUncaughtException()
{
$thrownException = new Exception();

$this->loggerMock->expects($this->once())
->method('error')
->with(
'Uncaught exception when processing SQS message',
['messageId' => 'example-id', 'exception' => $thrownException]
);

$this->loggingCallbacks->onUncaughtException($this->createMessage(), $thrownException);
}

public function testOnMessageReceived()
{
$this->loggerMock->expects($this->once())
->method('info')
->with('Received SQS message', ['messageId' => 'example-id']);

$this->loggingCallbacks->onMessageReceived($this->createMessage());
}

public function testOnLogicException()
{
$thrownException = new LogicException();

$this->loggerMock->expects($this->once())
->method('debug')
->with(
'Deleted SQS message due to logical exception',
['messageId' => 'example-id', 'exception' => $thrownException]
);

$this->loggingCallbacks->onLogicException($this->createMessage(), $thrownException);
}

public function testOnFatalException()
{
$thrownException = new FatalException();

$this->loggerMock->expects($this->once())
->method('critical')
->with(
'Stopped SQS processing due to fatal exception',
['messageId' => 'example-id', 'exception' => $thrownException]
);

$this->loggingCallbacks->onFatalException($this->createMessage(), $thrownException);
}

/**
* @return ReceivedMessage
*/
private function createMessage()
{
return new ReceivedMessage('example-id', 'example-recept-handle', 'example-body-hash', 'example-body', []);
}
}
Loading

0 comments on commit fff48bb

Please sign in to comment.