diff --git a/composer.json b/composer.json index a82b781..c683fd1 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^5.5", + "phpunit/phpunit": "^5.7", "mockery/mockery": "^0.9.5" } } diff --git a/examples/PhpRedisConsumerExample.php b/examples/PhpRedisConsumerExample.php new file mode 100644 index 0000000..6dc9bc4 --- /dev/null +++ b/examples/PhpRedisConsumerExample.php @@ -0,0 +1,12 @@ +connect('redis', 6379); + +$adapter = new \Superbalist\PubSub\Redis\PhpRedisPubSubAdapter($client); + +$adapter->subscribe('my_channel', function ($message) { + var_dump($message); +}); diff --git a/examples/PhpRedisPublishExample.php b/examples/PhpRedisPublishExample.php new file mode 100644 index 0000000..f19b39a --- /dev/null +++ b/examples/PhpRedisPublishExample.php @@ -0,0 +1,13 @@ +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); diff --git a/src/PhpRedisPubSubAdapter.php b/src/PhpRedisPubSubAdapter.php new file mode 100644 index 0000000..84ba257 --- /dev/null +++ b/src/PhpRedisPubSubAdapter.php @@ -0,0 +1,70 @@ +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); + } + } +} diff --git a/tests/PhpRedisPubSubAdapterTest.php b/tests/PhpRedisPubSubAdapterTest.php new file mode 100644 index 0000000..a6287b8 --- /dev/null +++ b/tests/PhpRedisPubSubAdapterTest.php @@ -0,0 +1,71 @@ +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); + } +}