Skip to content

Commit 8e634b2

Browse files
committed
Merge pull request #40 from chadicus/master
Construct Queue with MongoCollection
2 parents 423e28a + 852e093 commit 8e634b2

File tree

3 files changed

+67
-27
lines changed

3 files changed

+67
-27
lines changed

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<phpunit colors="true" forceCoversAnnotation="true" strict="true">
1+
<phpunit colors="true" forceCoversAnnotation="true">
22
<testsuite name="Unit Tests">
33
<directory>./tests</directory>
44
</testsuite>

src/Queue.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@ final class Queue implements QueueInterface
2525
/**
2626
* Construct queue.
2727
*
28-
* @param string $url the mongo url
28+
* @param \MongoCollection|string $collectionOrUrl A MongoCollection instance or the mongo connection url.
2929
* @param string $db the mongo db name
3030
* @param string $collection the collection name to use for the queue
3131
*
32-
* @throws \InvalidArgumentException $url, $db or $collection was not a string
32+
* @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string
3333
*/
34-
public function __construct($url, $db, $collection)
34+
public function __construct($collectionOrUrl, $db = null, $collection = null)
3535
{
36-
if (!is_string($url)) {
37-
throw new \InvalidArgumentException('$url was not a string');
36+
if ($collectionOrUrl instanceof \MongoCollection) {
37+
$this->collection = $collectionOrUrl;
38+
return;
39+
}
40+
41+
if (!is_string($collectionOrUrl)) {
42+
throw new \InvalidArgumentException('$collectionOrUrl was not a string');
3843
}
3944

4045
if (!is_string($db)) {
@@ -45,7 +50,7 @@ public function __construct($url, $db, $collection)
4550
throw new \InvalidArgumentException('$collection was not a string');
4651
}
4752

48-
$mongo = new \MongoClient($url);
53+
$mongo = new \MongoClient($collectionOrUrl);
4954
$mongoDb = $mongo->selectDB($db);
5055
$this->collection = $mongoDb->selectCollection($collection);
5156
}
@@ -68,32 +73,28 @@ public function ensureGetIndex(array $beforeSort = [], array $afterSort = [])
6873
//using general rule: equality, sort, range or more equality tests in that order for index
6974
$completeFields = ['running' => 1];
7075

71-
foreach ($beforeSort as $key => $value) {
72-
if (!is_string($key)) {
73-
throw new \InvalidArgumentException('key in $beforeSort was not a string');
74-
}
76+
$verifySort = function ($sort, $label, &$completeFields) {
77+
foreach ($sort as $key => $value) {
78+
if (!is_string($key)) {
79+
throw new \InvalidArgumentException("key in \${$label} was not a string");
80+
}
7581

76-
if ($value !== 1 && $value !== -1) {
77-
throw new \InvalidArgumentException('value of $beforeSort is not 1 or -1 for ascending and descending');
82+
if ($value !== 1 && $value !== -1) {
83+
throw new \InvalidArgumentException(
84+
'value of \${$label} is not 1 or -1 for ascending and descending'
85+
);
86+
}
87+
88+
$completeFields["payload.{$key}"] = $value;
7889
}
90+
};
7991

80-
$completeFields["payload.{$key}"] = $value;
81-
}
92+
$verifySort($beforeSort, 'beforeSort', $completeFields);
8293

8394
$completeFields['priority'] = 1;
8495
$completeFields['created'] = 1;
8596

86-
foreach ($afterSort as $key => $value) {
87-
if (!is_string($key)) {
88-
throw new \InvalidArgumentException('key in $afterSort was not a string');
89-
}
90-
91-
if ($value !== 1 && $value !== -1) {
92-
throw new \InvalidArgumentException('value of $afterSort is not 1 or -1 for ascending and descending');
93-
}
94-
95-
$completeFields["payload.{$key}"] = $value;
96-
}
97+
$verifySort($afterSort, 'afterSort', $completeFields);
9798

9899
$completeFields['earliestGet'] = 1;
99100

tests/QueueTest.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
/**
66
* @coversDefaultClass \DominionEnterprises\Mongo\Queue
77
* @covers ::<private>
8-
* @uses \DominionEnterprises\Mongo\Queue::__construct
98
*/
109
final class QueueTest extends \PHPUnit_Framework_TestCase
1110
{
@@ -853,4 +852,44 @@ public function sendWithLowEarliestGet()
853852

854853
$this->assertSame($expected, $message);
855854
}
855+
856+
/**
857+
* Verify Queue can be constructed with \MongoCollection
858+
*
859+
* @test
860+
* @covers ::__construct
861+
*
862+
* @return void
863+
*/
864+
public function constructWithCollection()
865+
{
866+
$mongo = new \MongoClient($this->mongoUrl);
867+
$collection = $mongo->selectDB('testing')->selectCollection('custom_collection');
868+
$collection->drop();
869+
$queue = new Queue($collection);
870+
871+
$payload = ['key1' => 0, 'key2' => true];
872+
$queue->send($payload, 34, 0.8);
873+
874+
$expected = [
875+
'payload' => $payload,
876+
'running' => false,
877+
'resetTimestamp' => Queue::MONGO_INT32_MAX,
878+
'earliestGet' => 34,
879+
'priority' => 0.8,
880+
];
881+
882+
$this->assertSame(1, $collection->count());
883+
884+
$message = $collection->findOne();
885+
886+
$this->assertLessThanOrEqual(time(), $message['created']->sec);
887+
$this->assertGreaterThan(time() - 10, $message['created']->sec);
888+
889+
unset($message['_id'], $message['created']);
890+
$message['resetTimestamp'] = $message['resetTimestamp']->sec;
891+
$message['earliestGet'] = $message['earliestGet']->sec;
892+
893+
$this->assertSame($expected, $message);
894+
}
856895
}

0 commit comments

Comments
 (0)