From bf0849bb2e9ea3f69dfb8ce37dddf1ad2d2de5ec Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Thu, 3 May 2018 22:59:49 +0300 Subject: [PATCH] mongodb transport, fixes --- JSON.php | 59 +++++++++++++++++++ MongodbConnectionFactory.php | 10 ++-- MongodbConsumer.php | 5 +- MongodbProducer.php | 4 +- Tests/Functional/MongodbConsumerTest.php | 6 +- Tests/MongodbConnectionFactoryTest.php | 4 +- Tests/Spec/CreateMongodbContextTrait.php | 21 ------- Tests/Spec/MongodbContextTest.php | 5 +- Tests/Spec/MongodbProducerTest.php | 5 +- Tests/Spec/MongodbRequeueMessageTest.php | 5 +- ...dAndReceiveDelayedMessageFromQueueTest.php | 5 +- ...ndReceivePriorityMessagesFromQueueTest.php | 5 +- ...ReceiveTimeToLiveMessagesFromQueueTest.php | 5 +- .../MongodbSendToAndReceiveFromQueueTest.php | 5 +- .../MongodbSendToAndReceiveFromTopicTest.php | 5 +- ...odbSendToAndReceiveNoWaitFromQueueTest.php | 5 +- ...odbSendToAndReceiveNoWaitFromTopicTest.php | 5 +- composer.json | 2 +- 18 files changed, 106 insertions(+), 55 deletions(-) create mode 100644 JSON.php delete mode 100644 Tests/Spec/CreateMongodbContextTrait.php diff --git a/JSON.php b/JSON.php new file mode 100644 index 0000000..84cac50 --- /dev/null +++ b/JSON.php @@ -0,0 +1,59 @@ + 'mongodb://127.0.0.1/' - Mongodb connection string. see http://docs.mongodb.org/manual/reference/connection-string/ + * 'dsn' => 'mongodb://127.0.0.1/' - Mongodb connection string. see http://docs.mongodb.org/manual/reference/connection-string/ * 'dbname' => 'enqueue', - database name. * 'collection_name' => 'enqueue' - collection name * 'polling_interval' => '1000', - How often query for new messages (milliseconds) @@ -39,7 +39,7 @@ public function __construct($config = 'mongodb:') throw new \LogicException('The config must be either an array of options, a DSN string or null'); } $config = array_replace([ - 'uri' => 'mongodb://127.0.0.1/', + 'dsn' => 'mongodb://127.0.0.1/', 'dbname' => 'enqueue', 'collection_name' => 'enqueue', ], $config); @@ -49,7 +49,7 @@ public function __construct($config = 'mongodb:') public function createContext() { - $client = new Client($this->config['uri']); + $client = new Client($this->config['dsn']); return new MongodbContext($client, $this->config); } @@ -75,10 +75,10 @@ public static function parseDsn($dsn) } if ('mongodb:' === $dsn) { return [ - 'uri' => 'mongodb://127.0.0.1/', + 'dsn' => 'mongodb://127.0.0.1/', ]; } - $config['uri'] = $dsn; + $config['dsn'] = $dsn; if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) { $pathParts = explode('/', $parsedUrl['path']); //DB name diff --git a/MongodbConsumer.php b/MongodbConsumer.php index 4986d66..6fe3428 100644 --- a/MongodbConsumer.php +++ b/MongodbConsumer.php @@ -163,7 +163,10 @@ protected function receiveMessage() */ protected function convertMessage(array $mongodbMessage) { - $message = $this->context->createMessage($mongodbMessage['body'], $mongodbMessage['properties'], $mongodbMessage['headers']); + $properties = JSON::decode($mongodbMessage['properties']); + $headers = JSON::decode($mongodbMessage['headers']); + + $message = $this->context->createMessage($mongodbMessage['body'], $properties, $headers); $message->setId((string) $mongodbMessage['_id']); $message->setPriority((int) $mongodbMessage['priority']); $message->setRedelivered((bool) $mongodbMessage['redelivered']); diff --git a/MongodbProducer.php b/MongodbProducer.php index 2a06791..c5132b6 100644 --- a/MongodbProducer.php +++ b/MongodbProducer.php @@ -80,8 +80,8 @@ public function send(PsrDestination $destination, PsrMessage $message) $mongoMessage = [ 'published_at' => $publishedAt, 'body' => $body, - 'headers' => $message->getHeaders(), - 'properties' => $message->getProperties(), + 'headers' => JSON::encode($message->getHeaders()), + 'properties' => JSON::encode($message->getProperties()), 'priority' => $message->getPriority(), 'queue' => $destination->getName(), 'redelivered' => $message->isRedelivered(), diff --git a/Tests/Functional/MongodbConsumerTest.php b/Tests/Functional/MongodbConsumerTest.php index eddf213..609a22e 100644 --- a/Tests/Functional/MongodbConsumerTest.php +++ b/Tests/Functional/MongodbConsumerTest.php @@ -4,7 +4,7 @@ use Enqueue\Mongodb\MongodbContext; use Enqueue\Mongodb\MongodbMessage; -use Enqueue\Mongodb\Tests\Spec\CreateMongodbContextTrait; +use Enqueue\Test\MongodbExtensionTrait; use PHPUnit\Framework\TestCase; /** @@ -12,7 +12,7 @@ */ class MongodbConsumerTest extends TestCase { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * @var MongodbContext @@ -21,7 +21,7 @@ class MongodbConsumerTest extends TestCase public function setUp() { - $this->context = $this->createMongodbContext(); + $this->context = $this->buildMongodbContext(); } protected function tearDown() diff --git a/Tests/MongodbConnectionFactoryTest.php b/Tests/MongodbConnectionFactoryTest.php index b46f856..daaef51 100644 --- a/Tests/MongodbConnectionFactoryTest.php +++ b/Tests/MongodbConnectionFactoryTest.php @@ -22,7 +22,7 @@ public function testShouldImplementConnectionFactoryInterface() public function testCouldBeConstructedWithEmptyConfiguration() { $params = [ - 'uri' => 'mongodb://127.0.0.1/', + 'dsn' => 'mongodb://127.0.0.1/', 'dbname' => 'enqueue', 'collection_name' => 'enqueue', ]; @@ -34,7 +34,7 @@ public function testCouldBeConstructedWithEmptyConfiguration() public function testCouldBeConstructedWithCustomConfiguration() { $params = [ - 'uri' => 'mongodb://127.0.0.3/', + 'dsn' => 'mongodb://127.0.0.3/', 'uriOptions' => ['testValue' => 123], 'driverOptions' => ['testValue' => 123], 'dbname' => 'enqueue', diff --git a/Tests/Spec/CreateMongodbContextTrait.php b/Tests/Spec/CreateMongodbContextTrait.php deleted file mode 100644 index 703ea7f..0000000 --- a/Tests/Spec/CreateMongodbContextTrait.php +++ /dev/null @@ -1,21 +0,0 @@ -markTestSkipped('The MONGO_DSN env is not available. Skip tests'); - } - - $factory = new MongodbConnectionFactory(['uri' => $env]); - - $context = $factory->createContext(); - - return $context; - } -} diff --git a/Tests/Spec/MongodbContextTest.php b/Tests/Spec/MongodbContextTest.php index a53efa0..51d4c4b 100644 --- a/Tests/Spec/MongodbContextTest.php +++ b/Tests/Spec/MongodbContextTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Mongodb\Tests\Spec; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\Spec\PsrContextSpec; /** @@ -10,13 +11,13 @@ */ class MongodbContextTest extends PsrContextSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * {@inheritdoc} */ protected function createContext() { - return $this->createMongodbContext(); + return $this->buildMongodbContext(); } } diff --git a/Tests/Spec/MongodbProducerTest.php b/Tests/Spec/MongodbProducerTest.php index 4f6dc85..54eb096 100644 --- a/Tests/Spec/MongodbProducerTest.php +++ b/Tests/Spec/MongodbProducerTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Mongodb\Tests\Spec; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\Spec\PsrProducerSpec; /** @@ -10,13 +11,13 @@ */ class MongodbProducerTest extends PsrProducerSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * {@inheritdoc} */ protected function createProducer() { - return $this->createMongodbContext()->createProducer(); + return $this->buildMongodbContext()->createProducer(); } } diff --git a/Tests/Spec/MongodbRequeueMessageTest.php b/Tests/Spec/MongodbRequeueMessageTest.php index fa5832b..454d357 100644 --- a/Tests/Spec/MongodbRequeueMessageTest.php +++ b/Tests/Spec/MongodbRequeueMessageTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Mongodb\Tests\Spec; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\Spec\RequeueMessageSpec; /** @@ -10,13 +11,13 @@ */ class MongodbRequeueMessageTest extends RequeueMessageSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * {@inheritdoc} */ protected function createContext() { - return $this->createMongodbContext(); + return $this->buildMongodbContext(); } } diff --git a/Tests/Spec/MongodbSendAndReceiveDelayedMessageFromQueueTest.php b/Tests/Spec/MongodbSendAndReceiveDelayedMessageFromQueueTest.php index aa7ebf4..a5eb351 100644 --- a/Tests/Spec/MongodbSendAndReceiveDelayedMessageFromQueueTest.php +++ b/Tests/Spec/MongodbSendAndReceiveDelayedMessageFromQueueTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Mongodb\Tests\Spec; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\Spec\SendAndReceiveDelayedMessageFromQueueSpec; /** @@ -10,13 +11,13 @@ */ class MongodbSendAndReceiveDelayedMessageFromQueueTest extends SendAndReceiveDelayedMessageFromQueueSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * {@inheritdoc} */ protected function createContext() { - return $this->createMongodbContext(); + return $this->buildMongodbContext(); } } diff --git a/Tests/Spec/MongodbSendAndReceivePriorityMessagesFromQueueTest.php b/Tests/Spec/MongodbSendAndReceivePriorityMessagesFromQueueTest.php index d105bc9..5400820 100644 --- a/Tests/Spec/MongodbSendAndReceivePriorityMessagesFromQueueTest.php +++ b/Tests/Spec/MongodbSendAndReceivePriorityMessagesFromQueueTest.php @@ -4,6 +4,7 @@ use Enqueue\Mongodb\MongodbContext; use Enqueue\Mongodb\MongodbMessage; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\PsrContext; use Interop\Queue\Spec\SendAndReceivePriorityMessagesFromQueueSpec; @@ -13,7 +14,7 @@ */ class MongodbSendAndReceivePriorityMessagesFromQueueTest extends SendAndReceivePriorityMessagesFromQueueSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; private $publishedAt; @@ -29,7 +30,7 @@ public function setUp() */ protected function createContext() { - return $this->createMongodbContext(); + return $this->buildMongodbContext(); } /** diff --git a/Tests/Spec/MongodbSendAndReceiveTimeToLiveMessagesFromQueueTest.php b/Tests/Spec/MongodbSendAndReceiveTimeToLiveMessagesFromQueueTest.php index 5f591eb..d87ac10 100644 --- a/Tests/Spec/MongodbSendAndReceiveTimeToLiveMessagesFromQueueTest.php +++ b/Tests/Spec/MongodbSendAndReceiveTimeToLiveMessagesFromQueueTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Mongodb\Tests\Spec; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\Spec\SendAndReceiveTimeToLiveMessagesFromQueueSpec; /** @@ -10,13 +11,13 @@ */ class MongodbSendAndReceiveTimeToLiveMessagesFromQueueTest extends SendAndReceiveTimeToLiveMessagesFromQueueSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * {@inheritdoc} */ protected function createContext() { - return $this->createMongodbContext(); + return $this->buildMongodbContext(); } } diff --git a/Tests/Spec/MongodbSendToAndReceiveFromQueueTest.php b/Tests/Spec/MongodbSendToAndReceiveFromQueueTest.php index cb420e3..992c062 100644 --- a/Tests/Spec/MongodbSendToAndReceiveFromQueueTest.php +++ b/Tests/Spec/MongodbSendToAndReceiveFromQueueTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Mongodb\Tests\Spec; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\Spec\SendToAndReceiveFromQueueSpec; /** @@ -10,13 +11,13 @@ */ class MongodbSendToAndReceiveFromQueueTest extends SendToAndReceiveFromQueueSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * {@inheritdoc} */ protected function createContext() { - return $this->createMongodbContext(); + return $this->buildMongodbContext(); } } diff --git a/Tests/Spec/MongodbSendToAndReceiveFromTopicTest.php b/Tests/Spec/MongodbSendToAndReceiveFromTopicTest.php index 1078291..c539386 100644 --- a/Tests/Spec/MongodbSendToAndReceiveFromTopicTest.php +++ b/Tests/Spec/MongodbSendToAndReceiveFromTopicTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Mongodb\Tests\Spec; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\Spec\SendToAndReceiveFromTopicSpec; /** @@ -10,13 +11,13 @@ */ class MongodbSendToAndReceiveFromTopicTest extends SendToAndReceiveFromTopicSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * {@inheritdoc} */ protected function createContext() { - return $this->createMongodbContext(); + return $this->buildMongodbContext(); } } diff --git a/Tests/Spec/MongodbSendToAndReceiveNoWaitFromQueueTest.php b/Tests/Spec/MongodbSendToAndReceiveNoWaitFromQueueTest.php index 596d177..ea4febc 100644 --- a/Tests/Spec/MongodbSendToAndReceiveNoWaitFromQueueTest.php +++ b/Tests/Spec/MongodbSendToAndReceiveNoWaitFromQueueTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Mongodb\Tests\Spec; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\Spec\SendToAndReceiveNoWaitFromQueueSpec; /** @@ -10,13 +11,13 @@ */ class MongodbSendToAndReceiveNoWaitFromQueueTest extends SendToAndReceiveNoWaitFromQueueSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * {@inheritdoc} */ protected function createContext() { - return $this->createMongodbContext(); + return $this->buildMongodbContext(); } } diff --git a/Tests/Spec/MongodbSendToAndReceiveNoWaitFromTopicTest.php b/Tests/Spec/MongodbSendToAndReceiveNoWaitFromTopicTest.php index c8ca6d1..1e1be32 100644 --- a/Tests/Spec/MongodbSendToAndReceiveNoWaitFromTopicTest.php +++ b/Tests/Spec/MongodbSendToAndReceiveNoWaitFromTopicTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Mongodb\Tests\Spec; +use Enqueue\Test\MongodbExtensionTrait; use Interop\Queue\Spec\SendToAndReceiveNoWaitFromTopicSpec; /** @@ -10,13 +11,13 @@ */ class MongodbSendToAndReceiveNoWaitFromTopicTest extends SendToAndReceiveNoWaitFromTopicSpec { - use CreateMongodbContextTrait; + use MongodbExtensionTrait; /** * {@inheritdoc} */ protected function createContext() { - return $this->createMongodbContext(); + return $this->buildMongodbContext(); } } diff --git a/composer.json b/composer.json index e4a08da..59a2b6c 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "require-dev": { "phpunit/phpunit": "~5.4.0", "queue-interop/queue-spec": "^0.5.5@dev", - "enqueue/test": "^0.8@dev", + "enqueue/test": "^0.8.25@dev", "enqueue/enqueue": "^0.8@dev", "enqueue/null": "^0.8@dev" },