Skip to content

Commit

Permalink
Merge pull request #95 from fhjbalfoort/master
Browse files Browse the repository at this point in the history
RedisStore use ClientInterface instead of Client
  • Loading branch information
ackintosh committed Apr 26, 2022
2 parents 7c7dcac + 3a8c69e commit 5792a96
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/Ganesha/Storage/Adapter/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
class RedisStore
{
/**
* @var \Predis\Client|\Redis|\RedisArray|\RedisCluster
* @var \Predis\ClientInterface|\Redis|\RedisArray|\RedisCluster
*/
private $redis;

/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient
* @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient
*
* @throws \InvalidArgumentException if redis client is not supported
*/
Expand All @@ -23,10 +23,10 @@ public function __construct($redisClient)
!$redisClient instanceof \Redis
&& !$redisClient instanceof \RedisArray
&& !$redisClient instanceof \RedisCluster
&& !$redisClient instanceof \Predis\Client
&& !$redisClient instanceof \Predis\ClientInterface
) {
throw new \InvalidArgumentException(sprintf(
'%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given',
'%s() expects parameter 1 to be Redis, RedisArray, RedisCluster, or \Predis\ClientInterface %s given',
__METHOD__,
\is_object($redisClient) ? \get_class($redisClient) : \gettype($redisClient)
));
Expand Down Expand Up @@ -178,7 +178,7 @@ public function get(string $key)
{
try {
$result = $this->redis->get($key);
if ($this->redis instanceof \Predis\Client && $result === null) {
if ($this->redis instanceof \Predis\ClientInterface && $result === null) {
return false;
}
return $result;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Ackintosh\Ganesha\Storage\Adapter;

use Predis\ClientInterface;

class PredisRedisClientInterfaceTest extends PredisRedisTest
{
protected function getRedisConnection(): ClientInterface
{
return new PredisRedisClientInterfaceTestDouble(
parent::getRedisConnection()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);

namespace Ackintosh\Ganesha\Storage\Adapter;

use Predis\ClientInterface;
use Predis\Command\CommandInterface;

final class PredisRedisClientInterfaceTestDouble implements ClientInterface
{
/**
* @var ClientInterface
*/
private $client;

public function __construct(ClientInterface $client)
{
$this->client = $client;
}

/**
* {@inheritdoc}
*/
public function getProfile()
{
return $this->client->getProfile();
}

/**
* {@inheritdoc}
*/
public function getOptions()
{
return $this->client->getOptions();
}

/**
* {@inheritdoc}
*/
public function connect()
{
$this->client->connect();
}

/**
* {@inheritdoc}
*/
public function disconnect()
{
$this->client->disconnect();
}

/**
* {@inheritdoc}
*/
public function getConnection()
{
return $this->client->getConnection();
}

/**
* {@inheritdoc}
*/
public function createCommand($commandID, $arguments = [])
{
return $this->client->createCommand($commandID, $arguments);
}

/**
* {@inheritdoc}
*/
public function executeCommand(CommandInterface $command)
{
return $this->client->executeCommand($command);
}

/**
* {@inheritdoc}
*/
public function __call($commandID, $arguments)
{
return $this->executeCommand(
$this->createCommand($commandID, $arguments)
);
}
}
6 changes: 2 additions & 4 deletions tests/Ackintosh/Ganesha/Storage/Adapter/PredisRedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
namespace Ackintosh\Ganesha\Storage\Adapter;

use Predis\Client;
use Predis\ClientInterface;

class PredisRedisTest extends AbstractRedisTest
{
/**
* @return Client
*/
protected function getRedisConnection(): Client
protected function getRedisConnection(): ClientInterface
{
$r = new Client('tcp://' . (getenv('GANESHA_EXAMPLE_REDIS') ?: 'localhost'));
$r->connect();
Expand Down

0 comments on commit 5792a96

Please sign in to comment.