Visitar URL original
tests: Remove any hardcoded class creation · phpredis/phpredis@a5346b8 · GitHub
Skip to content

Commit a5346b8

Browse files
tests: Remove any hardcoded class creation
It's possible to run these tests with any phpredis compatible classes but there were a few places we still hardcoded the class creation. This just removes all hardcoded classes in favor of overridable methods
1 parent 57b584f commit a5346b8

5 files changed

Lines changed: 54 additions & 27 deletions

File tree

‎tests/RedisArrayTest.php‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function testNotSerializable() {
6868
}
6969

7070
public function testMSet() {
71+
$class = $this->getRedisClass();
7172
// run mset
7273
$this->assertTrue($this->ra->mset($this->strings));
7374

@@ -82,7 +83,7 @@ public function testMSet() {
8283
$node = $this->ra->_target($k);
8384
if ( ! isset($clients[$node])) {
8485
parseHostPort($node, $host, $port);
85-
$clients[$node] = new Redis;
86+
$clients[$node] = new $class;
8687
$clients[$node]->pconnect($host, (int)$port);
8788
if ($this->getAuth()) {
8889
$this->assertTrue($clients[$node]->auth($this->getAuth()));
@@ -280,12 +281,13 @@ public function setUp() {
280281
}
281282

282283
public function testFlush() {
284+
$class = $this->getRedisClass();
283285
// flush all servers first.
284286
global $server_list;
285287
foreach ($server_list as $s) {
286288
parseHostPort($s, $host, $port);
287289

288-
$r = new Redis();
290+
$r = new $class();
289291
$r->pconnect($host, (int)$port, 0);
290292
if ($this->getAuth()) {
291293
$this->assertTrue($r->auth($this->getAuth()));
@@ -461,12 +463,13 @@ public function testReadAndMigrateAll() {
461463

462464
// Read and migrate keys on fallback, causing the whole ring to be rehashed.
463465
public function testAllKeysHaveBeenMigrated() {
466+
$class = $this->getRedisClass();
464467
$clients = [];
465468
foreach ($this->strings as $k => $v) {
466469
$node = $this->ra->_target($k);
467470
if ( ! isset($clients[$node])) {
468471
parseHostPort($node, $host, $port);
469-
$clients[$node] = new Redis;
472+
$clients[$node] = new $class;
470473
$clients[$node]->pconnect($host, (int)$port);
471474
if ($this->getAuth()) {
472475
$this->assertTrue($clients[$node]->auth($this->getAuth()));

‎tests/RedisClusterTest.php‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ public function testSession_lockWaitTime() { $this->markTestSkipped(); }
7272

7373
/* Regression test for GH #2810 */
7474
public function testConstructNullSeeds() {
75+
$class = $this->getRedisClusterClass();
7576
/* new RedisCluster(null, null) must not throw TypeError.
7677
* $seeds is declared ?array so null is a valid argument. */
7778
$thrown = false;
7879
try {
79-
new RedisCluster(null, null);
80+
new $class(null, null);
8081
} catch (\Throwable $e) {
8182
$thrown = true;
8283
$this->assertFalse($e instanceof \TypeError);
@@ -86,7 +87,7 @@ public function testConstructNullSeeds() {
8687
/* Passing an empty array must also not throw TypeError (control). */
8788
$thrown = false;
8889
try {
89-
new RedisCluster(null, []);
90+
new $class(null, []);
9091
} catch (\Throwable $e) {
9192
$thrown = true;
9293
$this->assertFalse($e instanceof \TypeError);
@@ -96,17 +97,18 @@ public function testConstructNullSeeds() {
9697
/* Both (null) and (null, null) mean "no name, no seeds" and must
9798
* produce the same exception type. */
9899
$ex1 = $ex2 = null;
99-
try { new RedisCluster(null); }
100+
try { new $class(null); }
100101
catch (\Throwable $e) { $ex1 = get_class($e); }
101-
try { new RedisCluster(null, null); }
102+
try { new $class(null, null); }
102103
catch (\Throwable $e) { $ex2 = get_class($e); }
103104
$this->assertTrue($ex1 !== null);
104105
$this->assertEquals($ex1, $ex2);
105106
}
106107

107108
private function loadSeedsFromHostPort($host, $port) {
109+
$class = $this->getRedisClusterClass();
108110
try {
109-
$rc = new RedisCluster(NULL, ["$host:$port"], 1, 1, true, $this->getAuth());
111+
$rc = new $class(NULL, ["$host:$port"], 1, 1, true, $this->getAuth());
110112
self::$seed_source = "Host: $host, Port: $port";
111113
return array_map(function($master) {
112114
return sprintf('%s:%s', $master[0], $master[1]);
@@ -203,16 +205,17 @@ private function getServerReply($host, $port, $cmd) {
203205
runners. If we're not on a runner we just get a new instance. Otherwise
204206
we allow for two tries to get the instance. */
205207
private function getNewInstance() {
208+
$class = $this->getRedisClusterClass();
206209
if (getenv('GITHUB_ACTIONS') === 'true') {
207210
try {
208-
return new RedisCluster(NULL, self::$seeds, 30, 30, true,
209-
$this->getAuth());
211+
return new $class(NULL, self::$seeds, 30, 30, true,
212+
$this->getAuth());
210213
} catch (Exception $ex) {
211214
TestSuite::errorMessage("Failed to connect: %s", $ex->getMessage());
212215
}
213216
}
214217

215-
return new RedisCluster(NULL, self::$seeds, 30, 30, true, $this->getAuth());
218+
return new $class(NULL, self::$seeds, 30, 30, true, $this->getAuth());
216219
}
217220

218221
/* Override newInstance as we want a RedisCluster object */
@@ -271,6 +274,7 @@ public function testPing() {
271274
}
272275

273276
public function testDirectedCommandsRejectUncoveredSlot() {
277+
$class = $this->getRedisClusterClass();
274278
for ($i = 0; $i < 256; $i++) {
275279
$key = "{uncovered-directed-$i}key";
276280
$slot = $this->redis->cluster($key, 'KEYSLOT', $key);
@@ -296,7 +300,7 @@ public function testDirectedCommandsRejectUncoveredSlot() {
296300
}
297301
$removed = true;
298302
/* Map from the owner so the object's slot table contains the hole. */
299-
$client = new RedisCluster(null, ["{$master[0]}:{$master[1]}"],
303+
$client = new $class(null, ["{$master[0]}:{$master[1]}"],
300304
1, 1, false, $this->getAuth());
301305

302306
/* GET already has this guard; directed commands must reject the slot too. */
@@ -448,7 +452,8 @@ private function replicasForKey(string $key): array {
448452
/* Directed RedisCluster commands can only address a node that owns slots,
449453
* so talk to a replica over a plain Redis connection instead. */
450454
private function connectToNode(array $node) {
451-
$redis = new Redis(['host' => $node[0], 'port' => $node[1]]);
455+
$class = $this->getRedisClass();
456+
$redis = new $class(['host' => $node[0], 'port' => $node[1]]);
452457

453458
if ($this->getAuth())
454459
$this->assertTrue($redis->auth($this->getAuth()));
@@ -474,6 +479,7 @@ private function movedCount(array $nodes): int {
474479
* READONLY we sent doesn't survive the new connection, so we have to send
475480
* it again or the replica answers MOVED to every read we send it. */
476481
public function testReplicaReadonlyResentAfterReconnect() {
482+
$class = $this->getRedisClusterClass();
477483
if ( ! $this->minVersionCheck('6.2.0'))
478484
$this->markTestSkipped('INFO ERRORSTATS requires Redis >= 6.2.0');
479485

@@ -487,7 +493,7 @@ public function testReplicaReadonlyResentAfterReconnect() {
487493
/* Without READONLY every read bounces on MOVED until the redirection
488494
* loop gives up after timeout + read_timeout, so keep those short or a
489495
* regression takes the default 60 seconds per read to surface. */
490-
$client = new RedisCluster(NULL, self::$seeds, 1, 1, false, $this->getAuth());
496+
$client = new $class(NULL, self::$seeds, 1, 1, false, $this->getAuth());
491497
$client->setOption(RedisCluster::OPT_SLAVE_FAILOVER,
492498
RedisCluster::FAILOVER_DISTRIBUTE_SLAVES);
493499

@@ -1013,7 +1019,7 @@ protected function checkKeyValue($key, $key_type, $value) {
10131019
$this->assertEquals($value, $this->redis->lrange($key, 0, -1));
10141020
break;
10151021
case Redis::REDIS_HASH:
1016-
$this->assertEquals($value, $this->redis->hgetall($key));
1022+
$this->assertEqualsCanonicalizing($value, $this->redis->hgetall($key), true);
10171023
break;
10181024
case Redis::REDIS_ZSET:
10191025
$this->checkZSetEquality($value, $this->redis->zrange($key, 0, -1, true));

‎tests/RedisSentinelTest.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class Redis_Sentinel_Test extends TestSuite
3030

3131
protected function newInstance()
3232
{
33-
return new RedisSentinel(['host' => $this->getHost()]);
33+
$class = $this->getRedisSentinelClass();
34+
return new $class(['host' => $this->getHost()]);
3435
}
3536

3637
public function setUp()
@@ -122,7 +123,7 @@ public function testSlaves()
122123
}
123124
}
124125

125-
protected function getClients(Redis $redis, string $cmd)
126+
protected function getClients($redis, string $cmd)
126127
{
127128
$result = [];
128129

@@ -143,14 +144,17 @@ protected function getClients(Redis $redis, string $cmd)
143144
}
144145

145146
public function testPersistent() {
147+
$redis_class = $this->getRedisClass();
148+
$sentinel_class = $this->getRedisSentinelClass();
149+
146150
/* I think the tests just use the default port */
147-
$redis = new Redis;
151+
$redis = new $redis_class;
148152
$redis->connect($this->getHost(), 26379);
149153

150154
$id = null;
151155

152156
for ($i = 0; $i < 3; $i++) {
153-
$sentinel = new RedisSentinel([
157+
$sentinel = new $sentinel_class([
154158
'host' => $this->getHost(),
155159
'persistent' => 'sentinel',
156160
]);

‎tests/RedisTest.php‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ protected function getAuthFragment() {
169169
}
170170

171171
protected function newInstance() {
172-
$r = new Redis([
172+
$class = $this->getRedisClass();
173+
$r = new $class([
173174
'host' => $this->getHost(),
174175
'port' => $this->getPort(),
175176
]);
@@ -3806,7 +3807,8 @@ public function testQueuedReplyModes() {
38063807
$this->redis->watch($skey);
38073808
$this->redis->multi()->hMGet($hkey, ['field'])->zRandMember($zkey);
38083809
$other->set($skey, 'changed');
3809-
$aborted = $this->redis instanceof RedisCluster ? [false, false] : false;
3810+
$cluster_class = $this->getRedisClusterClass();
3811+
$aborted = $this->redis instanceof $cluster_class ? [false, false] : false;
38103812
$this->assertEquals($aborted, $this->redis->exec());
38113813
$this->assertEquals('member', $this->redis->zRandMember($zkey));
38123814
$this->redis->del($zkey, $hkey, $skey);
@@ -8552,6 +8554,7 @@ function($o) { $o->auth(['1337haxx00r', 'lolwut']); }, '/^WRONGPASS.*$/');
85528554

85538555
/* If we detect a unix socket make sure we can connect to it in a variety of ways */
85548556
public function testUnixSocket() {
8557+
$class = $this->getRedisClass();
85558558
if ( ! file_exists('/tmp/redis.sock'))
85568559
$this->markTestSkipped();
85578560

@@ -8563,7 +8566,7 @@ public function testUnixSocket() {
85638566
];
85648567

85658568
foreach ($sock_tests as $args) {
8566-
$redis = new Redis();
8569+
$redis = new $class();
85678570

85688571
if (count($args) == 2) {
85698572
@$redis->connect($args[0], $args[1]);
@@ -8593,6 +8596,7 @@ protected function detectRedis($host, $port) {
85938596

85948597
/* Test high ports if we detect Redis running there */
85958598
public function testHighPorts() {
8599+
$class = $this->getRedisClass();
85968600
$ports = array_filter(array_map(function ($port) {
85978601
return $this->detectRedis('localhost', $port) ? $port : 0;
85988602
}, [32768, 32769, 32770]));
@@ -8601,7 +8605,7 @@ public function testHighPorts() {
86018605
$this->markTestSkipped();
86028606

86038607
foreach ($ports as $port) {
8604-
$redis = new Redis();
8608+
$redis = new $class();
86058609
@$redis->connect('localhost', $port);
86068610
if ($this->getAuth()) {
86078611
$this->assertTrue($redis->auth($this->getAuth()));
@@ -8981,6 +8985,7 @@ public function testMultipleConnect() {
89818985
}
89828986

89838987
public function testConnectDatabaseSelect() {
8988+
$class = $this->getRedisClass();
89848989
$options = [
89858990
'host' => $this->getHost(),
89868991
'port' => $this->getPort(),
@@ -8991,7 +8996,7 @@ public function testConnectDatabaseSelect() {
89918996
$options['auth'] = $this->getAuth();
89928997
}
89938998

8994-
$redis = new Redis($options);
8999+
$redis = new $class($options);
89959000
$this->assertEquals(2, $redis->getDBNum());
89969001
$this->assertEquals(2, $redis->client('info')['db']);
89979002

@@ -9002,11 +9007,12 @@ public function testConnectDatabaseSelect() {
90029007
}
90039008

90049009
public function testConnectException() {
9010+
$class = $this->getRedisClass();
90059011
$host = 'github.com';
90069012
if (gethostbyname($host) === $host)
90079013
$this->markTestSkipped('online test');
90089014

9009-
$redis = new Redis();
9015+
$redis = new $class();
90109016
try {
90119017
$redis->connect($host, 6379, 0.01);
90129018
} catch (Exception $e) {
@@ -9017,13 +9023,14 @@ public function testConnectException() {
90179023
/* Regression test for GitHub PR #2802). Make sure we don't leak the
90189024
* context array when explicitly reconnecting in a loop */
90199025
public function testTlsReconnect() {
9026+
$class = $this->getRedisClass();
90209027
$tls_port = $this->getTlsPort();
90219028
if (($fp = @fsockopen($this->getHost(), $tls_port)) == NULL)
90229029
$this->markTestSkipped();
90239030

90249031
fclose($fp);
90259032

9026-
$redis = new Redis;
9033+
$redis = new $class;
90279034

90289035
$context = ['stream' => [
90299036
'verify_peer_name' => false,
@@ -9043,6 +9050,7 @@ public function testTlsReconnect() {
90439050
}
90449051

90459052
public function testTlsConnect() {
9053+
$class = $this->getRedisClass();
90469054
$tls_port = $this->getTlsPort();
90479055

90489056
if (($fp = @fsockopen($this->getHost(), $tls_port)) == NULL)
@@ -9051,7 +9059,7 @@ public function testTlsConnect() {
90519059
fclose($fp);
90529060

90539061
foreach (['localhost' => true, '127.0.0.1' => false] as $host => $verify) {
9054-
$redis = new Redis();
9062+
$redis = new $class();
90559063
$this->assertTrue($redis->connect('tls://' . $host, $tls_port, 0, null, 0, 0, [
90569064
'stream' => ['verify_peer_name' => $verify, 'verify_peer' => false]
90579065
]));

‎tests/TestSuite.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function getPort() { return $this->port; }
5252
public function getTlsPort() { return $this->tls_port; }
5353
public function getAuth() { return $this->auth; }
5454

55+
/* Override these independently: cluster and sentinel tests also use
56+
* standalone clients for administrative commands. */
57+
protected function getRedisClass() { return Redis::class; }
58+
protected function getRedisClusterClass() { return RedisCluster::class; }
59+
protected function getRedisSentinelClass() { return RedisSentinel::class; }
60+
5561
/* Test classes that can query INFO may override this. */
5662
public function getServerInfo() {
5763
return NULL;

0 commit comments

Comments
 (0)