[tests] Improve code-reuse in tests for the Predis\Connection namespace.

This commit is contained in:
Daniele Alessandri
2015-07-25 20:33:00 +02:00
parent 4f30ac6370
commit c7cae66a97
7 changed files with 314 additions and 496 deletions
+12 -121
View File
@@ -18,103 +18,10 @@ class StreamConnectionTest extends PredisConnectionTestCase
{
const CONNECTION_CLASS = 'Predis\Connection\StreamConnection';
/**
* @group disconnected
*/
public function testConstructorDoesNotOpenConnection()
{
$connection = new StreamConnection($this->getParameters());
$this->assertFalse($connection->isConnected());
}
/**
* @group disconnected
*/
public function testSupportsSchemeTCP()
{
$parameters = $this->getParameters(array('scheme' => 'tcp'));
$connection = new StreamConnection($parameters);
$this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
}
/**
* @group disconnected
*/
public function testSupportsSchemeRedis()
{
$parameters = $this->getParameters(array('scheme' => 'redis'));
$connection = new StreamConnection($parameters);
$this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
}
/**
* @group disconnected
*/
public function testSupportsSchemeUnix()
{
$parameters = $this->getParameters(array('scheme' => 'unix'));
$connection = new StreamConnection($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 StreamConnection($parameters);
}
/**
* @group disconnected
*/
public function testExposesParameters()
{
$parameters = $this->getParameters();
$connection = new StreamConnection($parameters);
$this->assertSame($parameters, $connection->getParameters());
}
/**
* @group disconnected
*/
public function testCanBeSerialized()
{
$parameters = $this->getParameters(array('alias' => 'redis', 'read_write_timeout' => 10));
$connection = new StreamConnection($parameters);
$unserialized = unserialize(serialize($connection));
$this->assertEquals($connection, $unserialized);
}
// ******************************************************************** //
// ---- INTEGRATION TESTS --------------------------------------------- //
// ******************************************************************** //
/**
* @group connected
* @requires PHP 5.4
*/
public function testAcceptsTcpNodelayParameter()
{
$connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => false)));
$connection->connect();
$this->assertTrue($connection->isConnected());
$connection = new StreamConnection($this->getParameters(array('tcp_nodelay' => true)));
$connection->connect();
$this->assertTrue($connection->isConnected());
}
/**
* @group connected
*/
@@ -124,16 +31,16 @@ class StreamConnectionTest extends PredisConnectionTestCase
$this->markTestSkipped('This test does not currently work on HHVM.');
}
$connection1 = new StreamConnection($this->getParameters(array('persistent' => 0)));
$connection1 = $this->createConnectionWithParams(array('persistent' => 0));
$this->assertNonPersistentConnection($connection1);
$connection2 = new StreamConnection($this->getParameters(array('persistent' => false)));
$connection2 = $this->createConnectionWithParams(array('persistent' => false));
$this->assertNonPersistentConnection($connection2);
$connection3 = new StreamConnection($this->getParameters(array('persistent' => '0')));
$connection3 = $this->createConnectionWithParams(array('persistent' => '0'));
$this->assertNonPersistentConnection($connection3);
$connection4 = new StreamConnection($this->getParameters(array('persistent' => 'false')));
$connection4 = $this->createConnectionWithParams(array('persistent' => 'false'));
$this->assertNonPersistentConnection($connection4);
}
@@ -146,16 +53,16 @@ class StreamConnectionTest extends PredisConnectionTestCase
$this->markTestSkipped('This test does not currently work on HHVM.');
}
$connection1 = new StreamConnection($this->getParameters(array('persistent' => 1)));
$connection1 = $this->createConnectionWithParams(array('persistent' => 1));
$this->assertPersistentConnection($connection1);
$connection2 = new StreamConnection($this->getParameters(array('persistent' => true)));
$connection2 = $this->createConnectionWithParams(array('persistent' => true));
$this->assertPersistentConnection($connection2);
$connection3 = new StreamConnection($this->getParameters(array('persistent' => '1')));
$connection3 = $this->createConnectionWithParams(array('persistent' => '1'));
$this->assertPersistentConnection($connection3);
$connection4 = new StreamConnection($this->getParameters(array('persistent' => 'true')));
$connection4 = $this->createConnectionWithParams(array('persistent' => 'true'));
$this->assertPersistentConnection($connection4);
$connection1->disconnect();
@@ -170,8 +77,8 @@ class StreamConnectionTest extends PredisConnectionTestCase
$this->markTestSkipped('This test does not currently work on HHVM.');
}
$connection1 = new StreamConnection($this->getParameters(array('persistent' => true)));
$connection2 = new StreamConnection($this->getParameters(array('persistent' => true)));
$connection1 = $this->createConnectionWithParams(array('persistent' => true));
$connection2 = $this->createConnectionWithParams(array('persistent' => true));
$this->assertPersistentConnection($connection1);
$this->assertPersistentConnection($connection2);
@@ -190,28 +97,12 @@ class StreamConnectionTest extends PredisConnectionTestCase
$this->markTestSkipped('This test does not currently work on HHVM.');
}
$connection1 = new StreamConnection($this->getParameters(array('persistent' => 'conn1')));
$connection2 = new StreamConnection($this->getParameters(array('persistent' => 'conn2')));
$connection1 = $this->createConnectionWithParams(array('persistent' => 'conn1'));
$connection2 = $this->createConnectionWithParams(array('persistent' => 'conn2'));
$this->assertPersistentConnection($connection1);
$this->assertPersistentConnection($connection2);
$this->assertNotSame($connection1->getResource(), $connection2->getResource());
}
/**
* @group connected
* @expectedException \Predis\Protocol\ProtocolException
* @expectedExceptionMessage Unknown response prefix: 'P'.
*/
public function testThrowsExceptionOnProtocolDesynchronizationErrors()
{
$connection = $this->getConnection($profile);
$stream = $connection->getResource();
$connection->writeRequest($profile->createCommand('ping'));
fread($stream, 1);
$connection->read();
}
}