Skip to content

Commit bf0849b

Browse files
committed
mongodb transport, fixes
1 parent 2260abf commit bf0849b

18 files changed

+106
-55
lines changed

Diff for: JSON.php

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Enqueue\Mongodb;
4+
5+
class JSON
6+
{
7+
/**
8+
* @param string $string
9+
*
10+
* @throws \InvalidArgumentException
11+
*
12+
* @return array
13+
*/
14+
public static function decode($string)
15+
{
16+
if (!is_string($string)) {
17+
throw new \InvalidArgumentException(sprintf(
18+
'Accept only string argument but got: "%s"',
19+
is_object($string) ? get_class($string) : gettype($string)
20+
));
21+
}
22+
23+
// PHP7 fix - empty string and null cause syntax error
24+
if (empty($string)) {
25+
return null;
26+
}
27+
28+
$decoded = json_decode($string, true);
29+
if (JSON_ERROR_NONE !== json_last_error()) {
30+
throw new \InvalidArgumentException(sprintf(
31+
'The malformed json given. Error %s and message %s',
32+
json_last_error(),
33+
json_last_error_msg()
34+
));
35+
}
36+
37+
return $decoded;
38+
}
39+
40+
/**
41+
* @param mixed $value
42+
*
43+
* @return string
44+
*/
45+
public static function encode($value)
46+
{
47+
$encoded = json_encode($value, JSON_UNESCAPED_UNICODE);
48+
49+
if (JSON_ERROR_NONE !== json_last_error()) {
50+
throw new \InvalidArgumentException(sprintf(
51+
'Could not encode value into json. Error %s and message %s',
52+
json_last_error(),
53+
json_last_error_msg()
54+
));
55+
}
56+
57+
return $encoded;
58+
}
59+
}

Diff for: MongodbConnectionFactory.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MongodbConnectionFactory implements PsrConnectionFactory
1616
* The config could be an array, string DSN or null. In case of null it will attempt to connect to Mongodb localhost with default credentials.
1717
*
1818
* $config = [
19-
* 'uri' => 'mongodb://127.0.0.1/' - Mongodb connection string. see http://docs.mongodb.org/manual/reference/connection-string/
19+
* 'dsn' => 'mongodb://127.0.0.1/' - Mongodb connection string. see http://docs.mongodb.org/manual/reference/connection-string/
2020
* 'dbname' => 'enqueue', - database name.
2121
* 'collection_name' => 'enqueue' - collection name
2222
* 'polling_interval' => '1000', - How often query for new messages (milliseconds)
@@ -39,7 +39,7 @@ public function __construct($config = 'mongodb:')
3939
throw new \LogicException('The config must be either an array of options, a DSN string or null');
4040
}
4141
$config = array_replace([
42-
'uri' => 'mongodb://127.0.0.1/',
42+
'dsn' => 'mongodb://127.0.0.1/',
4343
'dbname' => 'enqueue',
4444
'collection_name' => 'enqueue',
4545
], $config);
@@ -49,7 +49,7 @@ public function __construct($config = 'mongodb:')
4949

5050
public function createContext()
5151
{
52-
$client = new Client($this->config['uri']);
52+
$client = new Client($this->config['dsn']);
5353

5454
return new MongodbContext($client, $this->config);
5555
}
@@ -75,10 +75,10 @@ public static function parseDsn($dsn)
7575
}
7676
if ('mongodb:' === $dsn) {
7777
return [
78-
'uri' => 'mongodb://127.0.0.1/',
78+
'dsn' => 'mongodb://127.0.0.1/',
7979
];
8080
}
81-
$config['uri'] = $dsn;
81+
$config['dsn'] = $dsn;
8282
if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) {
8383
$pathParts = explode('/', $parsedUrl['path']);
8484
//DB name

Diff for: MongodbConsumer.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ protected function receiveMessage()
163163
*/
164164
protected function convertMessage(array $mongodbMessage)
165165
{
166-
$message = $this->context->createMessage($mongodbMessage['body'], $mongodbMessage['properties'], $mongodbMessage['headers']);
166+
$properties = JSON::decode($mongodbMessage['properties']);
167+
$headers = JSON::decode($mongodbMessage['headers']);
168+
169+
$message = $this->context->createMessage($mongodbMessage['body'], $properties, $headers);
167170
$message->setId((string) $mongodbMessage['_id']);
168171
$message->setPriority((int) $mongodbMessage['priority']);
169172
$message->setRedelivered((bool) $mongodbMessage['redelivered']);

Diff for: MongodbProducer.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function send(PsrDestination $destination, PsrMessage $message)
8080
$mongoMessage = [
8181
'published_at' => $publishedAt,
8282
'body' => $body,
83-
'headers' => $message->getHeaders(),
84-
'properties' => $message->getProperties(),
83+
'headers' => JSON::encode($message->getHeaders()),
84+
'properties' => JSON::encode($message->getProperties()),
8585
'priority' => $message->getPriority(),
8686
'queue' => $destination->getName(),
8787
'redelivered' => $message->isRedelivered(),

Diff for: Tests/Functional/MongodbConsumerTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use Enqueue\Mongodb\MongodbContext;
66
use Enqueue\Mongodb\MongodbMessage;
7-
use Enqueue\Mongodb\Tests\Spec\CreateMongodbContextTrait;
7+
use Enqueue\Test\MongodbExtensionTrait;
88
use PHPUnit\Framework\TestCase;
99

1010
/**
1111
* @group functional
1212
*/
1313
class MongodbConsumerTest extends TestCase
1414
{
15-
use CreateMongodbContextTrait;
15+
use MongodbExtensionTrait;
1616

1717
/**
1818
* @var MongodbContext
@@ -21,7 +21,7 @@ class MongodbConsumerTest extends TestCase
2121

2222
public function setUp()
2323
{
24-
$this->context = $this->createMongodbContext();
24+
$this->context = $this->buildMongodbContext();
2525
}
2626

2727
protected function tearDown()

Diff for: Tests/MongodbConnectionFactoryTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testShouldImplementConnectionFactoryInterface()
2222
public function testCouldBeConstructedWithEmptyConfiguration()
2323
{
2424
$params = [
25-
'uri' => 'mongodb://127.0.0.1/',
25+
'dsn' => 'mongodb://127.0.0.1/',
2626
'dbname' => 'enqueue',
2727
'collection_name' => 'enqueue',
2828
];
@@ -34,7 +34,7 @@ public function testCouldBeConstructedWithEmptyConfiguration()
3434
public function testCouldBeConstructedWithCustomConfiguration()
3535
{
3636
$params = [
37-
'uri' => 'mongodb://127.0.0.3/',
37+
'dsn' => 'mongodb://127.0.0.3/',
3838
'uriOptions' => ['testValue' => 123],
3939
'driverOptions' => ['testValue' => 123],
4040
'dbname' => 'enqueue',

Diff for: Tests/Spec/CreateMongodbContextTrait.php

-21
This file was deleted.

Diff for: Tests/Spec/MongodbContextTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Mongodb\Tests\Spec;
44

5+
use Enqueue\Test\MongodbExtensionTrait;
56
use Interop\Queue\Spec\PsrContextSpec;
67

78
/**
@@ -10,13 +11,13 @@
1011
*/
1112
class MongodbContextTest extends PsrContextSpec
1213
{
13-
use CreateMongodbContextTrait;
14+
use MongodbExtensionTrait;
1415

1516
/**
1617
* {@inheritdoc}
1718
*/
1819
protected function createContext()
1920
{
20-
return $this->createMongodbContext();
21+
return $this->buildMongodbContext();
2122
}
2223
}

Diff for: Tests/Spec/MongodbProducerTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Mongodb\Tests\Spec;
44

5+
use Enqueue\Test\MongodbExtensionTrait;
56
use Interop\Queue\Spec\PsrProducerSpec;
67

78
/**
@@ -10,13 +11,13 @@
1011
*/
1112
class MongodbProducerTest extends PsrProducerSpec
1213
{
13-
use CreateMongodbContextTrait;
14+
use MongodbExtensionTrait;
1415

1516
/**
1617
* {@inheritdoc}
1718
*/
1819
protected function createProducer()
1920
{
20-
return $this->createMongodbContext()->createProducer();
21+
return $this->buildMongodbContext()->createProducer();
2122
}
2223
}

Diff for: Tests/Spec/MongodbRequeueMessageTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Mongodb\Tests\Spec;
44

5+
use Enqueue\Test\MongodbExtensionTrait;
56
use Interop\Queue\Spec\RequeueMessageSpec;
67

78
/**
@@ -10,13 +11,13 @@
1011
*/
1112
class MongodbRequeueMessageTest extends RequeueMessageSpec
1213
{
13-
use CreateMongodbContextTrait;
14+
use MongodbExtensionTrait;
1415

1516
/**
1617
* {@inheritdoc}
1718
*/
1819
protected function createContext()
1920
{
20-
return $this->createMongodbContext();
21+
return $this->buildMongodbContext();
2122
}
2223
}

Diff for: Tests/Spec/MongodbSendAndReceiveDelayedMessageFromQueueTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Mongodb\Tests\Spec;
44

5+
use Enqueue\Test\MongodbExtensionTrait;
56
use Interop\Queue\Spec\SendAndReceiveDelayedMessageFromQueueSpec;
67

78
/**
@@ -10,13 +11,13 @@
1011
*/
1112
class MongodbSendAndReceiveDelayedMessageFromQueueTest extends SendAndReceiveDelayedMessageFromQueueSpec
1213
{
13-
use CreateMongodbContextTrait;
14+
use MongodbExtensionTrait;
1415

1516
/**
1617
* {@inheritdoc}
1718
*/
1819
protected function createContext()
1920
{
20-
return $this->createMongodbContext();
21+
return $this->buildMongodbContext();
2122
}
2223
}

Diff for: Tests/Spec/MongodbSendAndReceivePriorityMessagesFromQueueTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Enqueue\Mongodb\MongodbContext;
66
use Enqueue\Mongodb\MongodbMessage;
7+
use Enqueue\Test\MongodbExtensionTrait;
78
use Interop\Queue\PsrContext;
89
use Interop\Queue\Spec\SendAndReceivePriorityMessagesFromQueueSpec;
910

@@ -13,7 +14,7 @@
1314
*/
1415
class MongodbSendAndReceivePriorityMessagesFromQueueTest extends SendAndReceivePriorityMessagesFromQueueSpec
1516
{
16-
use CreateMongodbContextTrait;
17+
use MongodbExtensionTrait;
1718

1819
private $publishedAt;
1920

@@ -29,7 +30,7 @@ public function setUp()
2930
*/
3031
protected function createContext()
3132
{
32-
return $this->createMongodbContext();
33+
return $this->buildMongodbContext();
3334
}
3435

3536
/**

Diff for: Tests/Spec/MongodbSendAndReceiveTimeToLiveMessagesFromQueueTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Mongodb\Tests\Spec;
44

5+
use Enqueue\Test\MongodbExtensionTrait;
56
use Interop\Queue\Spec\SendAndReceiveTimeToLiveMessagesFromQueueSpec;
67

78
/**
@@ -10,13 +11,13 @@
1011
*/
1112
class MongodbSendAndReceiveTimeToLiveMessagesFromQueueTest extends SendAndReceiveTimeToLiveMessagesFromQueueSpec
1213
{
13-
use CreateMongodbContextTrait;
14+
use MongodbExtensionTrait;
1415

1516
/**
1617
* {@inheritdoc}
1718
*/
1819
protected function createContext()
1920
{
20-
return $this->createMongodbContext();
21+
return $this->buildMongodbContext();
2122
}
2223
}

Diff for: Tests/Spec/MongodbSendToAndReceiveFromQueueTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Mongodb\Tests\Spec;
44

5+
use Enqueue\Test\MongodbExtensionTrait;
56
use Interop\Queue\Spec\SendToAndReceiveFromQueueSpec;
67

78
/**
@@ -10,13 +11,13 @@
1011
*/
1112
class MongodbSendToAndReceiveFromQueueTest extends SendToAndReceiveFromQueueSpec
1213
{
13-
use CreateMongodbContextTrait;
14+
use MongodbExtensionTrait;
1415

1516
/**
1617
* {@inheritdoc}
1718
*/
1819
protected function createContext()
1920
{
20-
return $this->createMongodbContext();
21+
return $this->buildMongodbContext();
2122
}
2223
}

Diff for: Tests/Spec/MongodbSendToAndReceiveFromTopicTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Mongodb\Tests\Spec;
44

5+
use Enqueue\Test\MongodbExtensionTrait;
56
use Interop\Queue\Spec\SendToAndReceiveFromTopicSpec;
67

78
/**
@@ -10,13 +11,13 @@
1011
*/
1112
class MongodbSendToAndReceiveFromTopicTest extends SendToAndReceiveFromTopicSpec
1213
{
13-
use CreateMongodbContextTrait;
14+
use MongodbExtensionTrait;
1415

1516
/**
1617
* {@inheritdoc}
1718
*/
1819
protected function createContext()
1920
{
20-
return $this->createMongodbContext();
21+
return $this->buildMongodbContext();
2122
}
2223
}

0 commit comments

Comments
 (0)