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
12 changes: 9 additions & 3 deletions src/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class ArrayAdapter implements AdapterInterface
/**
* @param MessageInterface[]
*/
protected $queue;
protected $queue = [];

/**
* @param MessageInterface[] $messages
Expand All @@ -49,9 +49,15 @@ public function acknowledge(array $messages)
*/
public function dequeue(MessageFactoryInterface $factory, $limit)
{
$total = null === $limit ? count($this->queue) : $limit;
/**
* If {@see $limit} is null then {@see LimitIterator} should be passed -1 as the count
* to avoid throwing OutOfBoundsException.
*
* @link https://github.com/php/php-src/blob/php-5.6.12/ext/spl/internal/limititerator.inc#L60-L62
*/
$count = (null === $limit) ? -1 : $limit;

return new LimitIterator(new ArrayIterator($this->queue), 0, $total);
return new LimitIterator(new ArrayIterator($this->queue), 0, $count);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/ArrayIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ public function testReceiveWithPolling()
assertThat($msgs, is(identicalTo($this->messages)));
}

public function testReceiveWithNoMessages()
{
$client = new Client(new ArrayAdapter());

$msgs = [];
$client->receive(function ($msg) use (&$msgs) {
$msgs[] = $msg;
}, null);

assertThat($msgs, is(emptyArray()));
}

public function testReceiveWithLimitAndNoMessages()
{
$client = new Client(new ArrayAdapter());

$msgs = [];
$client->receive(function ($msg) use (&$msgs) {
$msgs[] = $msg;
}, 10);

assertThat($msgs, is(emptyArray()));
}

public function testSend()
{
$this->client->send([$this->client->create('foo')]);
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Adapter/ArrayAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ public function testDequeueWithPollingLimit()
assertThat(iterator_to_array($iterator), is(identicalTo($this->messages)));
}

public function testDequeueWithNoMessages()
{
$adapter = new ArrayAdapter();

$iterator = $adapter->dequeue($this->factory, null);

assertThat(iterator_to_array($iterator), is(emptyArray()));
}

public function testDequeueWithLimitAndNoMessages()
{
$adapter = new ArrayAdapter();

$iterator = $adapter->dequeue($this->factory, 10);

assertThat(iterator_to_array($iterator), is(emptyArray()));
}

public function testEnqueue()
{
$messageA = m::mock('Graze\Queue\Message\MessageInterface');
Expand Down