Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit colors="true" forceCoversAnnotation="true" strict="true">
<phpunit colors="true" forceCoversAnnotation="true">
<testsuite name="Unit Tests">
<directory>./tests</directory>
</testsuite>
Expand Down
51 changes: 26 additions & 25 deletions src/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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);
}
Expand All @@ -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;

Expand Down
41 changes: 40 additions & 1 deletion tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/**
* @coversDefaultClass \DominionEnterprises\Mongo\Queue
* @covers ::<private>
* @uses \DominionEnterprises\Mongo\Queue::__construct
*/
final class QueueTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -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);
}
}