diff --git a/Makefile b/Makefile index 371e233..199deb5 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ SHELL = /bin/sh install: ## Download the depenedencies then build the image :rocket:. make 'composer-install --optimize-autoloader --ignore-platform-reqs' - docker build --tag graze-queue:latest . + docker build --tag graze/graze-queue:latest . composer-%: ## Run a composer command, `make "composer- [...]"`. docker run -it --rm \ @@ -22,17 +22,17 @@ test: ## Run the unit and intergration testsuites. test: test-unit test-intergration test-unit: ## Run the unit testsuite. - docker run --rm -t graze-queue \ + docker run --rm -t graze/graze-queue -v $$(pwd):/opt/graze/queue \ vendor/bin/phpunit --testsuite unit test-intergration: ## Run the integration testsuite. - docker run --rm -t graze-queue \ + docker run --rm -t graze/graze-queue -v $$(pwd):/opt/graze/queue \ vendor/bin/phpunit --testsuite integration test-coverage: ## Run the testsuites with coverage enabled. - docker run --rm -t graze-queue \ + docker run --rm -t graze/graze-queue -v $$(pwd):/opt/graze/queue \ vendor/bin/phpunit --coverage-text --testsuite unit - docker run --rm -t graze-queue \ + docker run --rm -t graze/graze-queue -v $$(pwd):/opt/graze/queue \ vendor/bin/phpunit --coverage-text --testsuite integration test-matrix: @@ -44,8 +44,8 @@ test-matrix: vendor/bin/phpunit --testsuite unit -clean: ## Stop running containers and clean up an images. - docker rmi graze-queue:latest +clean: ## Clean up any images. + docker rmi graze/graze-queue:latest help: ## Show this help message. echo "usage: make [target] ..." diff --git a/src/Adapter/AdapterInterface.php b/src/Adapter/AdapterInterface.php index eab5077..4db0a13 100644 --- a/src/Adapter/AdapterInterface.php +++ b/src/Adapter/AdapterInterface.php @@ -45,5 +45,13 @@ public function dequeue(MessageFactoryInterface $factory, $limit); */ public function enqueue(array $messages); + /** + * @return void + */ public function purge(); + + /** + * @return void + */ + public function delete(); } diff --git a/src/Adapter/ArrayAdapter.php b/src/Adapter/ArrayAdapter.php index 3eff7e2..52151fd 100644 --- a/src/Adapter/ArrayAdapter.php +++ b/src/Adapter/ArrayAdapter.php @@ -79,6 +79,14 @@ public function purge() $this->queue = []; } + /** + * {@inheritdoc} + */ + public function delete() + { + $this->purge(); + } + /** * @param MessageInterface $message */ diff --git a/src/Adapter/Exception/MethodNotSupportedException.php b/src/Adapter/Exception/MethodNotSupportedException.php new file mode 100644 index 0000000..6efd50c --- /dev/null +++ b/src/Adapter/Exception/MethodNotSupportedException.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license https://github.com/graze/queue/blob/master/LICENSE MIT + * + * @link https://github.com/graze/queue + */ + +namespace Graze\Queue\Adapter\Exception; + +use Exception; +use Graze\Queue\Adapter\AdapterInterface; +use Graze\Queue\Message\MessageInterface; + +/** + * Exception to throw when an implmentation of {@see \Graze\Queue\AdapterInterface} + * does not support a method on {@see \Graze\Queue\Adapter\AdapterInterface}. + */ +class MethodNotSupportedException extends AdapterException +{ + /** + * @var string + */ + protected $method; + + /** + * @param string $method + * @param AdapterInterface $adapter + * @param MessageInterface[] $messages + * @param array $debug + * @param Exception $previous + */ + public function __construct($method, AdapterInterface $adapter, array $messages, array $debug = [], Exception $previous = null) + { + $this->method = $method; + + parent::__construct(sprintf('Method `%s` is not supported by this adapter', $method), $adapter, $messages, $debug, $previous); + } + + /** + * @return string + */ + public function getMethod() + { + return $this->method; + } +} diff --git a/src/Adapter/SqsAdapter.php b/src/Adapter/SqsAdapter.php index 45c3399..fddb28c 100644 --- a/src/Adapter/SqsAdapter.php +++ b/src/Adapter/SqsAdapter.php @@ -198,6 +198,14 @@ public function purge() $this->client->purgeQueue(['QueueUrl' => $this->getQueueUrl()]); } + /** + * {@inheritdoc} + */ + public function delete() + { + $this->client->deleteQueue(['QueueUrl' => $this->getQueueUrl()]); + } + /** * @param MessageInterface[] $messages * diff --git a/src/Client.php b/src/Client.php index a40614c..7a4321a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -20,7 +20,7 @@ use Graze\Queue\Message\MessageFactory; use Graze\Queue\Message\MessageFactoryInterface; -final class Client implements ConsumerInterface, ProducerInterface +final class Client implements ConsumerInterface, DeleterInterface, ProducerInterface, PurgerInterface { /** * @param AdapterInterface @@ -92,6 +92,14 @@ public function purge() return $this->adapter->purge(); } + /** + * {@inheritdoc} + */ + public function delete() + { + return $this->adapter->delete(); + } + /** * @return callable */ diff --git a/src/DeleterInterface.php b/src/DeleterInterface.php new file mode 100644 index 0000000..21bf8d5 --- /dev/null +++ b/src/DeleterInterface.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license https://github.com/graze/queue/blob/master/LICENSE MIT + * + * @link https://github.com/graze/queue + */ + +namespace Graze\Queue; + +interface DeleterInterface +{ + /** + * @return void + */ + public function delete(); +} diff --git a/src/ProducerInterface.php b/src/ProducerInterface.php index b1c8f91..c5f5556 100644 --- a/src/ProducerInterface.php +++ b/src/ProducerInterface.php @@ -10,7 +10,7 @@ * * @license https://github.com/graze/queue/blob/master/LICENSE MIT * - * @link https://github.com/graze/queue + * @link https://github.com/graze/queue */ namespace Graze\Queue; @@ -24,9 +24,7 @@ interface ProducerInterface public function create($body, array $options = []); /** - * @param array $message + * @param array $messages */ public function send(array $messages); - - public function purge(); } diff --git a/src/PurgerInterface.php b/src/PurgerInterface.php new file mode 100644 index 0000000..d9737cf --- /dev/null +++ b/src/PurgerInterface.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license https://github.com/graze/queue/blob/master/LICENSE MIT + * + * @link https://github.com/graze/queue + */ + +namespace Graze\Queue; + +interface PurgerInterface +{ + /** + * @return void + */ + public function purge(); +} diff --git a/tests/integration/ArrayIntegrationTest.php b/tests/integration/ArrayIntegrationTest.php index 76609a3..f60af7a 100644 --- a/tests/integration/ArrayIntegrationTest.php +++ b/tests/integration/ArrayIntegrationTest.php @@ -94,4 +94,16 @@ public function testPurge() assertThat($msgs, is(emptyArray())); } + + public function testDelete() + { + $this->client->delete(); + + $msgs = []; + $this->client->receive(function ($msg) use (&$msgs) { + $msgs[] = $msg; + }, null); + + assertThat($msgs, is(emptyArray())); + } } diff --git a/tests/integration/SqsIntegrationTest.php b/tests/integration/SqsIntegrationTest.php index 684726f..13210f0 100644 --- a/tests/integration/SqsIntegrationTest.php +++ b/tests/integration/SqsIntegrationTest.php @@ -232,4 +232,16 @@ public function testPurge() assertThat($msgs, is(emptyArray())); } + + public function testDelete() + { + $url = $this->stubCreateQueue(); + + $deleteModel = m::mock('Aws\ResultInterface'); + $this->sqsClient->shouldReceive('deleteQueue')->once()->with([ + 'QueueUrl' => $url, + ])->andReturn($deleteModel); + + $this->client->delete(); + } } diff --git a/tests/unit/Adapter/ArrayAdapterTest.php b/tests/unit/Adapter/ArrayAdapterTest.php index e494cef..bd38e04 100644 --- a/tests/unit/Adapter/ArrayAdapterTest.php +++ b/tests/unit/Adapter/ArrayAdapterTest.php @@ -121,4 +121,17 @@ public function testPurge() assertThat(iterator_to_array($iterator), is(emptyArray())); } + + public function testDelete() + { + $iterator = $this->adapter->dequeue($this->factory, 10); + + assertThat(iterator_to_array($iterator), is(nonEmptyArray())); + + $this->adapter->delete(); + + $iterator = $this->adapter->dequeue($this->factory, 10); + + assertThat(iterator_to_array($iterator), is(emptyArray())); + } } diff --git a/tests/unit/Adapter/Exception/MethodNotSupportedExceptionTest.php b/tests/unit/Adapter/Exception/MethodNotSupportedExceptionTest.php new file mode 100644 index 0000000..07473a5 --- /dev/null +++ b/tests/unit/Adapter/Exception/MethodNotSupportedExceptionTest.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * @license https://github.com/graze/queue/blob/master/LICENSE MIT + * + * @link https://github.com/graze/queue + */ + +namespace Graze\Queue\Adapter\Exception; + +use Exception; +use Mockery as m; +use PHPUnit_Framework_TestCase as TestCase; + +class MethodNotSupportedExceptionTest extends TestCase +{ + public function setUp() + { + $this->adapter = m::mock('Graze\Queue\Adapter\AdapterInterface'); + $this->debug = ['foo' => 'bar']; + + $this->messageA = $a = m::mock('Graze\Queue\Message\MessageInterface'); + $this->messageB = $b = m::mock('Graze\Queue\Message\MessageInterface'); + $this->messageC = $c = m::mock('Graze\Queue\Message\MessageInterface'); + $this->messages = [$a, $b, $c]; + + $this->previous = new Exception(); + + $this->exception = new MethodNotSupportedException('method', $this->adapter, $this->messages, $this->debug, $this->previous); + } + + public function testInterface() + { + assertThat($this->exception, is(anInstanceOf('Graze\Queue\Adapter\Exception\AdapterException'))); + } + + public function testGetMethod() + { + assertThat($this->exception->getMethod(), is(identicalTo('method'))); + } + + public function testGetAdapter() + { + assertThat($this->exception->getAdapter(), is(identicalTo($this->adapter))); + } + + public function testGetDebug() + { + assertThat($this->exception->getDebug(), is(identicalTo($this->debug))); + } + + public function testGetMessages() + { + assertThat($this->exception->getMessages(), is(identicalTo($this->messages))); + } + + public function testGetPrevious() + { + assertThat($this->exception->getPrevious(), is(identicalTo($this->previous))); + } +} diff --git a/tests/unit/Adapter/SqsAdapterTest.php b/tests/unit/Adapter/SqsAdapterTest.php index 44dbe86..38e52bc 100644 --- a/tests/unit/Adapter/SqsAdapterTest.php +++ b/tests/unit/Adapter/SqsAdapterTest.php @@ -234,4 +234,16 @@ public function testPurge() assertThat($adapter->purge(), is(nullValue())); } + + public function testDelete() + { + $adapter = new SqsAdapter($this->client, 'foo'); + $url = $this->stubCreateQueue('foo'); + + $this->client->shouldReceive('deleteQueue')->once()->with([ + 'QueueUrl' => $url, + ])->andReturn($this->model); + + assertThat($adapter->delete(), is(nullValue())); + } } diff --git a/tests/unit/ClientTest.php b/tests/unit/ClientTest.php index 6fa1122..4b11775 100644 --- a/tests/unit/ClientTest.php +++ b/tests/unit/ClientTest.php @@ -41,7 +41,9 @@ public function setUp() public function testInterface() { assertThat($this->client, is(anInstanceOf('Graze\Queue\ConsumerInterface'))); + assertThat($this->client, is(anInstanceOf('Graze\Queue\DeleterInterface'))); assertThat($this->client, is(anInstanceOf('Graze\Queue\ProducerInterface'))); + assertThat($this->client, is(anInstanceOf('Graze\Queue\PurgerInterface'))); } public function testCreate() @@ -70,4 +72,16 @@ public function testReceive() $this->client->receive($worker); } + + public function testPurge() + { + $this->adapter->shouldReceive('purge')->once(); + $this->client->purge(); + } + + public function testDelete() + { + $this->adapter->shouldReceive('delete')->once(); + $this->client->delete(); + } }