Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Updated RedisStorage and added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Araujo committed Feb 8, 2013
1 parent 8fe303c commit 5d9013d
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Doctrine/KeyValueStore/Storage/RedisStorage.php
Expand Up @@ -106,7 +106,7 @@ public function delete($storageName, $key)
{
$key = $this->getKeyName($key);
if($this->client->exists($key) === true) {
$this->client->del($key);
$this->client->delete($key);
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ public function getName()
* @param string $id
* @return string
*/
protected function getKeyName($key)
public function getKeyName($key)
{
return $this->keyPrefix . $key;
}
Expand Down
103 changes: 103 additions & 0 deletions tests/Doctrine/Tests/KeyValueStore/Storage/RedisStorageTest.php
@@ -0,0 +1,103 @@
<?php

namespace Doctrine\Tests\KeyValueStore\Storage;

use Doctrine\KeyValueStore\Storage\RedisStorage;

/**
* @author Marcel Araujo <admin@marcelaraujo.me>
*/
class RedisStorageTest extends \PHPUnit_Framework_TestCase
{
/**
* @var RedisStorage
*/
private $storage;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $redis;

protected function setup()
{
if ( ! extension_loaded('redis')) {
$this->markTestSkipped('Redis Extension is not installed.');
}

$this->redis = $this->getMockBuilder('\Redis')
->disableOriginalConstructor()
->getMock();

$this->redis->expects($this->any())
->method('connect')
->with('127.0.0.1', '6379')
->will($this->returnValue(TRUE));

$this->storage = new RedisStorage($this->redis);
}

public function testSupportsPartialUpdates()
{
$this->assertFalse($this->storage->supportsPartialUpdates());
}

public function testSupportsCompositePrimaryKeys()
{
$this->assertFalse($this->storage->supportsCompositePrimaryKeys());
}

public function testRequiresCompositePrimaryKeys()
{
$this->assertFalse($this->storage->requiresCompositePrimaryKeys());
}

public function testInsert()
{
$data = array(
'author' => 'John Doe',
'title' => 'example book',
);

$dbDataset = array();

$this->redis->expects($this->once())
->method('set')
->will($this->returnCallback(function($key, $data) use (&$dbDataset) {
$dbDataset[] = array('key' => $key, 'value' => $data);
}));

$this->storage->insert('redis', '1', $data);

$this->assertCount(1, $dbDataset);
$this->assertEquals(array(array('key' => $this->storage->getKeyName('1'), 'value' => json_encode($data))), $dbDataset);
}

public function testUpdate()
{

$data = array(
'author' => 'John Doe Updated',
'title' => 'example book updated',
);

$dbDataset = array();

$this->redis->expects($this->once())
->method('set')
->will($this->returnCallback(function($key, $data) use (&$dbDataset) {
$dbDataset[] = array('key' => $key, 'value' => $data);
}));


$this->storage->update('redis', '1', $data);

$this->assertCount(1, $dbDataset);
$this->assertEquals(array(array('key' => $this->storage->getKeyName('1'), 'value' => json_encode($data))), $dbDataset);
}

public function testGetName()
{
$this->assertEquals('redis', $this->storage->getName());
}
}

0 comments on commit 5d9013d

Please sign in to comment.