Browse Source

Do not parse response to EXISTS into boolean value.

Starting with Redis 3.0.3 the EXISTS command is variadic so that it is
possible to check for the existence of multiple keys in one request,
with the server returning the number of keys found.

This change could break codebases relying on strict comparison (===)
against a boolean value, but just doing $redis->exists('key') == TRUE
is totally fine.
Daniele Alessandri 9 years ago
parent
commit
ab20c52115

+ 0 - 8
src/Command/KeyExists.php

@@ -24,12 +24,4 @@ class KeyExists extends Command
     {
         return 'EXISTS';
     }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function parseResponse($data)
-    {
-        return (bool) $data;
-    }
 }

+ 1 - 1
tests/Predis/Command/ConnectionSelectTest.php

@@ -65,7 +65,7 @@ class ConnectionSelectTest extends PredisCommandTestCase
         $redis->set('foo', 'bar');
 
         $this->assertEquals('OK', $redis->select(REDIS_SERVER_DBNUM - 1));
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 
     /**

+ 44 - 6
tests/Predis/Command/KeyExistsTest.php

@@ -47,6 +47,20 @@ class KeyExistsTest extends PredisCommandTestCase
         $this->assertSame($expected, $command->getArguments());
     }
 
+    /**
+     * @group disconnected
+     */
+    public function testFilterArgumentsMultipleKeys()
+    {
+        $arguments = array('key:1', 'key:2', 'key:3');
+        $expected = array('key:1', 'key:2', 'key:3');
+
+        $command = $this->getCommand();
+        $command->setArguments($arguments);
+
+        $this->assertSame($expected, $command->getArguments());
+    }
+
     /**
      * @group disconnected
      */
@@ -54,28 +68,52 @@ class KeyExistsTest extends PredisCommandTestCase
     {
         $command = $this->getCommand();
 
-        $this->assertTrue($command->parseResponse(1));
-        $this->assertFalse($command->parseResponse(0));
+        $this->assertSame(0, $command->parseResponse(0));
+        $this->assertSame(1, $command->parseResponse(1));
+        $this->assertSame(2, $command->parseResponse(2));
     }
 
     /**
      * @group connected
      */
-    public function testReturnsTrueIfKeyExists()
+    public function testReturnValueWhenKeyExists()
     {
         $redis = $this->getClient();
 
         $redis->set('foo', 'bar');
-        $this->assertTrue($redis->exists('foo'));
+        $this->assertSame(1, $redis->exists('foo'));
+    }
+
+    /**
+     * @group connected
+     */
+    public function testReturnValueWhenKeyDoesNotExist()
+    {
+        $redis = $this->getClient();
+
+        $this->assertSame(0, $redis->exists('foo'));
+    }
+
+    /**
+     * @group connected
+     * @expectedRedisVersion >= 3.0.3
+     */
+    public function testReturnValueWhenKeysExist()
+    {
+        $redis = $this->getClient();
+
+        $redis->mset('foo', 'bar', 'hoge', 'piyo');
+        $this->assertSame(2, $redis->exists('foo', 'hoge'));
     }
 
     /**
      * @group connected
+     * @expectedRedisVersion >= 3.0.3
      */
-    public function testReturnsFalseIfKeyDoesNotExist()
+    public function testReturnValueWhenKeyDoNotExist()
     {
         $redis = $this->getClient();
 
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo', 'bar'));
     }
 }

+ 2 - 2
tests/Predis/Command/KeyExpireAtTest.php

@@ -85,7 +85,7 @@ class KeyExpireAtTest extends PredisCommandTestCase
 
         $this->sleep(2);
 
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 
     /**
@@ -99,6 +99,6 @@ class KeyExpireAtTest extends PredisCommandTestCase
         $redis->set('foo', 'bar');
 
         $this->assertTrue($redis->expireat('foo', $now - 100));
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 }

+ 2 - 2
tests/Predis/Command/KeyExpireTest.php

@@ -84,7 +84,7 @@ class KeyExpireTest extends PredisCommandTestCase
 
         $this->sleep(2.0);
 
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 
     /**
@@ -113,6 +113,6 @@ class KeyExpireTest extends PredisCommandTestCase
         $redis->set('foo', 'bar');
 
         $this->assertTrue($redis->expire('foo', -10));
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 }

+ 2 - 2
tests/Predis/Command/KeyMoveTest.php

@@ -70,10 +70,10 @@ class KeyMoveTest extends PredisCommandTestCase
         $redis->set('foo', 'bar');
 
         $this->assertTrue($redis->move('foo', $db));
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
 
         $redis->select($db);
-        $this->assertTrue($redis->exists('foo'));
+        $this->assertSame(1, $redis->exists('foo'));
 
         $redis->del('foo');
     }

+ 2 - 2
tests/Predis/Command/KeyPreciseExpireAtTest.php

@@ -75,7 +75,7 @@ class KeyPreciseExpireAtTest extends PredisCommandTestCase
 
         $this->sleep($ttl + 0.5);
 
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 
     /**
@@ -88,6 +88,6 @@ class KeyPreciseExpireAtTest extends PredisCommandTestCase
         $redis->set('foo', 'bar');
 
         $this->assertTrue($redis->expireat('foo', time() - 100000));
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 }

+ 2 - 2
tests/Predis/Command/KeyPreciseExpireTest.php

@@ -83,7 +83,7 @@ class KeyPreciseExpireTest extends PredisCommandTestCase
         $this->assertTrue($redis->pexpire('foo', $ttl));
 
         $this->sleep(1.2);
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 
     /**
@@ -114,6 +114,6 @@ class KeyPreciseExpireTest extends PredisCommandTestCase
         $redis->set('foo', 'bar');
 
         $this->assertTrue($redis->pexpire('foo', -10000));
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 }

+ 2 - 2
tests/Predis/Command/KeyRenamePreserveTest.php

@@ -66,8 +66,8 @@ class KeyRenamePreserveTest extends PredisCommandTestCase
         $redis->set('foo', 'bar');
 
         $this->assertTrue($redis->renamenx('foo', 'foofoo'));
-        $this->assertFalse($redis->exists('foo'));
-        $this->assertTrue($redis->exists('foofoo'));
+        $this->assertSame(0, $redis->exists('foo'));
+        $this->assertSame(1, $redis->exists('foofoo'));
     }
 
     /**

+ 2 - 2
tests/Predis/Command/KeyRenameTest.php

@@ -65,8 +65,8 @@ class KeyRenameTest extends PredisCommandTestCase
         $redis->set('foo', 'bar');
 
         $this->assertEquals('OK', $redis->rename('foo', 'foofoo'));
-        $this->assertFalse($redis->exists('foo'));
-        $this->assertTrue($redis->exists('foofoo'));
+        $this->assertSame(0, $redis->exists('foo'));
+        $this->assertSame(1, $redis->exists('foofoo'));
     }
 
     /**

+ 1 - 1
tests/Predis/Command/ListPushHeadXTest.php

@@ -77,7 +77,7 @@ class ListPushHeadXTest extends PredisCommandTestCase
 
         $this->assertSame(0, $redis->lpushx('metavars', 'foo'));
         $this->assertSame(0, $redis->lpushx('metavars', 'hoge'));
-        $this->assertFalse($redis->exists('metavars'));
+        $this->assertSame(0, $redis->exists('metavars'));
     }
 
     /**

+ 1 - 1
tests/Predis/Command/ListPushTailXTest.php

@@ -77,7 +77,7 @@ class ListPushTailXTest extends PredisCommandTestCase
 
         $this->assertSame(0, $redis->rpushx('metavars', 'foo'));
         $this->assertSame(0, $redis->rpushx('metavars', 'hoge'));
-        $this->assertFalse($redis->exists('metavars'));
+        $this->assertSame(0, $redis->exists('metavars'));
     }
 
     /**

+ 1 - 1
tests/Predis/Command/ServerFlushDatabaseTest.php

@@ -62,6 +62,6 @@ class ServerFlushDatabaseTest extends PredisCommandTestCase
         $redis->set('foo', 'bar');
 
         $this->assertEquals('OK', $redis->flushdb());
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 }

+ 1 - 1
tests/Predis/Command/SetIntersectionStoreTest.php

@@ -92,7 +92,7 @@ class SetIntersectionStoreTest extends PredisCommandTestCase
         $redis->sadd('letters:1st', 'a', 'b', 'c', 'd', 'e', 'f', 'g');
 
         $this->assertSame(0, $redis->sinterstore('letters:destination', 'letters:1st', 'letters:2nd'));
-        $this->assertFalse($redis->exists('letters:destination'));
+        $this->assertSame(0, $redis->exists('letters:destination'));
     }
 
     /**

+ 2 - 2
tests/Predis/Command/StringGetTest.php

@@ -75,7 +75,7 @@ class StringGetTest extends PredisCommandTestCase
 
         $redis->set('foo', '');
 
-        $this->assertTrue($redis->exists('foo'));
+        $this->assertSame(1, $redis->exists('foo'));
         $this->assertSame('', $redis->get('foo'));
     }
 
@@ -86,7 +86,7 @@ class StringGetTest extends PredisCommandTestCase
     {
         $redis = $this->getClient();
 
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
         $this->assertNull($redis->get('foo'));
     }
 

+ 2 - 2
tests/Predis/Command/StringPreciseSetExpireTest.php

@@ -63,7 +63,7 @@ class StringPreciseSetExpireTest extends PredisCommandTestCase
         $redis = $this->getClient();
 
         $this->assertEquals('OK', $redis->psetex('foo', 10000, 'bar'));
-        $this->assertTrue($redis->exists('foo'));
+        $this->assertSame(1, $redis->exists('foo'));
         $this->assertEquals(10, $redis->ttl('foo'));
     }
 
@@ -77,7 +77,7 @@ class StringPreciseSetExpireTest extends PredisCommandTestCase
 
         $redis->psetex('foo', 50, 'bar');
         $this->sleep(0.5);
-        $this->assertFalse($redis->exists('foo'));;
+        $this->assertSame(0, $redis->exists('foo'));;
     }
 
     /**

+ 2 - 2
tests/Predis/Command/StringSetExpireTest.php

@@ -63,7 +63,7 @@ class StringSetExpireTest extends PredisCommandTestCase
         $redis = $this->getClient();
 
         $this->assertEquals('OK', $redis->setex('foo', 10, 'bar'));
-        $this->assertTrue($redis->exists('foo'));
+        $this->assertSame(1, $redis->exists('foo'));
         $this->assertEquals(10, $redis->ttl('foo'));
     }
 
@@ -78,7 +78,7 @@ class StringSetExpireTest extends PredisCommandTestCase
 
         $redis->setex('foo', 1, 'bar');
         $this->sleep(2.0);
-        $this->assertFalse($redis->exists('foo'));;
+        $this->assertSame(0, $redis->exists('foo'));;
     }
 
     /**

+ 1 - 1
tests/Predis/Command/StringSetMultiplePreserveTest.php

@@ -92,6 +92,6 @@ class StringSetMultiplePreserveTest extends PredisCommandTestCase
 
         $this->assertFalse($redis->msetnx('foo', 'barbar', 'hoge', 'piyo'));
         $this->assertSame('bar', $redis->get('foo'));
-        $this->assertFalse($redis->exists('hoge'));
+        $this->assertSame(0, $redis->exists('hoge'));
     }
 }

+ 1 - 1
tests/Predis/Command/StringSetTest.php

@@ -63,7 +63,7 @@ class StringSetTest extends PredisCommandTestCase
         $redis = $this->getClient();
 
         $this->assertEquals('OK', $redis->set('foo', 'bar'));
-        $this->assertTrue($redis->exists('foo'));
+        $this->assertSame(1, $redis->exists('foo'));
         $this->assertSame('bar', $redis->get('foo'));
     }
 }

+ 1 - 1
tests/Predis/Command/TransactionDiscardTest.php

@@ -63,7 +63,7 @@ class TransactionDiscardTest extends PredisCommandTestCase
 
         $this->assertEquals('QUEUED', $redis->set('foo', 'bar'));
         $this->assertEquals('OK', $redis->discard());
-        $this->assertFalse($redis->exists('foo'));
+        $this->assertSame(0, $redis->exists('foo'));
     }
 
     /**

+ 5 - 5
tests/Predis/Pipeline/PipelineTest.php

@@ -390,7 +390,7 @@ class PipelineTest extends PredisTestCase
         });
 
         $this->assertEquals(array('OK', 'bar'), $results);
-        $this->assertTrue($client->exists('foo'));
+        $this->assertSame(1, $client->exists('foo'));
     }
 
     /**
@@ -409,7 +409,7 @@ class PipelineTest extends PredisTestCase
 
         $this->assertEquals(array('OK', 'bar'), $results);
         $this->assertSame('oob message', $oob);
-        $this->assertTrue($client->exists('foo'));
+        $this->assertSame(1, $client->exists('foo'));
     }
 
     /**
@@ -432,7 +432,7 @@ class PipelineTest extends PredisTestCase
 
         $this->assertInstanceOf('Predis\ClientException', $exception);
         $this->assertSame('TEST', $exception->getMessage());
-        $this->assertFalse($client->exists('foo'));
+        $this->assertSame(0, $client->exists('foo'));
     }
 
     /**
@@ -457,8 +457,8 @@ class PipelineTest extends PredisTestCase
         }
 
         $this->assertInstanceOf('Predis\Response\ServerException', $exception);
-        $this->assertTrue($client->exists('foo'));
-        $this->assertTrue($client->exists('hoge'));
+        $this->assertSame(1, $client->exists('foo'));
+        $this->assertSame(1, $client->exists('hoge'));
     }
 
     /**

+ 3 - 3
tests/Predis/Transaction/MultiExecTest.php

@@ -662,7 +662,7 @@ class MultiExecTest extends PredisTestCase
         }
 
         $this->assertInstanceOf('RuntimeException', $exception);
-        $this->assertFalse($client->exists('foo'));
+        $this->assertSame(0, $client->exists('foo'));
     }
 
     /**
@@ -720,8 +720,8 @@ class MultiExecTest extends PredisTestCase
         });
 
         $this->assertSame(1, count($responses));
-        $this->assertFalse($client->exists('foo'));
-        $this->assertTrue($client->exists('hoge'));
+        $this->assertSame(0, $client->exists('foo'));
+        $this->assertSame(1, $client->exists('hoge'));
     }
 
     /**