Skip to content

Commit

Permalink
Adding support for the phpredis extension
Browse files Browse the repository at this point in the history
Along with the new addition, the code coverage is now up to 88%!
  • Loading branch information
justinrainbow committed Jul 22, 2012
1 parent 3b6a5c9 commit 5f3c12e
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Presque/AbstractJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function isSuccessful()
*/
public function isError()
{
return StatusInterface::FAILED === $this->getStatus();
return !$this->isActive() && !$this->isSuccessful();
}

/**
Expand Down
16 changes: 0 additions & 16 deletions src/Presque/Event/StatusEvent.php

This file was deleted.

54 changes: 54 additions & 0 deletions src/Presque/Storage/PhpredisStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Storage;

use Redis;

class PhpredisStorage implements StorageInterface
{
private $prefix;
private $connection;

public function __construct(Redis $connection, $prefix = null)
{
$this->prefix = $prefix;
$this->connection = $connection;
}

public function setPrefix($prefix = null)
{
$this->prefix = $prefix;

$this->connection->setOption(Redis::OPT_PREFIX, null === $prefix ? '' : rtrim($prefix, ':') . ':');
}

public function getPrefix()
{
return $this->prefix;
}

public function push($listName, $payload)
{
$this->connection->lPush($listName, json_encode($payload));
}

public function pop($listName, $waitTimeout = null)
{
$payload = $this->connection->blPop($listName, $waitTimeout);

if (is_array($payload) && isset($payload[1])) {
return json_decode($payload[1], true);
}

return false;
}
}
91 changes: 91 additions & 0 deletions tests/Presque/Tests/Storage/PhpredisStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Tests\Storage;

use Presque\Tests\TestCase;
use Presque\Storage\PhpredisStorage;
use Mockery as m;

class PhpredisStorageTest extends TestCase
{
protected $redis;
protected $storage;

public function testPushingDataOntoList()
{
$payload = array(
'class' => 'test',
'args' => array()
);

$this->redis
->expects($this->once())
->method('lPush')
->with($this->equalTo('list'), $this->equalTo(json_encode($payload)));

$this->storage->push('list', $payload);
}

public function testPopingDataFromList()
{
$payload = array(
'class' => 'test',
'args' => array()
);

$this->redis
->expects($this->exactly(3))
->method('blPop')
->with($this->equalTo('list'), $this->equalTo(5))
->will($this->onConsecutiveCalls(
array('list', json_encode($payload)),
array(),
false
));

$result = $this->storage->pop('list', 5);
$this->assertTrue(is_array($result));
$this->assertArrayHasKey('class', $result);
$this->assertArrayHasKey('args', $result);
$this->assertEquals('test', $result['class']);

$this->assertFalse($this->storage->pop('list', 5));
$this->assertFalse($this->storage->pop('list', 5));
}

public function testPrefixingRedisData()
{
$this->assertEquals(null, $this->storage->getPrefix());

$this->redis
->expects($this->once())
->method('setOption')
->with($this->equalTo(\Redis::OPT_PREFIX), $this->equalTo('presque:'));

$this->storage->setPrefix('presque');
}

protected function setUp()
{
if (!extension_loaded('redis')) {
$this->markTestSkipped('The phpredis extension is not loaded.');
} else {
$this->redis = $this->createRedisMock();
$this->storage = new PhpredisStorage($this->redis);
}
}

protected function createRedisMock()
{
return $this->getMock('Redis');
}
}

0 comments on commit 5f3c12e

Please sign in to comment.