Skip to content

Commit

Permalink
Issue 14 - re-enable queue creation (#15)
Browse files Browse the repository at this point in the history
* PR for #14 - re-enable queue creation

* Correct PHPCS errors
  • Loading branch information
TiS authored and sylfabre committed Nov 19, 2019
1 parent e846894 commit 2a2a726
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/AzureStorageConnectionFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Enqueue\AzureStorage;
Expand Down
54 changes: 53 additions & 1 deletion src/AzureStorageContext.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

declare(strict_types=1);

namespace Enqueue\AzureStorage;

use Exception;
use Interop\Queue\Consumer;
use Interop\Queue\Context;
use Interop\Queue\Destination;
use Interop\Queue\Exception\Exception as InteropException;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\PurgeQueueNotSupportedException;
use Interop\Queue\Exception\SubscriptionConsumerNotSupportedException;
Expand Down Expand Up @@ -46,15 +49,35 @@ public function createMessage(string $body = '', array $properties = [], array $
return $message;
}

/**
* {@inheritDoc}
*
* @throws InteropException Thrown, when topic creation fails.
*/
public function createTopic(string $topicName): Topic
{
$this->client->createQueue($topicName);
try {
$this->client->createQueue($topicName);
} catch (Exception $e) {
$this->handleQueueCreationException($e);
}

return new AzureStorageDestination($topicName);
}

/**
* {@inheritDoc}
*
* @throws InteropException Thrown, when queue creation fails.
*/
public function createQueue(string $queueName): Queue
{
try {
$this->client->createQueue($queueName);
} catch (Exception $e) {
$this->handleQueueCreationException($e);
}

return new AzureStorageDestination($queueName);
}

Expand Down Expand Up @@ -135,4 +158,33 @@ public function purgeQueue(Queue $queue): void
public function close(): void
{
}

/**
* Handle queue creation exception.
*
* @param Exception $e
*
* @throws InteropException
*
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-queue4#remarks
* @see https://docs.microsoft.com/en-us/rest/api/storageservices/queue-service-error-codes
*/
private function handleQueueCreationException(Exception $e): void
{
// If we have 409 response code, it indicates a conflict between new queue and queue already present in Azure.
// This error should be silenced for now.
// @see https://docs.microsoft.com/en-us/rest/api/storageservices/create-queue4#remarks
// TODO: Prepare more elaborate checks for error 409, as sometimes it may be desirable to throw it.

if ($e->getCode() === 409) {
return;
}

// Every other error indicates problems with transport, and, as such, it should be thrown.
// Error is wrapped with common Queue interop Exception class, because throwing generic Exceptions
// is bad for error catching upwards.
// TODO: Create own exception class, for even better error handling possibilities.

throw new InteropException($e->getMessage(), $e->getCode(), $e);
}
}
1 change: 1 addition & 0 deletions src/AzureStorageDestination.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Enqueue\AzureStorage;
Expand Down
1 change: 1 addition & 0 deletions src/AzureStorageMessage.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Enqueue\AzureStorage;
Expand Down
1 change: 1 addition & 0 deletions src/AzureStorageProducer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Enqueue\AzureStorage;
Expand Down
1 change: 1 addition & 0 deletions src/Driver/AzureStorageDriverFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Enqueue\AzureStorage\Driver;
Expand Down
1 change: 0 additions & 1 deletion tests/AzureStorageConsumerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Enqueue\AzureStorage\AzureStorageContext;
use Enqueue\AzureStorage\AzureStorageDestination;
use Enqueue\AzureStorage\AzureStorageMessage;

use Enqueue\Test\ClassExtensionTrait;
use Interop\Queue\Consumer;
use LogicException;
Expand Down
60 changes: 59 additions & 1 deletion tests/AzureStorageContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
use Enqueue\AzureStorage\AzureStorageProducer;
use Enqueue\Test\ClassExtensionTrait;
use Interop\Queue\Context;
use Interop\Queue\Exception;
use Interop\Queue\Exception\PurgeQueueNotSupportedException;
use Interop\Queue\Message;
use Interop\Queue\Exception\InvalidDestinationException;
use Interop\Queue\Exception\TemporaryQueueNotSupportedException;
use Interop\Queue\Exception\SubscriptionConsumerNotSupportedException;
use Interop\Queue\Queue;
use MicrosoftAzure\Storage\Queue\QueueRestProxy;
use PHPUnit\Framework\TestCase;

class AzureStorageContextTest extends \PHPUnit\Framework\TestCase
class AzureStorageContextTest extends TestCase
{
use ClassExtensionTrait;

Expand Down Expand Up @@ -63,6 +65,34 @@ public function testShouldCreateQueue()
$this->assertSame('aQueue', $queue->getQueueName());
}

public function testShouldCreateQueueThrowException()
{
$this->expectException(Exception::class);
$restMock = $this->createQueueRestProxyMock();
$restMock
->expects($this->once())
->method('createQueue')
->willThrowException(new \Exception('Test'));
$context = new AzureStorageContext($restMock);

$context->createQueue('aQueue');
}

public function testShouldCreateQueueSilenceException()
{
$restMock = $this->createQueueRestProxyMock();
$restMock
->expects($this->once())
->method('createQueue')
->willThrowException(new \Exception('Test', 409));
$context = new AzureStorageContext($restMock);

$queue = $context->createQueue('aQueue');

$this->assertInstanceOf(AzureStorageDestination::class, $queue);
$this->assertSame('aQueue', $queue->getQueueName());
}

public function testShouldAllowCreateTopic()
{
$context = new AzureStorageContext($this->createQueueRestProxyMock());
Expand All @@ -73,6 +103,34 @@ public function testShouldAllowCreateTopic()
$this->assertSame('aTopic', $topic->getTopicName());
}

public function testShouldCreateTopicThrowException()
{
$this->expectException(Exception::class);
$restMock = $this->createQueueRestProxyMock();
$restMock
->expects($this->once())
->method('createQueue')
->willThrowException(new \Exception('Test'));
$context = new AzureStorageContext($restMock);

$context->createTopic('aTopic');
}

public function testShouldCreateTopicSilenceException()
{
$restMock = $this->createQueueRestProxyMock();
$restMock
->expects($this->once())
->method('createQueue')
->willThrowException(new \Exception('Test', 409));
$context = new AzureStorageContext($restMock);

$queue = $context->createTopic('aTopic');

$this->assertInstanceOf(AzureStorageDestination::class, $queue);
$this->assertSame('aTopic', $queue->getQueueName());
}

public function testThrowNotImplementedOnCreateTmpQueueCall()
{
$context = new AzureStorageContext($this->createQueueRestProxyMock());
Expand Down
1 change: 1 addition & 0 deletions tests/AzureStorageMessageTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Enqueue\AzureStorage\Tests;
Expand Down
2 changes: 1 addition & 1 deletion tests/AzureStorageProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AzureStorageProducerTest extends TestCase
{
use ClassExtensionTrait;

public function getProducer():AzureStorageProducer
public function getProducer(): AzureStorageProducer
{
return new AzureStorageProducer($this->createQueueRestProxyMock());
}
Expand Down

0 comments on commit 2a2a726

Please sign in to comment.