Skip to content

Commit

Permalink
Merge pull request #15 from graze/array-adapter-out-of-bounds
Browse files Browse the repository at this point in the history
ArrayAdapter throws OutOfBoundsException when empty
  • Loading branch information
jakewright committed Aug 21, 2015
2 parents fd4bf84 + 42e8b07 commit 150f49d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
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

0 comments on commit 150f49d

Please sign in to comment.