Skip to content

Commit bef5c9d

Browse files
committed
Fixed HSET, added more tests.
1 parent 7811cbd commit bef5c9d

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,14 +1396,14 @@ Adds a value to the hash stored at key. If this value is already in the hash, `F
13961396
*value*
13971397

13981398
##### *Return value*
1399-
*BOOL* `TRUE` if value didn't exist and was added successfully, `FALSE` if the value was already present and was replaced.
1399+
*LONG* `1` if value didn't exist and was added successfully, `0` if the value was already present and was replaced, `FALSE` if there was an error.
14001400
##### *Example*
14011401
<pre>
14021402
$redis->delete('h')
1403-
$redis->hSet('h', 'key1', 'hello'); /* TRUE, 'key1' => 'hello' in the hash at "h" */
1403+
$redis->hSet('h', 'key1', 'hello'); /* 1, 'key1' => 'hello' in the hash at "h" */
14041404
$redis->hGet('h', 'key1'); /* returns "hello" */
14051405

1406-
$redis->hSet('h', 'key1', 'plop'); /* FALSE, value was replaced. */
1406+
$redis->hSet('h', 'key1', 'plop'); /* 0, value was replaced. */
14071407
$redis->hGet('h', 'key1'); /* returns "plop" */
14081408
</pre>
14091409

redis.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,9 +3355,9 @@ PHP_METHOD(Redis, hSet)
33553355

33563356
REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
33573357
IF_ATOMIC() {
3358-
redis_1_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL TSRMLS_CC);
3358+
redis_long_response(INTERNAL_FUNCTION_PARAM_PASSTHRU, redis_sock, NULL TSRMLS_CC);
33593359
}
3360-
REDIS_PROCESS_RESPONSE(redis_1_response);
3360+
REDIS_PROCESS_RESPONSE(redis_long_response);
33613361
}
33623362
/* }}} */
33633363

tests/TestRedis.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,15 +1457,15 @@ public function testHashes() {
14571457
$this->redis->delete('h', 'key');
14581458

14591459
$this->assertTrue(0 === $this->redis->hLen('h'));
1460-
$this->assertTrue(TRUE === $this->redis->hSet('h', 'a', 'a-value'));
1460+
$this->assertTrue(1 === $this->redis->hSet('h', 'a', 'a-value'));
14611461
$this->assertTrue(1 === $this->redis->hLen('h'));
1462-
$this->assertTrue(TRUE === $this->redis->hSet('h', 'b', 'b-value'));
1462+
$this->assertTrue(1 === $this->redis->hSet('h', 'b', 'b-value'));
14631463
$this->assertTrue(2 === $this->redis->hLen('h'));
14641464

14651465
$this->assertTrue('a-value' === $this->redis->hGet('h', 'a')); // simple get
14661466
$this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get
14671467

1468-
$this->assertTrue(FALSE === $this->redis->hSet('h', 'a', 'another-value')); // replacement
1468+
$this->assertTrue(0 === $this->redis->hSet('h', 'a', 'another-value')); // replacement
14691469
$this->assertTrue('another-value' === $this->redis->hGet('h', 'a')); // get the new value
14701470

14711471
$this->assertTrue('b-value' === $this->redis->hGet('h', 'b')); // simple get
@@ -1508,7 +1508,6 @@ public function testHashes() {
15081508
$this->assertTrue(FALSE === $this->redis->hIncrBy('h', 'y', 1));
15091509
}
15101510

1511-
15121511
public function testMultiExec() {
15131512
$this->sequence(Redis::MULTI);
15141513
}
@@ -1997,24 +1996,35 @@ protected function sequence($mode) {
19971996
->hexists('hkey1', 'key2')
19981997
->hkeys('hkey1')
19991998
->hvals('hkey1')
2000-
// ->hgetall('hkey1')
2001-
//->hincrby()
1999+
->hgetall('hkey1')
2000+
->hset('hkey1', 'valn', 1)
2001+
->hincrby('hkey1', 'valn', 4)
2002+
->hincrby('hkey1', 'val-fail', 42)
2003+
->hset('hkey1', 'val-fail', 'non-string')
2004+
->hget('hkey1', 'val-fail')
2005+
->hincrby('hkey1', 'val-fail', 42)
20022006
->exec();
20032007

20042008
$i = 0;
20052009
$this->assertTrue(is_array($ret));
20062010
$this->assertTrue($ret[$i++] <= 1); // delete
2007-
$this->assertTrue($ret[$i++] === TRUE); // added 1 element
2008-
$this->assertTrue($ret[$i++] === TRUE); // added 1 element
2009-
$this->assertTrue($ret[$i++] === TRUE); // added 1 element
2011+
$this->assertTrue($ret[$i++] === 1); // added 1 element
2012+
$this->assertTrue($ret[$i++] === 1); // added 1 element
2013+
$this->assertTrue($ret[$i++] === 1); // added 1 element
20102014
$this->assertTrue($ret[$i++] === 'value1'); // hget
20112015
$this->assertTrue($ret[$i++] === 3); // hlen
20122016
$this->assertTrue($ret[$i++] === TRUE); // hdel succeeded
20132017
$this->assertTrue($ret[$i++] === FALSE); // hdel failed
20142018
$this->assertTrue($ret[$i++] === FALSE); // hexists didn't find the deleted key
20152019
$this->assertTrue($ret[$i] === array('key1', 'key3') || $ret[$i] === array('key3', 'key1')); $i++; // hkeys
20162020
$this->assertTrue($ret[$i] === array('value1', 'value3') || $ret[$i] === array('value3', 'value1')); $i++; // hvals
2017-
//$this->assertTrue($ret[$i] === array('key1' => 'value1', 'key3' => 'value3') || $ret[$i] === array('key3' => 'value3', 'key1' => 'value1')); $i++; // hgetall
2021+
$this->assertTrue($ret[$i] === array('key1' => 'value1', 'key3' => 'value3') || $ret[$i] === array('key3' => 'value3', 'key1' => 'value1')); $i++; // hgetall
2022+
$this->assertTrue($ret[$i++] === 1); // added 1 element
2023+
$this->assertTrue($ret[$i++] === 5); // added 4 to value 1 → 5
2024+
$this->assertTrue($ret[$i++] === 42); // member doesn't exist → assume 0, and then add.
2025+
$this->assertTrue($ret[$i++] === 0); // didn't add the element, already present, so 0.
2026+
$this->assertTrue($ret[$i++] === 'non-string'); // hset succeeded
2027+
$this->assertTrue($ret[$i++] === FALSE); // member isn't a number → fail.
20182028
$this->assertTrue(count($ret) === $i);
20192029

20202030
}

0 commit comments

Comments
 (0)