From 71d64bf5311e0afc40567c34da44d83280f4901d Mon Sep 17 00:00:00 2001 From: asiries335 Date: Sun, 6 Sep 2020 18:07:08 +0300 Subject: [PATCH 1/2] feat(client): Add ClientRedisStreamPhpInterface and update tests --- src/Client.php | 8 ++++---- src/ClientRedisStreamPhpInterface.php | 17 +++++++++++++++++ src/Stream.php | 18 +++++++++--------- tests/Unit/StreamTest.php | 11 ++++++----- 4 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 src/ClientRedisStreamPhpInterface.php diff --git a/src/Client.php b/src/Client.php index 46954ce..b231fbc 100644 --- a/src/Client.php +++ b/src/Client.php @@ -14,18 +14,18 @@ final class Client { /** - * Client. + * Client Redis Interface. * - * @var Redis + * @var ClientRedisStreamPhpInterface */ private $_client; /** * Client constructor. * - * @param \Redis $redisClient Redis + * @param ClientRedisStreamPhpInterface $redisClient Client redis interface */ - public function __construct(\Redis $redisClient) + public function __construct(ClientRedisStreamPhpInterface $redisClient) { $this->_client = $redisClient; } diff --git a/src/ClientRedisStreamPhpInterface.php b/src/ClientRedisStreamPhpInterface.php new file mode 100644 index 0000000..e3e33d1 --- /dev/null +++ b/src/ClientRedisStreamPhpInterface.php @@ -0,0 +1,17 @@ +_client = $client; $this->_streamName = $nameStream; @@ -60,7 +60,7 @@ public function __construct(\Redis $client, string $nameStream) public function add(string $key, array $values) : string { try { - return (string) $this->_client->rawCommand( + return (string) $this->_client->call( Constants::COMMAND_XADD, $this->_streamName, '*', @@ -86,7 +86,7 @@ public function add(string $key, array $values) : string public function delete(string $key) : int { try { - return (int) $this->_client->rawCommand( + return (int) $this->_client->call( Constants::COMMAND_XDEL, $this->_streamName, $key @@ -108,7 +108,7 @@ public function delete(string $key) : int public function get() : Collection { try { - $items = $this->_client->rawCommand( + $items = $this->_client->call( Constants::COMMAND_XREAD, 'STREAMS', $this->_streamName, @@ -146,7 +146,7 @@ public function listen(\Closure $closure) : void $loop->addPeriodicTimer( Constants::TIME_TICK_INTERVAL, function () use ($closure, $messageHydrate, $loop) { - $rows = $this->_client->rawCommand( + $rows = $this->_client->call( Constants::COMMAND_XRANGE, $this->_streamName, '-', diff --git a/tests/Unit/StreamTest.php b/tests/Unit/StreamTest.php index 78b1908..191c3c9 100644 --- a/tests/Unit/StreamTest.php +++ b/tests/Unit/StreamTest.php @@ -3,6 +3,7 @@ namespace Asiries335\redisSteamPhp\Tests; +use Asiries335\redisSteamPhp\ClientRedisStreamPhpInterface; use Asiries335\redisSteamPhp\Data\Collection; use Asiries335\redisSteamPhp\Stream; use PHPUnit\Framework\TestCase; @@ -20,7 +21,7 @@ class StreamTest extends TestCase */ public function setUp() : void { - $this->client = \Mockery::mock(\Redis::class); + $this->client = \Mockery::mock(ClientRedisStreamPhpInterface::class); } /** @@ -32,7 +33,7 @@ public function setUp() : void */ public function testReadEmptyStream() : void { - $this->client->shouldReceive('rawCommand')->andReturn([]); + $this->client->shouldReceive('call')->andReturn([]); $stream = new Stream($this->client, self::TEST_NAME_STREAM); @@ -58,7 +59,7 @@ public function testAddDataToStream() : void 'age' => 25, ]; - $this->client->shouldReceive('rawCommand')->andReturn($key); + $this->client->shouldReceive('call')->andReturn($key); $stream = new Stream($this->client, self::TEST_NAME_STREAM); @@ -89,7 +90,7 @@ public function testReadStream() : void ] ]; - $this->client->shouldReceive('rawCommand')->andReturn($data); + $this->client->shouldReceive('call')->andReturn($data); $stream = new Stream($this->client, self::TEST_NAME_STREAM); @@ -116,7 +117,7 @@ public function testDeleteMessage() : void 'age' => 25, ]; - $this->client->shouldReceive('rawCommand')->andReturn($key); + $this->client->shouldReceive('call')->andReturn($key); $stream = new Stream($this->client, self::TEST_NAME_STREAM); From 7e88a5363f24732b5bca5b8cfaac9f33f8407c9a Mon Sep 17 00:00:00 2001 From: asiries335 Date: Sun, 6 Sep 2020 18:09:22 +0300 Subject: [PATCH 2/2] feat(stream): update README --- README.md | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b63a583..8c1145c 100644 --- a/README.md +++ b/README.md @@ -32,12 +32,31 @@ _Start working_ connect('127.0.0.1', '6379'); +class Config implements \Asiries335\redisSteamPhp\ClientRedisStreamPhpInterface { + private $client; -$client = new \Asiries335\redisSteamPhp\Client($redisClient); + public function __construct() + { + $this->client = new \Redis(); + $this->client->connect('127.0.0.1', '6379'); + } + + /** + * Method for run command of redis + * + * @param string $command + * @param mixed ...$args + * + * @return mixed + */ + public function call(string $command, ...$args) + { + return $this->client->rawCommand($command, ...$args); + } +} + +$client = new \Asiries335\redisSteamPhp\Client(new Config()); ``` _Add a message to stream_