123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?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\Connection;
- /**
- * @group ext-phpiredis
- */
- class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
- {
- /**
- * @group disconnected
- */
- public function testConstructorDoesNotOpenConnection()
- {
- $connection = new PhpiredisStreamConnection($this->getParameters());
- $this->assertFalse($connection->isConnected());
- }
- /**
- * @group disconnected
- */
- public function testSupportsSchemeTCP()
- {
- $parameters = $this->getParameters(array('scheme' => 'tcp'));
- $connection = new PhpiredisStreamConnection($parameters);
- $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
- }
- /**
- * @group disconnected
- */
- public function testSupportsSchemeRedis()
- {
- $parameters = $this->getParameters(array('scheme' => 'redis'));
- $connection = new PhpiredisStreamConnection($parameters);
- $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
- }
- /**
- * @group disconnected
- */
- public function testSupportsSchemeUnix()
- {
- $parameters = $this->getParameters(array('scheme' => 'unix'));
- $connection = new PhpiredisStreamConnection($parameters);
- $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
- }
- /**
- * @group disconnected
- * @expectedException \InvalidArgumentException
- * @expectedExceptionMessage Invalid scheme: 'udp'.
- */
- public function testThrowsExceptionOnInvalidScheme()
- {
- $parameters = $this->getParameters(array('scheme' => 'udp'));
- new PhpiredisStreamConnection($parameters);
- }
- /**
- * @group disconnected
- */
- public function testExposesParameters()
- {
- $parameters = $this->getParameters();
- $connection = new PhpiredisStreamConnection($parameters);
- $this->assertSame($parameters, $connection->getParameters());
- }
- /**
- * @group disconnected
- */
- public function testCanBeSerialized()
- {
- $parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
- $connection = new PhpiredisStreamConnection($parameters);
- $unserialized = unserialize(serialize($connection));
- $this->assertInstanceOf('Predis\Connection\PhpiredisStreamConnection', $unserialized);
- $this->assertEquals($parameters, $unserialized->getParameters());
- }
- // ******************************************************************** //
- // ---- INTEGRATION TESTS --------------------------------------------- //
- // ******************************************************************** //
- /**
- * @group connected
- */
- public function testAcceptsTcpNodelayParameter()
- {
- if (!version_compare(PHP_VERSION, '5.4.0', '>=')) {
- $this->markTestSkipped('Setting TCP_NODELAY on PHP socket streams works on PHP >= 5.4.0');
- }
- $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => false)));
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- $connection = new PhpiredisStreamConnection($this->getParameters(array('tcp_nodelay' => true)));
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- }
- /**
- * @group connected
- */
- public function testExecutesCommandsOnServer()
- {
- $connection = $this->getConnection($profile, true);
- $cmdPing = $profile->createCommand('ping');
- $cmdEcho = $profile->createCommand('echo', array('echoed'));
- $cmdGet = $profile->createCommand('get', array('foobar'));
- $cmdRpush = $profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol'));
- $cmdLrange = $profile->createCommand('lrange', array('metavars', 0, -1));
- $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
- $this->assertSame('echoed', $connection->executeCommand($cmdEcho));
- $this->assertNull($connection->executeCommand($cmdGet));
- $this->assertSame(3, $connection->executeCommand($cmdRpush));
- $this->assertSame(array('foo', 'hoge', 'lol'), $connection->executeCommand($cmdLrange));
- }
- /**
- * @group connected
- */
- public function testPersistentParameterWithFalseLikeValues()
- {
- if ($this->isHHVM()) {
- $this->markTestSkipped('This test does not currently work on HHVM.');
- }
- $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 0)));
- $this->assertNonPersistentConnection($connection1);
- $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => false)));
- $this->assertNonPersistentConnection($connection2);
- $connection3 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => '0')));
- $this->assertNonPersistentConnection($connection3);
- $connection4 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'false')));
- $this->assertNonPersistentConnection($connection4);
- }
- /**
- * @group connected
- */
- public function testPersistentParameterWithTrueLikeValues()
- {
- if ($this->isHHVM()) {
- $this->markTestSkipped('This test does not currently work on HHVM.');
- }
- $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 1)));
- $this->assertPersistentConnection($connection1);
- $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true)));
- $this->assertPersistentConnection($connection2);
- $connection3 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => '1')));
- $this->assertPersistentConnection($connection3);
- $connection4 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'true')));
- $this->assertPersistentConnection($connection4);
- $connection1->disconnect();
- }
- /**
- * @group connected
- */
- public function testPersistentConnectionsToSameNodeShareResource()
- {
- if ($this->isHHVM()) {
- $this->markTestSkipped('This test does not currently work on HHVM.');
- }
- $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true)));
- $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => true)));
- $this->assertPersistentConnection($connection1);
- $this->assertPersistentConnection($connection2);
- $this->assertSame($connection1->getResource(), $connection2->getResource());
- $connection1->disconnect();
- }
- /**
- * @group connected
- */
- public function testPersistentConnectionsToSameNodeDoNotShareResourceUsingDifferentPersistentID()
- {
- if ($this->isHHVM()) {
- $this->markTestSkipped('This test does not currently work on HHVM.');
- }
- $connection1 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'conn1')));
- $connection2 = new PhpiredisStreamConnection($this->getParameters(array('persistent' => 'conn2')));
- $this->assertPersistentConnection($connection1);
- $this->assertPersistentConnection($connection2);
- $this->assertNotSame($connection1->getResource(), $connection2->getResource());
- }
- /**
- * @medium
- * @group connected
- * @expectedException \Predis\Protocol\ProtocolException
- * @expectedExceptionMessage Protocol error, got "P" as reply type byte
- */
- public function testThrowsExceptionOnProtocolDesynchronizationErrors()
- {
- $connection = $this->getConnection($profile);
- $socket = $connection->getResource();
- $connection->writeRequest($profile->createCommand('ping'));
- fread($socket, 1);
- $connection->read();
- }
- // ******************************************************************** //
- // ---- HELPER METHODS ------------------------------------------------ //
- // ******************************************************************** //
- /**
- * {@inheritdoc}
- */
- protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
- {
- $parameters = $this->getParameters($parameters);
- $profile = $this->getProfile();
- $connection = new PhpiredisStreamConnection($parameters);
- if ($initialize) {
- $connection->addConnectCommand(
- $profile->createCommand('select', array($parameters->database))
- );
- $connection->addConnectCommand(
- $profile->createCommand('flushdb')
- );
- }
- return $connection;
- }
- }
|