Просмотр исходного кода

[tests] Share common test among connection classes.

Daniele Alessandri 9 лет назад
Родитель
Сommit
3700d48771

+ 41 - 1
tests/PHPUnit/PredisConnectionTestCase.php

@@ -126,6 +126,26 @@ abstract class PredisConnectionTestCase extends PredisTestCase
         $this->assertEquals('PONG', $connection->executeCommand($cmdPing));
     }
 
+    /**
+     * @group connected
+     */
+    public function testExecutesMultipleCommandsOnServer()
+    {
+        $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
      */
@@ -343,5 +363,25 @@ abstract class PredisConnectionTestCase extends PredisTestCase
      *
      * @return StreamConnection
      */
-    abstract protected function getConnection(&$profile = null, $initialize = false, array $parameters = array());
+    protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
+    {
+        $class = static::CONNECTION_CLASS;
+
+        $parameters = $this->getParameters($parameters);
+        $profile = $this->getProfile();
+
+        $connection = new $class($parameters);
+
+        if ($initialize) {
+            $connection->addConnectCommand(
+                $profile->createCommand('select', array($parameters->database))
+            );
+
+            $connection->addConnectCommand(
+                $profile->createCommand('flushdb')
+            );
+        }
+
+        return $connection;
+    }
 }

+ 2 - 27
tests/Predis/Connection/CompositeStreamConnectionTest.php

@@ -16,6 +16,8 @@ namespace Predis\Connection;
  */
 class CompositeStreamConnectionTest extends PredisConnectionTestCase
 {
+    const CONNECTION_CLASS = 'Predis\Connection\CompositeStreamConnection';
+
     /**
      * @group disconnected
      */
@@ -128,31 +130,4 @@ class CompositeStreamConnectionTest extends PredisConnectionTestCase
 
         $connection->read();
     }
-
-    // ******************************************************************** //
-    // ---- HELPER METHODS ------------------------------------------------ //
-    // ******************************************************************** //
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
-    {
-        $parameters = $this->getParameters($parameters);
-        $profile = $this->getProfile();
-
-        $connection = new CompositeStreamConnection($parameters);
-
-        if ($initialize) {
-            $connection->addConnectCommand(
-                $profile->createCommand('select', array($parameters->database))
-            );
-
-            $connection->addConnectCommand(
-                $profile->createCommand('flushdb')
-            );
-        }
-
-        return $connection;
-    }
 }

+ 2 - 47
tests/Predis/Connection/PhpiredisSocketConnectionTest.php

@@ -17,6 +17,8 @@ namespace Predis\Connection;
  */
 class PhpiredisSocketConnectionTest extends PredisConnectionTestCase
 {
+    const CONNECTION_CLASS = 'Predis\Connection\PhpiredisSocketConnection';
+
     /**
      * @group disconnected
      */
@@ -100,26 +102,6 @@ class PhpiredisSocketConnectionTest extends PredisConnectionTestCase
     // ---- INTEGRATION TESTS --------------------------------------------- //
     // ******************************************************************** //
 
-    /**
-     * @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
      * @expectedException \Predis\Connection\ConnectionException
@@ -147,31 +129,4 @@ class PhpiredisSocketConnectionTest extends PredisConnectionTestCase
 
         $connection->read();
     }
-
-    // ******************************************************************** //
-    // ---- HELPER METHODS ------------------------------------------------ //
-    // ******************************************************************** //
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
-    {
-        $parameters = $this->getParameters($parameters);
-        $profile = $this->getProfile();
-
-        $connection = new PhpiredisSocketConnection($parameters);
-
-        if ($initialize) {
-            $connection->addConnectCommand(
-                $profile->createCommand('select', array($parameters->database))
-            );
-
-            $connection->addConnectCommand(
-                $profile->createCommand('flushdb')
-            );
-        }
-
-        return $connection;
-    }
 }

+ 2 - 47
tests/Predis/Connection/PhpiredisStreamConnectionTest.php

@@ -17,6 +17,8 @@ namespace Predis\Connection;
  */
 class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
 {
+    const CONNECTION_CLASS = 'Predis\Connection\PhpiredisStreamConnection';
+
     /**
      * @group disconnected
      */
@@ -115,26 +117,6 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
         $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));
-    }
-
     /**
      * @medium
      * @group connected
@@ -151,31 +133,4 @@ class PhpiredisStreamConnectionTest extends PredisConnectionTestCase
 
         $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;
-    }
 }

+ 2 - 27
tests/Predis/Connection/StreamConnectionTest.php

@@ -16,6 +16,8 @@ namespace Predis\Connection;
  */
 class StreamConnectionTest extends PredisConnectionTestCase
 {
+    const CONNECTION_CLASS = 'Predis\Connection\StreamConnection';
+
     /**
      * @group disconnected
      */
@@ -128,31 +130,4 @@ class StreamConnectionTest extends PredisConnectionTestCase
 
         $connection->read();
     }
-
-    // ******************************************************************** //
-    // ---- HELPER METHODS ------------------------------------------------ //
-    // ******************************************************************** //
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function getConnection(&$profile = null, $initialize = false, array $parameters = array())
-    {
-        $parameters = $this->getParameters($parameters);
-        $profile = $this->getProfile();
-
-        $connection = new StreamConnection($parameters);
-
-        if ($initialize) {
-            $connection->addConnectCommand(
-                $profile->createCommand('select', array($parameters->database))
-            );
-
-            $connection->addConnectCommand(
-                $profile->createCommand('flushdb')
-            );
-        }
-
-        return $connection;
-    }
 }

+ 6 - 28
tests/Predis/Connection/WebdisConnectionTest.php

@@ -11,7 +11,6 @@
 
 namespace Predis\Connection;
 
-use Predis\Profile;
 use PredisTestCase;
 
 /**
@@ -24,6 +23,8 @@ use PredisTestCase;
  */
 class WebdisConnectionTest extends PredisTestCase
 {
+    const CONNECTION_CLASS = 'Predis\Connection\WebdisConnection';
+
     /**
      * @group disconnected
      */
@@ -140,26 +141,6 @@ class WebdisConnectionTest extends PredisTestCase
     // ---- INTEGRATION TESTS --------------------------------------------- //
     // ******************************************************************** //
 
-    /**
-     * @group connected
-     */
-    public function testExecutesCommandsOnServer()
-    {
-        $connection = $this->getConnection($profile);
-
-        $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));
-    }
-
     /**
      * @medium
      * @group disconnected
@@ -192,19 +173,16 @@ class WebdisConnectionTest extends PredisTestCase
     }
 
     /**
-     * Returns a new instance of a connection instance.
-     *
-     * @param mixed $profile    Redis profile.
-     * @param array $parameters Additional connection parameters.
-     *
-     * @return WebdisConnection
+     * {@inheritdoc}
      */
     protected function getConnection(&$profile = null, array $parameters = array())
     {
+        $class = static::CONNECTION_CLASS;
+
         $parameters = $this->getParameters($parameters);
         $profile = $this->getProfile();
 
-        $connection = new WebdisConnection($parameters);
+        $connection = new $class($parameters);
         $connection->executeCommand($profile->createCommand('flushdb'));
 
         return $connection;