Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move SQS driver to new namespace #344

Merged
merged 1 commit into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions doc/drivers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -459,21 +459,21 @@ require a HTTP request to amazon to be resolved.
<?php

use Aws\Sqs\SqsClient;
use Bernard\Driver\SqsDriver;
use Bernard\Driver\Sqs\Driver;

$connection = SqsClient::factory(array(
'key' => 'your-aws-access-key',
'secret' => 'your-aws-secret-key',
'region' => 'the-aws-region-you-choose'
));

$driver = new SqsDriver($connection);
$driver = new Driver($connection);

// or with prefetching
$driver = new SqsDriver($connection, array(), 5);
$driver = new Driver($connection, array(), 5);

// or with aliased queue urls
$driver = new SqsDriver($connection, array(
$driver = new Driver($connection, array(
'queue-name' => 'queue-url',
));

Expand Down
4 changes: 2 additions & 2 deletions example/sqs.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Aws\Sqs\SqsClient;
use Bernard\Driver\SqsDriver;
use Bernard\Driver\Sqs\Driver;

/**
* Must be defined before including bootstrap.php
Expand All @@ -14,7 +14,7 @@ function get_driver() {
'region' => getenv('SQS_REGION')
));

return new SqsDriver($sqs);
return new Driver($sqs);
}

if (!getenv('ACCESS_KEY') || !getenv('SECRET_KEY') || !getenv('SQS_REGION')) {
Expand Down
11 changes: 6 additions & 5 deletions src/Driver/SqsDriver.php → src/Driver/Sqs/Driver.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<?php

namespace Bernard\Driver;
namespace Bernard\Driver\Sqs;

use Aws\Sqs\Exception\SqsException;
use Aws\Sqs\SqsClient;
use Bernard\Driver\AbstractPrefetchDriver;

/**
* Implements a Driver for use with AWS SQS client API:
* @link http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sqs-2012-11-05.html
*
* @package Bernard
*/
class SqsDriver extends AbstractPrefetchDriver
final class Driver extends AbstractPrefetchDriver
{
const AWS_SQS_FIFO_SUFFIX = '.fifo';
const AWS_SQS_EXCEPTION_BAD_REQUEST = 400;

protected $sqs;
protected $queueUrls;
private $sqs;
private $queueUrls;

/**
* @param SqsClient $sqs
Expand Down Expand Up @@ -257,7 +258,7 @@ public function info()
*
* @throws SqsException
*/
protected function resolveUrl($queueName)
private function resolveUrl($queueName)
{
if (isset($this->queueUrls[$queueName])) {
return $this->queueUrls[$queueName];
Expand Down
25 changes: 16 additions & 9 deletions tests/Driver/SqsDriverTest.php → tests/Driver/Sqs/DriverTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
<?php

namespace Bernard\Tests\Driver;
namespace Bernard\Tests\Driver\Sqs;

use Aws\Result;
use Aws\Sqs\SqsClient;
use Bernard\Driver\SqsDriver;
use Bernard\Driver\Sqs\Driver;
use Guzzle\Service\Resource\Model;
use Bernard\Driver\AbstractPrefetchDriver;

class SqsDriverTest extends \PHPUnit\Framework\TestCase
class DriverTest extends \PHPUnit\Framework\TestCase
{
const DUMMY_QUEUE_NAME = 'my-queue';
const DUMMY_FIFO_QUEUE_NAME = 'my-queue.fifo';
const DUMMY_QUEUE_URL_PREFIX = 'https://sqs.eu-west-1.amazonaws.com/123123';

/** @var \PHPUnit_Framework_MockObject_MockObject */
private $sqs;

/** @var Driver */
private $driver;

public function setUp()
{
$this->sqs = $this->getMockBuilder('Aws\Sqs\SqsClient')
$this->sqs = $this->getMockBuilder(SqsClient::class)
->disableOriginalConstructor()
->setMethods(array(
'createQueue',
Expand All @@ -29,20 +36,20 @@ public function setUp()
))
->getMock();

$this->driver = new SqsDriver($this->sqs, ['send-newsletter' => 'url']);
$this->driver = new Driver($this->sqs, ['send-newsletter' => 'url']);
}

public function testItExposesInfo()
{
$driver = new SqsDriver($this->sqs, [], 10);
$driver = new Driver($this->sqs, [], 10);

$this->assertEquals(['prefetch' => 10], $driver->info());
$this->assertEquals(['prefetch' => 2], $this->driver->info());
}

public function testItImplementsDriverInterface()
{
$this->assertInstanceOf('Bernard\Driver\AbstractPrefetchDriver', $this->driver);
$this->assertInstanceOf(AbstractPrefetchDriver::class, $this->driver);
}

public function testItCreatesQueue()
Expand Down Expand Up @@ -127,7 +134,7 @@ public function testUnresolveableQueueNameThrowsException()

public function testItGetsAllQueues()
{
$driver = new SqsDriver($this->sqs, [
$driver = new Driver($this->sqs, [
'import-users' => 'alreadyknowurl/import_users_prod'
]);

Expand Down Expand Up @@ -195,7 +202,7 @@ public function testItPushesMessagesToFifoQueue()
->with($this->equalTo([
'QueueUrl' => self::DUMMY_QUEUE_URL_PREFIX. '/'. self::DUMMY_FIFO_QUEUE_NAME,
'MessageBody' => $message,
'MessageGroupId' => \Bernard\Driver\SqsDriver::class . '::pushMessage',
'MessageGroupId' => \Bernard\Driver\Sqs\Driver::class . '::pushMessage',
'MessageDeduplicationId' => md5($message),

]));
Expand Down