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

Adding support to PhpRedis #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^5.5",
"phpunit/phpunit": "^5.7",
"mockery/mockery": "^0.9.5"
}
}
12 changes: 12 additions & 0 deletions examples/PhpRedisConsumerExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

include __DIR__ . '/../vendor/autoload.php';

$client = new \Redis();
$client->connect('redis', 6379);

$adapter = new \Superbalist\PubSub\Redis\PhpRedisPubSubAdapter($client);

$adapter->subscribe('my_channel', function ($message) {
var_dump($message);
});
13 changes: 13 additions & 0 deletions examples/PhpRedisPublishExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

include __DIR__ . '/../vendor/autoload.php';

$client = new \Redis();
$client->connect('redis', 6379);

$adapter = new \Superbalist\PubSub\Redis\PhpRedisPubSubAdapter($client);

$adapter->publish('my_channel', 'HELLO WORLD');
$adapter->publish('my_channel', ['hello' => 'world']);
$adapter->publish('my_channel', 1);
$adapter->publish('my_channel', false);
70 changes: 70 additions & 0 deletions src/PhpRedisPubSubAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Superbalist\PubSub\Redis;

use Redis;
use Superbalist\PubSub\PubSubAdapterInterface;
use Superbalist\PubSub\Utils;

class PhpRedisPubSubAdapter implements PubSubAdapterInterface
{
/**
* @var Redis
*/
protected $client;

/**
* @param Redis $client
*/
public function __construct(Redis $client)
{
$this->client = $client;
}

/**
* Return the Redis client.
*
* @return Redis
*/
public function getClient()
{
return $this->client;
}

/**
* Subscribe a handler to a channel.
*
* @param string $channel
* @param callable $handler
*/
public function subscribe($channel, callable $handler)
{
$this->client->subscribe([$channel], function($instance, $channelName, $message) use ($handler) {
call_user_func($handler, Utils::unserializeMessagePayload($message));
});
}

/**
* Publish a message to a channel.
*
* @param string $channel
* @param mixed $message
*/
public function publish($channel, $message)
{
$this->client->publish($channel, Utils::serializeMessage($message));
}

/**
* Publish multiple messages to a channel.
*
* @param string $channel
* @param array $messages
*/
public function publishBatch($channel, array $messages)
{
foreach ($messages as $message) {
$this->publish($channel, $message);
}
}
}
71 changes: 71 additions & 0 deletions tests/PhpRedisPubSubAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Tests;

use Mockery;
use Redis;
use PHPUnit\Framework\TestCase;
use Superbalist\PubSub\Redis\PhpRedisPubSubAdapter;

class PhpRedisPubSubAdapterTest extends TestCase
{
protected function tearDown()
{
Mockery::close();
}

public function testGetClient()
{
$client = Mockery::mock(Redis::class);
$adapter = new PhpRedisPubSubAdapter($client);
$this->assertSame($client, $adapter->getClient());
}

public function testSubscribe()
{
$client = Mockery::mock(Redis::class);
$client->shouldReceive('subscribe')
->once();

$adapter = new PhpRedisPubSubAdapter($client);
$adapter->subscribe('subscribe', function(){});
}

public function testPublish()
{
$client = Mockery::mock(Redis::class);
$client->shouldReceive('publish')
->withArgs([
'channel_name',
'{"hello":"world"}',
])
->once();

$adapter = new PhpRedisPubSubAdapter($client);
$adapter->publish('channel_name', ['hello' => 'world']);
}

public function testPublishBatch()
{
$client = Mockery::mock(Redis::class);
$client->shouldReceive('publish')
->withArgs([
'channel_name',
'"message1"',
])
->once();
$client->shouldReceive('publish')
->withArgs([
'channel_name',
'"message2"',
])
->once();

$adapter = new PhpRedisPubSubAdapter($client);
$messages = [
'message1',
'message2',
];
$adapter->publishBatch('channel_name', $messages);
}
}