From 7716a063d9b493edd166c8e46aba9a087cf7f2d0 Mon Sep 17 00:00:00 2001 From: michael-grunder Date: Mon, 17 Jun 2024 10:42:47 -0700 Subject: [PATCH] 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 --- tests/RedisArrayTest.php | 3 ++- tests/RedisClusterTest.php | 13 +++++------ tests/RedisSentinelTest.php | 2 +- tests/RedisTest.php | 43 +++++++++++++++++++++++++++++++++++-- tests/TestRedis.php | 10 ++++----- tests/TestSuite.php | 12 ++++++++++- 6 files changed, 67 insertions(+), 16 deletions(-) diff --git a/tests/RedisArrayTest.php b/tests/RedisArrayTest.php index 0586f09289..82e11ddab2 100644 --- a/tests/RedisArrayTest.php +++ b/tests/RedisArrayTest.php @@ -1,5 +1,6 @@ loadSeedsFromHostPort($host, $port))) return $seeds; - fprintf(STDERR, "Error: Unable to load seeds for RedisCluster tests\n"); + TestSuite::errorMessage("Error: Unable to load seeds for RedisCluster tests"); foreach (self::$seed_messages as $msg) { - fprintf(STDERR, " Tried: %s\n", $msg); + TestSuite::errorMessage(" Tried: %s", $msg); } exit(1); @@ -139,9 +140,9 @@ protected function newInstance() { try { return new RedisCluster(NULL, self::$seeds, 30, 30, true, $this->getAuth()); } catch (Exception $ex) { - fprintf(STDERR, "Fatal error: %s\n", $ex->getMessage()); - fprintf(STDERR, "Seeds: %s\n", implode(' ', self::$seeds)); - fprintf(STDERR, "Seed source: %s\n", self::$seed_source); + TestSuite::errorMessage("Fatal error: %s\n", $ex->getMessage()); + TestSuite::errorMessage("Seeds: %s\n", implode(' ', self::$seeds)); + TestSuite::errorMessage("Seed source: %s\n", self::$seed_source); exit(1); } } diff --git a/tests/RedisSentinelTest.php b/tests/RedisSentinelTest.php index 0fdc3a957e..cfb7d6b044 100644 --- a/tests/RedisSentinelTest.php +++ b/tests/RedisSentinelTest.php @@ -1,6 +1,6 @@ savePath($this->sessionSavePath()); } + protected function testRequiresMode(string $mode) { + if (php_sapi_name() != $mode) { + $this->markTestSkipped("Test requires PHP running in '$mode' mode"); + } + } + public function testSession_compression() { + $this->testRequiresMode('cli'); + foreach ($this->getCompressors() as $name => $val) { $data = "testing_compression_$name"; @@ -7244,6 +7252,8 @@ public function testSession_compression() { } public function testSession_savedToRedis() { + $this->testRequiresMode('cli'); + $runner = $this->sessionRunner(); $this->assertEquals('SUCCESS', $runner->execFg()); @@ -7260,6 +7270,8 @@ protected function sessionWaitSec() { } public function testSession_lockKeyCorrect() { + $this->testRequiresMode('cli'); + $runner = $this->sessionRunner()->sleep(5); $this->assertTrue($runner->execBg()); @@ -7272,6 +7284,8 @@ public function testSession_lockKeyCorrect() { } public function testSession_lockingDisabledByDefault() { + $this->testRequiresMode('cli'); + $runner = $this->sessionRunner() ->lockingEnabled(false) ->sleep(5); @@ -7281,6 +7295,8 @@ public function testSession_lockingDisabledByDefault() { } public function testSession_lockReleasedOnClose() { + $this->testRequiresMode('cli'); + $runner = $this->sessionRunner() ->sleep(1) ->lockingEnabled(true); @@ -7291,6 +7307,8 @@ public function testSession_lockReleasedOnClose() { } public function testSession_lock_ttlMaxExecutionTime() { + $this->testRequiresMode('cli'); + $runner1 = $this->sessionRunner() ->sleep(10) ->maxExecutionTime(2); @@ -7309,6 +7327,7 @@ public function testSession_lock_ttlMaxExecutionTime() { } public function testSession_lock_ttlLockExpire() { + $this->testRequiresMode('cli'); $runner1 = $this->sessionRunner() ->sleep(10) @@ -7329,6 +7348,8 @@ public function testSession_lock_ttlLockExpire() { } public function testSession_lockHoldCheckBeforeWrite_otherProcessHasLock() { + $this->testRequiresMode('cli'); + $id = 'test-id'; $runner = $this->sessionRunner() @@ -7352,6 +7373,8 @@ public function testSession_lockHoldCheckBeforeWrite_otherProcessHasLock() { } public function testSession_lockHoldCheckBeforeWrite_nobodyHasLock() { + $this->testRequiresMode('cli'); + $runner = $this->sessionRunner() ->sleep(2) ->lockingEnabled(true) @@ -7363,6 +7386,8 @@ public function testSession_lockHoldCheckBeforeWrite_nobodyHasLock() { } public function testSession_correctLockRetryCount() { + $this->testRequiresMode('cli'); + $runner = $this->sessionRunner() ->sleep(10); @@ -7394,6 +7419,8 @@ public function testSession_correctLockRetryCount() { } public function testSession_defaultLockRetryCount() { + $this->testRequiresMode('cli'); + $runner = $this->sessionRunner() ->sleep(10); @@ -7420,6 +7447,8 @@ public function testSession_defaultLockRetryCount() { } public function testSession_noUnlockOfOtherProcess() { + $this->testRequiresMode('cli'); + $st = microtime(true); $sleep = 3; @@ -7453,6 +7482,8 @@ public function testSession_noUnlockOfOtherProcess() { } public function testSession_lockWaitTime() { + $this->testRequiresMode('cli'); + $runner = $this->sessionRunner() ->sleep(1) @@ -7603,6 +7634,8 @@ public function testBadOptionValue() { } protected function regenerateIdHelper(bool $lock, bool $destroy, bool $proxy) { + $this->testRequiresMode('cli'); + $data = uniqid('regenerate-id:'); $runner = $this->sessionRunner() ->sleep(0) @@ -7652,12 +7685,16 @@ public function testSession_regenerateSessionId_withLock_withDestroy_withProxy( } public function testSession_ttl_equalsToSessionLifetime() { + $this->testRequiresMode('cli'); + $runner = $this->sessionRunner()->lifetime(600); $this->assertEquals('SUCCESS', $runner->execFg()); $this->assertEquals(600, $this->redis->ttl($runner->getSessionKey())); } public function testSession_ttl_resetOnWrite() { + $this->testRequiresMode('cli'); + $runner1 = $this->sessionRunner()->lifetime(600); $this->assertEquals('SUCCESS', $runner1->execFg()); @@ -7668,6 +7705,8 @@ public function testSession_ttl_resetOnWrite() { } public function testSession_ttl_resetOnRead() { + $this->testRequiresMode('cli'); + $data = uniqid(__FUNCTION__); $runner = $this->sessionRunner()->lifetime(600)->data($data); diff --git a/tests/TestRedis.php b/tests/TestRedis.php index 3efca43ffd..7ddb231574 100644 --- a/tests/TestRedis.php +++ b/tests/TestRedis.php @@ -1,10 +1,10 @@ host; } public function getPort() { return $this->port; } public function getAuth() { return $this->auth; } + public static function errorMessage(string $fmt, ...$args) { + $msg = vsprintf($fmt . "\n", $args); + + if (defined('STDERR')) { + fwrite(STDERR, $msg); + } else { + echo $msg; + } + } + public static function make_bold(string $msg) { return self::$colorize ? self::$BOLD_ON . $msg . self::$BOLD_OFF : $msg; } @@ -516,7 +526,7 @@ public static function loadTestClass($class) { /* Flag colorization */ public static function flagColorization(bool $override) { self::$colorize = $override && function_exists('posix_isatty') && - posix_isatty(STDOUT); + defined('STDOUT') && posix_isatty(STDOUT); } public static function run($class_name, ?string $limit = NULL,