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.
This commit is contained in:
Daniele Alessandri
2015-07-24 14:59:23 +02:00
parent 2eaf3d8595
commit ab20c52115
22 changed files with 80 additions and 50 deletions
-8
View File
@@ -24,12 +24,4 @@ class KeyExists extends Command
{
return 'EXISTS';
}
/**
* {@inheritdoc}
*/
public function parseResponse($data)
{
return (bool) $data;
}
}
@@ -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
View File
@@ -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 testReturnsFalseIfKeyDoesNotExist()
public function testReturnValueWhenKeyDoesNotExist()
{
$redis = $this->getClient();
$this->assertFalse($redis->exists('foo'));
$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 testReturnValueWhenKeyDoNotExist()
{
$redis = $this->getClient();
$this->assertSame(0, $redis->exists('foo', 'bar'));
}
}
+2 -2
View File
@@ -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
View File
@@ -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
View File
@@ -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');
}
@@ -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'));
}
}
@@ -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'));
}
}
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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'));
}
/**
@@ -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'));
}
}
@@ -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
View File
@@ -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'));
}
@@ -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
View File
@@ -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'));;
}
/**
@@ -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
View File
@@ -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'));
}
}
@@ -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
View File
@@ -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
View File
@@ -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'));
}
/**