Skip to content

Commit 988ab4a

Browse files
committed
Allow Queue to be constructed with MongoCollection
1 parent cbe626a commit 988ab4a

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/Queue.php

Lines changed: 11 additions & 6 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
}

tests/QueueTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,4 +852,44 @@ public function sendWithLowEarliestGet()
852852

853853
$this->assertSame($expected, $message);
854854
}
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+
}
855895
}

0 commit comments

Comments
 (0)