123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- /*
- * This file is part of the Predis package.
- *
- * (c) Daniele Alessandri <suppakilla@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Predis\PubSub;
- use PredisTestCase;
- use Predis\Client;
- use Predis\Profile\ServerProfile;
- /**
- * @group realm-pubsub
- */
- class PubSubContextTest extends PredisTestCase
- {
- /**
- * @group disconnected
- * @expectedException Predis\NotSupportedException
- * @expectedExceptionMessage The current profile does not support PUB/SUB related commands
- */
- public function testPubSubContextRequirePubSubRelatedCommand()
- {
- $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
- $profile->expects($this->any())
- ->method('supportsCommands')
- ->will($this->returnValue(false));
- $client = new Client(null, array('profile' => $profile));
- $pubsub = new PubSubContext($client);
- }
- /**
- * @group disconnected
- * @expectedException Predis\NotSupportedException
- * @expectedExceptionMessage Cannot initialize a PUB/SUB context when using aggregated connections
- */
- public function testPubSubContextDoesNotWorkOnClusters()
- {
- $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
- $client = new Client($cluster);
- $pubsub = new PubSubContext($client);
- }
- /**
- * @group disconnected
- */
- public function testConstructorWithoutSubscriptionsDoesNotOpenContext()
- {
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $client = $this->getMock('Predis\Client', array('executeCommand'), array($connection));
- $client->expects($this->never())->method('executeCommand');
- $pubsub = new PubSubContext($client);
- }
- /**
- * @group disconnected
- */
- public function testConstructorWithSubscriptionsOpensContext()
- {
- $profile = ServerProfile::get(REDIS_SERVER_VERSION);
- $cmdSubscribe = $profile->createCommand('subscribe', array('channel:foo'));
- $cmdPsubscribe = $profile->createCommand('psubscribe', array('channels:*'));
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->exactly(2))->method('writeCommand');
- $client = $this->getMock('Predis\Client', array('createCommand', 'writeCommand'), array($connection));
- $client->expects($this->exactly(2))
- ->method('createCommand')
- ->with($this->logicalOr($this->equalTo('subscribe'), $this->equalTo('psubscribe')))
- ->will($this->returnCallback(function ($id, $args) use ($profile) {
- return $profile->createCommand($id, $args);
- }));
- $options = array('subscribe' => 'channel:foo', 'psubscribe' => 'channels:*');
- $pubsub = new PubSubContext($client, $options);
- }
- /**
- * @group disconnected
- */
- public function testClosingContextWithTrueClosesConnection()
- {
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
- $client->expects($this->exactly(1))->method('disconnect');
- $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
- $connection->expects($this->never())->method('writeCommand');
- $pubsub->closeContext(true);
- }
- /**
- * @group disconnected
- */
- public function testClosingContextWithFalseSendsUnsubscriptions()
- {
- $profile = ServerProfile::get(REDIS_SERVER_VERSION);
- $classUnsubscribe = $profile->getCommandClass('unsubscribe');
- $classPunsubscribe = $profile->getCommandClass('punsubscribe');
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
- $options = array('subscribe' => 'channel:foo', 'psubscribe' => 'channels:*');
- $pubsub = new PubSubContext($client, $options);
- $connection->expects($this->exactly(2))
- ->method('writeCommand')
- ->with($this->logicalOr(
- $this->isInstanceOf($classUnsubscribe),
- $this->isInstanceOf($classPunsubscribe)
- ));
- $pubsub->closeContext(false);
- }
- /**
- * @group disconnected
- */
- public function testIsNotValidWhenNotSubscribed()
- {
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $client = $this->getMock('Predis\Client', array('disconnect'), array($connection));
- $pubsub = new PubSubContext($client);
- $this->assertFalse($pubsub->valid());
- $this->assertNull($pubsub->next());
- }
- public function testHandlesPongMessages()
- {
- $rawmessage = array('pong', '');
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
- $client = new Client($connection);
- $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
- $message = $pubsub->current();
- $this->assertSame('pong', $message->kind);
- $this->assertSame('', $message->payload);
- }
- /**
- * @group disconnected
- */
- public function testHandlesPongMessagesWithPayload()
- {
- $rawmessage = array('pong', 'foobar');
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
- $client = new Client($connection);
- $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
- $message = $pubsub->current();
- $this->assertSame('pong', $message->kind);
- $this->assertSame('foobar', $message->payload);
- }
- /**
- * @group disconnected
- */
- public function testReadsMessageFromConnection()
- {
- $rawmessage = array('message', 'channel:foo', 'message from channel');
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
- $client = new Client($connection);
- $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
- $message = $pubsub->current();
- $this->assertSame('message', $message->kind);
- $this->assertSame('channel:foo', $message->channel);
- $this->assertSame('message from channel', $message->payload);
- }
- /**
- * @group disconnected
- */
- public function testReadsPmessageFromConnection()
- {
- $rawmessage = array('pmessage', 'channel:*', 'channel:foo', 'message from channel');
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
- $client = new Client($connection);
- $pubsub = new PubSubContext($client, array('psubscribe' => 'channel:*'));
- $message = $pubsub->current();
- $this->assertSame('pmessage', $message->kind);
- $this->assertSame('channel:*', $message->pattern);
- $this->assertSame('channel:foo', $message->channel);
- $this->assertSame('message from channel', $message->payload);
- }
- /**
- * @group disconnected
- */
- public function testReadsSubscriptionMessageFromConnection()
- {
- $rawmessage = array('subscribe', 'channel:foo', 1);
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
- $client = new Client($connection);
- $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
- $message = $pubsub->current();
- $this->assertSame('subscribe', $message->kind);
- $this->assertSame('channel:foo', $message->channel);
- $this->assertSame(1, $message->payload);
- }
- /**
- * @group disconnected
- */
- public function testReadsUnsubscriptionMessageFromConnection()
- {
- $rawmessage = array('unsubscribe', 'channel:foo', 1);
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
- $client = new Client($connection);
- $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
- $message = $pubsub->current();
- $this->assertSame('unsubscribe', $message->kind);
- $this->assertSame('channel:foo', $message->channel);
- $this->assertSame(1, $message->payload);
- }
- /**
- * @group disconnected
- */
- public function testUnsubscriptionMessageWithZeroChannelCountInvalidatesContext()
- {
- $rawmessage = array('unsubscribe', 'channel:foo', 0);
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->once())->method('read')->will($this->returnValue($rawmessage));
- $client = new Client($connection);
- $pubsub = new PubSubContext($client, array('subscribe' => 'channel:foo'));
- $this->assertTrue($pubsub->valid());
- $message = $pubsub->current();
- $this->assertSame('unsubscribe', $message->kind);
- $this->assertSame('channel:foo', $message->channel);
- $this->assertSame(0, $message->payload);
- $this->assertFalse($pubsub->valid());
- }
- /**
- * @group disconnected
- */
- public function testGetUnderlyingClientInstance()
- {
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $client = new Client($connection);
- $pubsub = new PubSubContext($client);
- $this->assertSame($client, $pubsub->getClient());
- }
- // ******************************************************************** //
- // ---- INTEGRATION TESTS --------------------------------------------- //
- // ******************************************************************** //
- /**
- * @group connected
- */
- public function testPubSubAgainstRedisServer()
- {
- $parameters = array(
- 'host' => REDIS_SERVER_HOST,
- 'port' => REDIS_SERVER_PORT,
- 'database' => REDIS_SERVER_DBNUM,
- // Prevents suite from handing on broken test
- 'read_write_timeout' => 2,
- );
- $options = array('profile' => REDIS_SERVER_VERSION);
- $messages = array();
- $producer = new Client($parameters, $options);
- $producer->connect();
- $consumer = new Client($parameters, $options);
- $consumer->connect();
- $pubsub = new PubSubContext($consumer);
- $pubsub->subscribe('channel:foo');
- $producer->publish('channel:foo', 'message1');
- $producer->publish('channel:foo', 'message2');
- $producer->publish('channel:foo', 'QUIT');
- foreach ($pubsub as $message) {
- if ($message->kind !== 'message') {
- continue;
- }
- $messages[] = ($payload = $message->payload);
- if ($payload === 'QUIT') {
- $pubsub->closeContext();
- }
- }
- $this->assertSame(array('message1', 'message2', 'QUIT'), $messages);
- $this->assertFalse($pubsub->valid());
- $this->assertEquals('ECHO', $consumer->echo('ECHO'));
- }
- }
|