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

Skip to content

Commit b808cc6

Browse files
Update tests so they can run in php-cgi.
This probably isn't a very common scenerio since we've never had someone ask it in a decade, but it was very simple to get them working. Primarily we just needed to test for `STDTOUT`/`STDERR` and use `__DIR__` instead of `$_SERVER['PHP_SELF']`. Fixes #2507
1 parent 7050c98 commit b808cc6

6 files changed

Lines changed: 67 additions & 16 deletions

File tree

tests/RedisArrayTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php defined('PHPREDIS_TESTRUN') or die("Use TestRedis.php to run tests!\n");
2-
require_once(dirname($_SERVER['PHP_SELF'])."/TestSuite.php");
2+
3+
require_once __DIR__ . "/TestSuite.php";
34

45
define('REDIS_ARRAY_DATA_SIZE', 1000);
56
define('REDIS_RA_DEFAULT_PORTS', [6379, 6380, 6381, 6382]);

tests/RedisClusterTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php defined('PHPREDIS_TESTRUN') or die("Use TestRedis.php to run tests!\n");
2-
require_once(dirname($_SERVER['PHP_SELF'])."/RedisTest.php");
2+
3+
require_once __DIR__ . "/RedisTest.php";
34

45
/**
56
* Most RedisCluster tests should work the same as the standard Redis object
@@ -111,9 +112,9 @@ private function loadSeeds($host, $port) {
111112
if (($seeds = $this->loadSeedsFromHostPort($host, $port)))
112113
return $seeds;
113114

114-
fprintf(STDERR, "Error: Unable to load seeds for RedisCluster tests\n");
115+
TestSuite::errorMessage("Error: Unable to load seeds for RedisCluster tests");
115116
foreach (self::$seed_messages as $msg) {
116-
fprintf(STDERR, " Tried: %s\n", $msg);
117+
TestSuite::errorMessage(" Tried: %s", $msg);
117118
}
118119

119120
exit(1);
@@ -139,9 +140,9 @@ protected function newInstance() {
139140
try {
140141
return new RedisCluster(NULL, self::$seeds, 30, 30, true, $this->getAuth());
141142
} catch (Exception $ex) {
142-
fprintf(STDERR, "Fatal error: %s\n", $ex->getMessage());
143-
fprintf(STDERR, "Seeds: %s\n", implode(' ', self::$seeds));
144-
fprintf(STDERR, "Seed source: %s\n", self::$seed_source);
143+
TestSuite::errorMessage("Fatal error: %s\n", $ex->getMessage());
144+
TestSuite::errorMessage("Seeds: %s\n", implode(' ', self::$seeds));
145+
TestSuite::errorMessage("Seed source: %s\n", self::$seed_source);
145146
exit(1);
146147
}
147148
}

tests/RedisSentinelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php defined('PHPREDIS_TESTRUN') or die("Use TestRedis.php to run tests!\n");
22

3-
require_once(dirname($_SERVER['PHP_SELF'])."/TestSuite.php");
3+
require_once __DIR__ . "/TestSuite.php";
44

55
class Redis_Sentinel_Test extends TestSuite
66
{

tests/RedisTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php defined('PHPREDIS_TESTRUN') or die('Use TestRedis.php to run tests!\n');
22

3-
require_once(dirname($_SERVER['PHP_SELF']).'/TestSuite.php');
4-
require_once(dirname($_SERVER['PHP_SELF']).'/SessionHelpers.php');
3+
require_once __DIR__ . '/TestSuite.php';
4+
require_once __DIR__ . '/SessionHelpers.php';
55

66
class Redis_Test extends TestSuite {
77
/**
@@ -7223,7 +7223,15 @@ protected function sessionRunner() {
72237223
->savePath($this->sessionSavePath());
72247224
}
72257225

7226+
protected function testRequiresMode(string $mode) {
7227+
if (php_sapi_name() != $mode) {
7228+
$this->markTestSkipped("Test requires PHP running in '$mode' mode");
7229+
}
7230+
}
7231+
72267232
public function testSession_compression() {
7233+
$this->testRequiresMode('cli');
7234+
72277235
foreach ($this->getCompressors() as $name => $val) {
72287236
$data = "testing_compression_$name";
72297237

@@ -7244,6 +7252,8 @@ public function testSession_compression() {
72447252
}
72457253

72467254
public function testSession_savedToRedis() {
7255+
$this->testRequiresMode('cli');
7256+
72477257
$runner = $this->sessionRunner();
72487258

72497259
$this->assertEquals('SUCCESS', $runner->execFg());
@@ -7260,6 +7270,8 @@ protected function sessionWaitSec() {
72607270
}
72617271

72627272
public function testSession_lockKeyCorrect() {
7273+
$this->testRequiresMode('cli');
7274+
72637275
$runner = $this->sessionRunner()->sleep(5);
72647276

72657277
$this->assertTrue($runner->execBg());
@@ -7272,6 +7284,8 @@ public function testSession_lockKeyCorrect() {
72727284
}
72737285

72747286
public function testSession_lockingDisabledByDefault() {
7287+
$this->testRequiresMode('cli');
7288+
72757289
$runner = $this->sessionRunner()
72767290
->lockingEnabled(false)
72777291
->sleep(5);
@@ -7281,6 +7295,8 @@ public function testSession_lockingDisabledByDefault() {
72817295
}
72827296

72837297
public function testSession_lockReleasedOnClose() {
7298+
$this->testRequiresMode('cli');
7299+
72847300
$runner = $this->sessionRunner()
72857301
->sleep(1)
72867302
->lockingEnabled(true);
@@ -7291,6 +7307,8 @@ public function testSession_lockReleasedOnClose() {
72917307
}
72927308

72937309
public function testSession_lock_ttlMaxExecutionTime() {
7310+
$this->testRequiresMode('cli');
7311+
72947312
$runner1 = $this->sessionRunner()
72957313
->sleep(10)
72967314
->maxExecutionTime(2);
@@ -7309,6 +7327,7 @@ public function testSession_lock_ttlMaxExecutionTime() {
73097327
}
73107328

73117329
public function testSession_lock_ttlLockExpire() {
7330+
$this->testRequiresMode('cli');
73127331

73137332
$runner1 = $this->sessionRunner()
73147333
->sleep(10)
@@ -7329,6 +7348,8 @@ public function testSession_lock_ttlLockExpire() {
73297348
}
73307349

73317350
public function testSession_lockHoldCheckBeforeWrite_otherProcessHasLock() {
7351+
$this->testRequiresMode('cli');
7352+
73327353
$id = 'test-id';
73337354

73347355
$runner = $this->sessionRunner()
@@ -7352,6 +7373,8 @@ public function testSession_lockHoldCheckBeforeWrite_otherProcessHasLock() {
73527373
}
73537374

73547375
public function testSession_lockHoldCheckBeforeWrite_nobodyHasLock() {
7376+
$this->testRequiresMode('cli');
7377+
73557378
$runner = $this->sessionRunner()
73567379
->sleep(2)
73577380
->lockingEnabled(true)
@@ -7363,6 +7386,8 @@ public function testSession_lockHoldCheckBeforeWrite_nobodyHasLock() {
73637386
}
73647387

73657388
public function testSession_correctLockRetryCount() {
7389+
$this->testRequiresMode('cli');
7390+
73667391
$runner = $this->sessionRunner()
73677392
->sleep(10);
73687393

@@ -7394,6 +7419,8 @@ public function testSession_correctLockRetryCount() {
73947419
}
73957420

73967421
public function testSession_defaultLockRetryCount() {
7422+
$this->testRequiresMode('cli');
7423+
73977424
$runner = $this->sessionRunner()
73987425
->sleep(10);
73997426

@@ -7420,6 +7447,8 @@ public function testSession_defaultLockRetryCount() {
74207447
}
74217448

74227449
public function testSession_noUnlockOfOtherProcess() {
7450+
$this->testRequiresMode('cli');
7451+
74237452
$st = microtime(true);
74247453

74257454
$sleep = 3;
@@ -7453,6 +7482,8 @@ public function testSession_noUnlockOfOtherProcess() {
74537482
}
74547483

74557484
public function testSession_lockWaitTime() {
7485+
$this->testRequiresMode('cli');
7486+
74567487

74577488
$runner = $this->sessionRunner()
74587489
->sleep(1)
@@ -7603,6 +7634,8 @@ public function testBadOptionValue() {
76037634
}
76047635

76057636
protected function regenerateIdHelper(bool $lock, bool $destroy, bool $proxy) {
7637+
$this->testRequiresMode('cli');
7638+
76067639
$data = uniqid('regenerate-id:');
76077640
$runner = $this->sessionRunner()
76087641
->sleep(0)
@@ -7652,12 +7685,16 @@ public function testSession_regenerateSessionId_withLock_withDestroy_withProxy(
76527685
}
76537686

76547687
public function testSession_ttl_equalsToSessionLifetime() {
7688+
$this->testRequiresMode('cli');
7689+
76557690
$runner = $this->sessionRunner()->lifetime(600);
76567691
$this->assertEquals('SUCCESS', $runner->execFg());
76577692
$this->assertEquals(600, $this->redis->ttl($runner->getSessionKey()));
76587693
}
76597694

76607695
public function testSession_ttl_resetOnWrite() {
7696+
$this->testRequiresMode('cli');
7697+
76617698
$runner1 = $this->sessionRunner()->lifetime(600);
76627699
$this->assertEquals('SUCCESS', $runner1->execFg());
76637700

@@ -7668,6 +7705,8 @@ public function testSession_ttl_resetOnWrite() {
76687705
}
76697706

76707707
public function testSession_ttl_resetOnRead() {
7708+
$this->testRequiresMode('cli');
7709+
76717710
$data = uniqid(__FUNCTION__);
76727711

76737712
$runner = $this->sessionRunner()->lifetime(600)->data($data);

tests/TestRedis.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php define('PHPREDIS_TESTRUN', true);
22

3-
require_once(dirname($_SERVER['PHP_SELF'])."/TestSuite.php");
4-
require_once(dirname($_SERVER['PHP_SELF'])."/RedisTest.php");
5-
require_once(dirname($_SERVER['PHP_SELF'])."/RedisArrayTest.php");
6-
require_once(dirname($_SERVER['PHP_SELF'])."/RedisClusterTest.php");
7-
require_once(dirname($_SERVER['PHP_SELF'])."/RedisSentinelTest.php");
3+
require_once __DIR__ . "/TestSuite.php";
4+
require_once __DIR__ . "/RedisTest.php";
5+
require_once __DIR__ . "/RedisArrayTest.php";
6+
require_once __DIR__ . "/RedisClusterTest.php";
7+
require_once __DIR__ . "/RedisSentinelTest.php";
88

99
function getClassArray($classes) {
1010
$result = [];

tests/TestSuite.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public function getHost() { return $this->host; }
4343
public function getPort() { return $this->port; }
4444
public function getAuth() { return $this->auth; }
4545

46+
public static function errorMessage(string $fmt, ...$args) {
47+
$msg = vsprintf($fmt . "\n", $args);
48+
49+
if (defined('STDERR')) {
50+
fwrite(STDERR, $msg);
51+
} else {
52+
echo $msg;
53+
}
54+
}
55+
4656
public static function make_bold(string $msg) {
4757
return self::$colorize ? self::$BOLD_ON . $msg . self::$BOLD_OFF : $msg;
4858
}
@@ -516,7 +526,7 @@ public static function loadTestClass($class) {
516526
/* Flag colorization */
517527
public static function flagColorization(bool $override) {
518528
self::$colorize = $override && function_exists('posix_isatty') &&
519-
posix_isatty(STDOUT);
529+
defined('STDOUT') && posix_isatty(STDOUT);
520530
}
521531

522532
public static function run($class_name, ?string $limit = NULL,

0 commit comments

Comments
 (0)