Browse Source

[tests] Add more tests for Predis\Network\MasterSlaveReplication.

Daniele Alessandri 13 years ago
parent
commit
e3fba5d4b7
2 changed files with 54 additions and 0 deletions
  1. 31 0
      tests/Predis/ClientTest.php
  2. 23 0
      tests/Predis/ConnectionFactoryTest.php

+ 31 - 0
tests/Predis/ClientTest.php

@@ -15,6 +15,7 @@ use \PHPUnit_Framework_TestCase as StandardTestCase;
 
 use Predis\Profiles\ServerProfile;
 use Predis\Network\PredisCluster;
+use Predis\Network\MasterSlaveReplication;
 
 /**
  *
@@ -179,6 +180,22 @@ class ClientTest extends StandardTestCase
         $this->assertSame($cluster, $client->getConnection());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testConstructorWithReplicationArgument()
+    {
+        $replication = new MasterSlaveReplication();
+
+        $factory = new ConnectionFactory();
+        $factory->createReplication($replication, array('tcp://host1?alias=master', 'tcp://host2?alias=slave'));
+
+        $client = new Client($replication);
+
+        $this->assertInstanceOf('Predis\Network\IConnectionReplication', $client->getConnection());
+        $this->assertSame($replication, $client->getConnection());
+    }
+
     /**
      * @group disconnected
      */
@@ -217,6 +234,20 @@ class ClientTest extends StandardTestCase
         $this->assertSame($factory, $client->getConnectionFactory());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testConstructorWithArrayAndOptionReplicationArgument()
+    {
+        $arg1 = array('tcp://host1?alias=master', 'tcp://host2?alias=slave');
+        $arg2 = array('replication' => true);
+        $client = new Client($arg1, $arg2);
+
+        $this->assertInstanceOf('Predis\Network\IConnectionReplication', $connection = $client->getConnection());
+        $this->assertSame('host1', $connection->getConnectionById('master')->getParameters()->host);
+        $this->assertSame('host2', $connection->getConnectionById('slave')->getParameters()->host);
+    }
+
     /**
      * @group disconnected
      */

+ 23 - 0
tests/Predis/ConnectionFactoryTest.php

@@ -335,6 +335,29 @@ class ConnectionFactoryTest extends StandardTestCase
         $factory->createCluster($cluster, $nodes, $profile);
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testReplicationWithMixedConnectionParameters()
+    {
+        list(, $connectionClass) = $this->getMockConnectionClass();
+
+        $replication = $this->getMock('Predis\Network\IConnectionReplication');
+        $replication->expects($this->exactly(4))
+                    ->method('add')
+                    ->with($this->isInstanceOf('Predis\Network\IConnectionSingle'));
+
+        $factory = $this->getMock('Predis\ConnectionFactory', array('create'));
+        $factory->expects($this->exactly(3))
+                ->method('create')
+                ->will($this->returnCallback(function($_, $_) use($connectionClass) {
+                    return new $connectionClass;
+                }));
+
+        $factory->createReplication($replication, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
+    }
+
+
     // ******************************************************************** //
     // ---- HELPER METHODS ------------------------------------------------ //
     // ******************************************************************** //