Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c125b0

Browse files
More unit test cleanup.
* Tighten up `assertTrue` and `assertFalse` such that they test that passed arguments `===` `true` and `===` `false` respectively, instead of testing for truth-like or false-like. * Start modernizing our unit tests to use explicit types for arguments, return types, member variables, etc. * Multiple assertion fixes that were exposed when making `assertTrue` and `assertFalse` more explicit. * Some formatting cleanup to style for incorrect indentation, etc, that had crept in over many years. * Add some more assertion helpers like `assertNull`, `assertGT`, `assertGTE`, `assertLT`, and `assertLTE`.
1 parent 18b0da7 commit 3c125b0

5 files changed

Lines changed: 535 additions & 614 deletions

File tree

tests/RedisArrayTest.php

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Redis_Array_Test extends TestSuite
4949
public function setUp() {
5050
// initialize strings.
5151
$n = REDIS_ARRAY_DATA_SIZE;
52-
$this->strings = array();
52+
$this->strings = [];
5353
for($i = 0; $i < $n; $i++) {
5454
$this->strings['key-'.$i] = 'val-'.$i;
5555
}
@@ -66,11 +66,11 @@ public function setUp() {
6666

6767
public function testMSet() {
6868
// run mset
69-
$this->assertTrue(TRUE === $this->ra->mset($this->strings));
69+
$this->assertTrue($this->ra->mset($this->strings));
7070

7171
// check each key individually using the array
7272
foreach($this->strings as $k => $v) {
73-
$this->assertTrue($v === $this->ra->get($k));
73+
$this->assertEquals($v, $this->ra->get($k));
7474
}
7575

7676
// check each key individually using a new connection
@@ -88,16 +88,16 @@ public function testMSet() {
8888
if ($this->getAuth()) {
8989
$this->assertTrue($r->auth($this->getAuth()));
9090
}
91-
$this->assertTrue($v === $r->get($k));
91+
$this->assertEquals($v, $r->get($k));
9292
}
9393
}
9494

9595
public function testMGet() {
96-
$this->assertTrue(array_values($this->strings) === $this->ra->mget(array_keys($this->strings)));
96+
$this->assertEquals(array_values($this->strings), $this->ra->mget(array_keys($this->strings)));
9797
}
9898

9999
private function addData($commonString) {
100-
$this->data = array();
100+
$this->data = [];
101101
for($i = 0; $i < REDIS_ARRAY_DATA_SIZE; $i++) {
102102
$k = rand().'_'.$commonString.'_'.rand();
103103
$this->data[$k] = rand();
@@ -111,9 +111,9 @@ private function checkCommonLocality() {
111111
foreach($this->data as $k => $v) {
112112
$node = $this->ra->_target($k);
113113
if($lastNode) {
114-
$this->assertTrue($node === $lastNode);
114+
$this->assertEquals($node, $lastNode);
115115
}
116-
$this->assertTrue($this->ra->get($k) == $v);
116+
$this->assertEqualsWeak($v, $this->ra->get($k));
117117
$lastNode = $node;
118118
}
119119
}
@@ -163,7 +163,7 @@ public function testKeyDistributor()
163163
foreach($this->data as $k => $v) {
164164
$node = $this->ra->_target($k);
165165
$pos = $this->customDistributor($k);
166-
$this->assertTrue($node === $newRing[$pos]);
166+
$this->assertEquals($node, $newRing[$pos]);
167167
}
168168
}
169169

@@ -225,7 +225,7 @@ public function setUp() {
225225

226226
// initialize strings.
227227
$n = REDIS_ARRAY_DATA_SIZE;
228-
$this->strings = array();
228+
$this->strings = [];
229229
for($i = 0; $i < $n; $i++) {
230230
$this->strings['key-'.$i] = 'val-'.$i;
231231
}
@@ -245,13 +245,13 @@ public function setUp() {
245245
// initialize hashes
246246
for($i = 0; $i < $n; $i++) {
247247
// each hash has 5 keys
248-
$this->hashes['hash-'.$i] = array('A' => $i, 'B' => $i+1, 'C' => $i+2, 'D' => $i+3, 'E' => $i+4);
248+
$this->hashes['hash-'.$i] = ['A' => $i, 'B' => $i+1, 'C' => $i+2, 'D' => $i+3, 'E' => $i+4];
249249
}
250250

251251
// initialize sorted sets
252252
for($i = 0; $i < $n; $i++) {
253253
// each sorted sets has 5 elements
254-
$this->zsets['zset-'.$i] = array($i, 'A', $i+1, 'B', $i+2, 'C', $i+3, 'D', $i+4, 'E');
254+
$this->zsets['zset-'.$i] = [$i, 'A', $i+1, 'B', $i+2, 'C', $i+3, 'D', $i+4, 'E'];
255255
}
256256

257257
global $newRing, $oldRing, $useIndex;
@@ -289,12 +289,12 @@ private function distributeKeys() {
289289

290290
// sets
291291
foreach($this->sets as $k => $v) {
292-
call_user_func_array(array($this->ra, 'sadd'), array_merge(array($k), $v));
292+
call_user_func_array([$this->ra, 'sadd'], array_merge([$k], $v));
293293
}
294294

295295
// lists
296296
foreach($this->lists as $k => $v) {
297-
call_user_func_array(array($this->ra, 'rpush'), array_merge(array($k), $v));
297+
call_user_func_array([$this->ra, 'rpush'], array_merge([$k], $v));
298298
}
299299

300300
// hashes
@@ -304,7 +304,7 @@ private function distributeKeys() {
304304

305305
// sorted sets
306306
foreach($this->zsets as $k => $v) {
307-
call_user_func_array(array($this->ra, 'zadd'), array_merge(array($k), $v));
307+
call_user_func_array([$this->ra, 'zadd'], array_merge([$k], $v));
308308
}
309309
}
310310

@@ -320,7 +320,7 @@ private function readAllvalues() {
320320

321321
// strings
322322
foreach($this->strings as $k => $v) {
323-
$this->assertTrue($this->ra->get($k) === $v);
323+
$this->assertEquals($v, $this->ra->get($k));
324324
}
325325

326326
// sets
@@ -351,7 +351,7 @@ private function readAllvalues() {
351351
$ret = $this->ra->zrange($k, 0, -1, TRUE); // get values with scores
352352

353353
// create assoc array from local dataset
354-
$tmp = array();
354+
$tmp = [];
355355
for($i = 0; $i < count($v); $i += 2) {
356356
$tmp[$v[$i+1]] = $v[$i];
357357
}
@@ -402,7 +402,7 @@ class Redis_Auto_Rehashing_Test extends TestSuite {
402402
public function setUp() {
403403
// initialize strings.
404404
$n = REDIS_ARRAY_DATA_SIZE;
405-
$this->strings = array();
405+
$this->strings = [];
406406
for($i = 0; $i < $n; $i++) {
407407
$this->strings['key-'.$i] = 'val-'.$i;
408408
}
@@ -426,7 +426,7 @@ public function testDistribute() {
426426

427427
private function readAllvalues() {
428428
foreach($this->strings as $k => $v) {
429-
$this->assertTrue($this->ra->get($k) === $v);
429+
$this->assertEquals($v, $this->ra->get($k));
430430
}
431431
}
432432

@@ -458,7 +458,7 @@ public function testAllKeysHaveBeenMigrated() {
458458
$this->assertTrue($r->auth($this->getAuth()));
459459
}
460460

461-
$this->assertTrue($v === $r->get($k)); // check that the key has actually been migrated to the new node.
461+
$this->assertEquals($v, $r->get($k)); // check that the key has actually been migrated to the new node.
462462
}
463463
}
464464
}
@@ -491,10 +491,10 @@ public function testInit() {
491491
public function testKeyDistribution() {
492492
// check that all of joe's keys are on the same instance
493493
$lastNode = NULL;
494-
foreach(array('name', 'group', 'salary') as $field) {
494+
foreach(['name', 'group', 'salary'] as $field) {
495495
$node = $this->ra->_target('1_{employee:joe}_'.$field);
496496
if($lastNode) {
497-
$this->assertTrue($node === $lastNode);
497+
$this->assertEquals($node, $lastNode);
498498
}
499499
$lastNode = $node;
500500
}
@@ -514,8 +514,8 @@ public function testMultiExec() {
514514
->exec();
515515

516516
// check that the group and salary have been changed
517-
$this->assertTrue($this->ra->get('1_{employee:joe}_group') === $newGroup);
518-
$this->assertTrue($this->ra->get('1_{employee:joe}_salary') == $newSalary);
517+
$this->assertEquals($newGroup, $this->ra->get('1_{employee:joe}_group'));
518+
$this->assertEqualsWeak($newSalary, $this->ra->get('1_{employee:joe}_salary'));
519519

520520
}
521521

@@ -527,10 +527,10 @@ public function testMultiExecMSet() {
527527

528528
// test MSET, making Joe a top-level executive
529529
$out = $this->ra->multi($this->ra->_target('{employee:joe}'))
530-
->mset(array('1_{employee:joe}_group' => $newGroup, '1_{employee:joe}_salary' => $newSalary))
530+
->mset(['1_{employee:joe}_group' => $newGroup, '1_{employee:joe}_salary' => $newSalary])
531531
->exec();
532532

533-
$this->assertTrue($out[0] === TRUE);
533+
$this->assertTrue($out[0]);
534534
}
535535

536536
public function testMultiExecMGet() {
@@ -539,7 +539,7 @@ public function testMultiExecMGet() {
539539

540540
// test MGET
541541
$out = $this->ra->multi($this->ra->_target('{employee:joe}'))
542-
->mget(array('1_{employee:joe}_group', '1_{employee:joe}_salary'))
542+
->mget(['1_{employee:joe}_group', '1_{employee:joe}_salary'])
543543
->exec();
544544

545545
$this->assertTrue($out[0][0] == $newGroup);
@@ -553,7 +553,7 @@ public function testMultiExecDel() {
553553
->del('1_{employee:joe}_group', '1_{employee:joe}_salary')
554554
->exec();
555555

556-
$this->assertTrue($out[0] === 2);
556+
$this->assertEquals(2, $out[0]);
557557
$this->assertEquals(0, $this->ra->exists('1_{employee:joe}_group'));
558558
$this->assertEquals(0, $this->ra->exists('1_{employee:joe}_salary'));
559559
}
@@ -570,39 +570,39 @@ public function testMultiExecUnlink() {
570570
->del('{unlink}:key1', '{unlink}:key2')
571571
->exec();
572572

573-
$this->assertTrue($out[0] === 2);
573+
$this->assertEquals(2, $out[0]);
574574
}
575575

576576
public function testDiscard() {
577577
/* phpredis issue #87 */
578578
$key = 'test_err';
579579

580580
$this->assertTrue($this->ra->set($key, 'test'));
581-
$this->assertTrue('test' === $this->ra->get($key));
581+
$this->assertEquals('test', $this->ra->get($key));
582582

583583
$this->ra->watch($key);
584584

585585
// After watch, same
586-
$this->assertTrue('test' === $this->ra->get($key));
586+
$this->assertEquals('test', $this->ra->get($key));
587587

588588
// change in a multi/exec block.
589589
$ret = $this->ra->multi($this->ra->_target($key))->set($key, 'test1')->exec();
590-
$this->assertTrue($ret === array(true));
590+
$this->assertEquals([true], $ret);
591591

592592
// Get after exec, 'test1':
593-
$this->assertTrue($this->ra->get($key) === 'test1');
593+
$this->assertEquals('test1', $this->ra->get($key));
594594

595595
$this->ra->watch($key);
596596

597597
// After second watch, still test1.
598-
$this->assertTrue($this->ra->get($key) === 'test1');
598+
$this->assertEquals('test1', $this->ra->get($key));
599599

600600
$ret = $this->ra->multi($this->ra->_target($key))->set($key, 'test2')->discard();
601601
// Ret after discard: NULL";
602-
$this->assertTrue($ret === NULL);
602+
$this->assertNull($ret);
603603

604604
// Get after discard, unchanged:
605-
$this->assertTrue($this->ra->get($key) === 'test1');
605+
$this->assertEquals('test1', $this->ra->get($key));
606606
}
607607
}
608608

@@ -629,9 +629,9 @@ public function testInit() {
629629
}
630630

631631
public function distribute($key) {
632-
$matches = array();
632+
$matches = [];
633633
if (preg_match('/{([^}]+)}.*/', $key, $matches) == 1) {
634-
$countries = array('uk' => 0, 'us' => 1);
634+
$countries = ['uk' => 0, 'us' => 1];
635635
if (array_key_exists($matches[1], $countries)) {
636636
return $countries[$matches[1]];
637637
}
@@ -646,10 +646,10 @@ public function testDistribution() {
646646
$defaultServer = $this->ra->_target('unknown');
647647

648648
$nodes = $this->ra->_hosts();
649-
$this->assertTrue($ukServer === $nodes[0]);
650-
$this->assertTrue($usServer === $nodes[1]);
651-
$this->assertTrue($deServer === $nodes[2]);
652-
$this->assertTrue($defaultServer === $nodes[2]);
649+
$this->assertEquals($ukServer, $nodes[0]);
650+
$this->assertEquals($usServer,$nodes[1]);
651+
$this->assertEquals($deServer,$nodes[2]);
652+
$this->assertEquals($defaultServer, $nodes[2]);
653653
}
654654
}
655655

0 commit comments

Comments
 (0)