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

[tests] Improve code coverage.

There is still some work to improve coverage in other classes though.
Daniele Alessandri 8 лет назад
Родитель
Сommit
f02f3b6d37

+ 3 - 1
src/Client.php

@@ -90,7 +90,7 @@ class Client implements ClientInterface, \IteratorAggregate
             return $options;
         }
 
-        throw new \InvalidArgumentException('Invalid type for client options.');
+        throw new \InvalidArgumentException('Invalid type for client options');
     }
 
     /**
@@ -416,9 +416,11 @@ class Client implements ClientInterface, \IteratorAggregate
 
                 return $this->$initializer($arg0, $arg1);
 
+        // @codeCoverageIgnoreStart
             default:
                 return $this->$initializer($this, $argv);
         }
+        // @codeCoverageIgnoreEnd
     }
 
     /**

+ 3 - 0
src/Command/Factory.php

@@ -52,9 +52,12 @@ abstract class Factory implements FactoryInterface
     /**
      * Returns the FQN of a class that represents the specified command ID.
      *
+     * @codeCoverageIgnore
+     *
      * @param string $commandID Command ID.
      *
      * @return string|null
+     *
      */
     public function getCommandClass($commandID)
     {

+ 1 - 2
src/Command/Processor/ProcessorChain.php

@@ -111,8 +111,7 @@ class ProcessorChain implements \ArrayAccess, ProcessorInterface
     {
         if (!$processor instanceof ProcessorInterface) {
             throw new \InvalidArgumentException(
-                'A processor chain accepts only instances of '.
-                "'Predis\Command\Processor\ProcessorInterface'."
+                'Processor chain accepts only instances of `Predis\Command\Processor\ProcessorInterface`'
             );
         }
 

+ 1 - 1
src/Command/Redis/CLIENT.php

@@ -46,7 +46,7 @@ class CLIENT extends RedisCommand
             case 'SETNAME':
             default:
                 return $data;
-        }
+        } // @codeCoverageIgnore
     }
 
     /**

+ 51 - 9
tests/Predis/ClientTest.php

@@ -366,6 +366,26 @@ class ClientTest extends PredisTestCase
         $this->assertSame($connection, $client->getConnection());
     }
 
+    /**
+     * @group disconnected
+     * @expectedException \InvalidArgumentException
+     * @expectedExceptionMessage Invalid type for connection parameters
+     */
+    public function testConstructorWithInvalidArgumentType()
+    {
+        $client = new Client(new \stdClass);
+    }
+
+    /**
+     * @group disconnected
+     * @expectedException \InvalidArgumentException
+     * @expectedExceptionMessage Invalid type for client options
+     */
+    public function testConstructorWithInvalidOptionType()
+    {
+        $client = new Client('tcp://host1', new \stdClass);
+    }
+
     /**
      * @group disconnected
      */
@@ -802,24 +822,46 @@ class ClientTest extends PredisTestCase
      */
     public function testPubSubLoopWithArrayAndCallableExecutesPubSub()
     {
-        // NOTE: we use a subscribe count of 0 in the fake message to trick
-        //       the context and to make it think that it can be closed
-        //       since there are no more subscriptions active.
-
-        $message = array('subscribe', 'channel', 0);
-        $options = array('subscribe' => 'channel');
-
+        // NOTE: we use a subscribe count of 0 in the message payload to trick
+        //       the context and forcing it to be closed since there are no more
+        //       active subscriptions.
         $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
         $connection->expects($this->once())
                    ->method('read')
-                   ->will($this->returnValue($message));
+                   ->will($this->returnValue(array('subscribe', 'channel', 0)));
 
         $callable = $this->getMock('stdClass', array('__invoke'));
         $callable->expects($this->once())
                  ->method('__invoke');
 
         $client = new Client($connection);
-        $client->pubSubLoop($options, $callable);
+        $this->assertNull($client->pubSubLoop(array('subscribe' => 'channel'), $callable));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testPubSubLoopWithCallableReturningFalseStopsPubSubConsumer()
+    {
+        $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
+        $connection->expects($this->at(1))
+                   ->method('read')
+                   ->will($this->returnValue(array('subscribe', 'channel', 1)));
+        $connection->expects($this->at(2))
+                   ->method('writeRequest')
+                   ->with($this->isRedisCommand('UNSUBSCRIBE'));
+        $connection->expects($this->at(3))
+                   ->method('read')
+                   ->will($this->returnValue(array('unsubscribe', 'channel', 0)));
+
+        $callable = $this->getMock('stdClass', array('__invoke'));
+        $callable->expects($this->at(0))
+                 ->method('__invoke')
+                 ->will($this->returnValue(false));
+
+        $client = new Client($connection);
+
+        $this->assertNull($client->pubSubLoop(array('subscribe' => 'channel'), $callable));
     }
 
     /**

+ 11 - 0
tests/Predis/Command/Processor/KeyPrefixProcessorTest.php

@@ -338,6 +338,17 @@ class KeyPrefixProcessorTest extends PredisTestCase
         $this->assertSame(array('key', 'value'), $command->getArguments());
     }
 
+    /**
+     * @group disconnected
+     * @expectedException \InvalidArgumentException
+     * @expectedExceptionMessage Callback must be a valid callable object or NULL
+     */
+    public function testCannotDefineCommandHandlerWithInvalidType()
+    {
+        $processor = new KeyPrefixProcessor('prefix:');
+        $processor->setCommandHandler('NEWCMD', new \stdClass());
+    }
+
     // ******************************************************************** //
     // ---- HELPER METHODS ------------------------------------------------ //
     // ******************************************************************** //

+ 72 - 0
tests/Predis/Command/Processor/ProcessorChainTest.php

@@ -148,6 +148,78 @@ class ProcessorChainTest extends PredisTestCase
         $this->assertEmpty($chain->getProcessors());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testOffsetGet()
+    {
+        $processors = array(
+            $this->getMock('Predis\Command\Processor\ProcessorInterface'),
+            $this->getMock('Predis\Command\Processor\ProcessorInterface'),
+        );
+
+        $chain = new ProcessorChain($processors);
+
+        $this->assertSame($processors[0], $chain[0]);
+        $this->assertSame($processors[1], $chain[1]);
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testOffsetIsset()
+    {
+        $processors = array(
+            $this->getMock('Predis\Command\Processor\ProcessorInterface'),
+            $this->getMock('Predis\Command\Processor\ProcessorInterface'),
+        );
+
+        $chain = new ProcessorChain($processors);
+
+        $this->assertTrue(isset($chain[0]));
+        $this->assertTrue(isset($chain[1]));
+        $this->assertFalse(isset($chain[2]));
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testOffsetSet()
+    {
+        $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
+
+        $chain = new ProcessorChain();
+        $chain[0] = $processor;
+
+        $this->assertSame($processor, $chain[0]);
+    }
+
+    /**
+     * @group disconnected
+     * @expectedException \InvalidArgumentException
+     * @expectedExceptionMessage Processor chain accepts only instances of `Predis\Command\Processor\ProcessorInterface`
+     */
+    public function testOffsetSetWithInvalidType()
+    {
+        $chain = new ProcessorChain();
+        $chain[0] = new \stdClass();
+    }
+
+    /**
+     * @group disconnected
+     */
+    public function testGetIterator()
+    {
+        $processors = array(
+            $this->getMock('Predis\Command\Processor\ProcessorInterface'),
+            $this->getMock('Predis\Command\Processor\ProcessorInterface'),
+        );
+
+        $chain = new ProcessorChain($processors);
+
+        $this->assertSame($processors, iterator_to_array($chain->getIterator()));
+    }
+
     /**
      * @group disconnected
      */

+ 11 - 0
tests/Predis/Command/RawCommandTest.php

@@ -101,6 +101,17 @@ class RawCommandTest extends PredisTestCase
         $this->assertSame($commandArgs, $command->getArguments());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testGetArgumentAtIndex()
+    {
+        $command = new RawCommand('GET', array('key'));
+
+        $this->assertSame('key', $command->getArgument(0));
+        $this->assertNull($command->getArgument(1));
+    }
+
     /**
      * @group disconnected
      */

+ 14 - 0
tests/Predis/Command/Redis/SLOWLOG_Test.php

@@ -51,6 +51,8 @@ class SLOWLOG_Test extends PredisCommandTestCase
     }
 
     /**
+     * This is the response type for SLOWLOG GET
+     *
      * @group disconnected
      */
     public function testParseResponse()
@@ -70,6 +72,18 @@ class SLOWLOG_Test extends PredisCommandTestCase
         $this->assertSame($expected, $command->parseResponse($raw));
     }
 
+    /**
+     * This is the response type for SLOWLOG LEN
+     *
+     * @group disconnected
+     */
+    public function testParseResponseInteger()
+    {
+        $command = $this->getCommand();
+
+        $this->assertSame(10, $command->parseResponse(10));
+    }
+
     /**
      * @group connected
      * @requiresRedisVersion >= 2.2.12