diff --git a/phpunit.xml b/phpunit.xml index dc845da..ba76cec 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,4 +1,4 @@ - + ./tests diff --git a/src/Queue.php b/src/Queue.php index 8279b34..a829cf9 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -25,16 +25,21 @@ final class Queue implements QueueInterface /** * Construct queue. * - * @param string $url the mongo url + * @param \MongoCollection|string $collectionOrUrl A MongoCollection instance or the mongo connection url. * @param string $db the mongo db name * @param string $collection the collection name to use for the queue * - * @throws \InvalidArgumentException $url, $db or $collection was not a string + * @throws \InvalidArgumentException $collectionOrUrl, $db or $collection was not a string */ - public function __construct($url, $db, $collection) + public function __construct($collectionOrUrl, $db = null, $collection = null) { - if (!is_string($url)) { - throw new \InvalidArgumentException('$url was not a string'); + if ($collectionOrUrl instanceof \MongoCollection) { + $this->collection = $collectionOrUrl; + return; + } + + if (!is_string($collectionOrUrl)) { + throw new \InvalidArgumentException('$collectionOrUrl was not a string'); } if (!is_string($db)) { @@ -45,7 +50,7 @@ public function __construct($url, $db, $collection) throw new \InvalidArgumentException('$collection was not a string'); } - $mongo = new \MongoClient($url); + $mongo = new \MongoClient($collectionOrUrl); $mongoDb = $mongo->selectDB($db); $this->collection = $mongoDb->selectCollection($collection); } @@ -68,32 +73,28 @@ public function ensureGetIndex(array $beforeSort = [], array $afterSort = []) //using general rule: equality, sort, range or more equality tests in that order for index $completeFields = ['running' => 1]; - foreach ($beforeSort as $key => $value) { - if (!is_string($key)) { - throw new \InvalidArgumentException('key in $beforeSort was not a string'); - } + $verifySort = function ($sort, $label, &$completeFields) { + foreach ($sort as $key => $value) { + if (!is_string($key)) { + throw new \InvalidArgumentException("key in \${$label} was not a string"); + } - if ($value !== 1 && $value !== -1) { - throw new \InvalidArgumentException('value of $beforeSort is not 1 or -1 for ascending and descending'); + if ($value !== 1 && $value !== -1) { + throw new \InvalidArgumentException( + 'value of \${$label} is not 1 or -1 for ascending and descending' + ); + } + + $completeFields["payload.{$key}"] = $value; } + }; - $completeFields["payload.{$key}"] = $value; - } + $verifySort($beforeSort, 'beforeSort', $completeFields); $completeFields['priority'] = 1; $completeFields['created'] = 1; - foreach ($afterSort as $key => $value) { - if (!is_string($key)) { - throw new \InvalidArgumentException('key in $afterSort was not a string'); - } - - if ($value !== 1 && $value !== -1) { - throw new \InvalidArgumentException('value of $afterSort is not 1 or -1 for ascending and descending'); - } - - $completeFields["payload.{$key}"] = $value; - } + $verifySort($afterSort, 'afterSort', $completeFields); $completeFields['earliestGet'] = 1; diff --git a/tests/QueueTest.php b/tests/QueueTest.php index e6aa8e0..ec30a00 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -5,7 +5,6 @@ /** * @coversDefaultClass \DominionEnterprises\Mongo\Queue * @covers :: - * @uses \DominionEnterprises\Mongo\Queue::__construct */ final class QueueTest extends \PHPUnit_Framework_TestCase { @@ -853,4 +852,44 @@ public function sendWithLowEarliestGet() $this->assertSame($expected, $message); } + + /** + * Verify Queue can be constructed with \MongoCollection + * + * @test + * @covers ::__construct + * + * @return void + */ + public function constructWithCollection() + { + $mongo = new \MongoClient($this->mongoUrl); + $collection = $mongo->selectDB('testing')->selectCollection('custom_collection'); + $collection->drop(); + $queue = new Queue($collection); + + $payload = ['key1' => 0, 'key2' => true]; + $queue->send($payload, 34, 0.8); + + $expected = [ + 'payload' => $payload, + 'running' => false, + 'resetTimestamp' => Queue::MONGO_INT32_MAX, + 'earliestGet' => 34, + 'priority' => 0.8, + ]; + + $this->assertSame(1, $collection->count()); + + $message = $collection->findOne(); + + $this->assertLessThanOrEqual(time(), $message['created']->sec); + $this->assertGreaterThan(time() - 10, $message['created']->sec); + + unset($message['_id'], $message['created']); + $message['resetTimestamp'] = $message['resetTimestamp']->sec; + $message['earliestGet'] = $message['earliestGet']->sec; + + $this->assertSame($expected, $message); + } }