From ee9cdbb95b2a75c1a36c8d19cd8e5515453ccd24 Mon Sep 17 00:00:00 2001 From: Renat Bilalov Date: Mon, 23 Nov 2026 21:29:13 +0400 Subject: [PATCH] Add support for PHP 8.6 (#1771) --- .github/workflows/tests.yml | 3 +- CHANGELOG.md | 1 + composer.json | 2 +- src/Session/Handler.php | 38 +++++++-- tests/Predis/ClientTest.php | 1 + tests/Predis/Command/Redis/HOTKEYS_Test.php | 8 +- tests/Predis/Consumer/PubSub/ConsumerTest.php | 5 ++ tests/Predis/Session/HandlerTest.php | 77 +++++++++++++++++++ 8 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 tests/Predis/Session/HandlerTest.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a0d4f3d1..82d98e8a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -29,8 +29,7 @@ jobs: php: - '7.2' - '8.0' - - '8.3' - - '8.5' + - '8.6' redis: - '4.0' - '7.2' diff --git a/CHANGELOG.md b/CHANGELOG.md index ba19deba..277ddcd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased ### Added +- Added PHP 8.6 to the test matrix and updated session handling for PHP 8.6 (#1771) - Added support for Set Expiration commands (#1749) - Added support for `BITRANGEINTARRAY` command (#1754) - Added support for `ZMOVE`, `BZMOVE`, `ZMOVEMEMBER` commands (#1762) diff --git a/composer.json b/composer.json index a5be6408..55fdb94a 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.3", "phpstan/phpstan": "^1.9", - "phpunit/phpunit": "^8.0 || ~9.4.4", + "phpunit/phpunit": "^8.0 || ^9.6", "phpunit/phpcov": "^6.0 || ^8.0" }, "suggest": { diff --git a/src/Session/Handler.php b/src/Session/Handler.php index 7619ba20..85445a82 100644 --- a/src/Session/Handler.php +++ b/src/Session/Handler.php @@ -13,18 +13,18 @@ namespace Predis\Session; use Predis\ClientInterface; +use Predis\Response\ErrorInterface; +use Predis\Response\ServerException; use ReturnTypeWillChange; use SessionHandlerInterface; +use SessionIdInterface; +use SessionUpdateTimestampHandlerInterface; /** * Session handler class that relies on Predis\Client to store PHP's sessions * data into one or multiple Redis servers. - * - * This class is mostly intended for PHP 5.4 but it can be used under PHP 5.3 - * provided that a polyfill for `SessionHandlerInterface` is defined by either - * you or an external package such as `symfony/http-foundation`. */ -class Handler implements SessionHandlerInterface +class Handler implements SessionHandlerInterface, SessionIdInterface, SessionUpdateTimestampHandlerInterface { protected $client; protected $ttl; @@ -52,6 +52,34 @@ class Handler implements SessionHandlerInterface session_set_save_handler($this, true); } + #[ReturnTypeWillChange] + public function create_sid() + { + do { + $id = bin2hex(random_bytes(32)); + } while ($this->validateId($id)); + + return $id; + } + + #[ReturnTypeWillChange] + public function validateId($id) + { + $response = $this->client->exists($id); + + if ($response instanceof ErrorInterface) { + throw new ServerException($response->getMessage()); + } + + return (bool) $response; + } + + #[ReturnTypeWillChange] + public function updateTimestamp($id, $data) + { + return $this->write($id, $data); + } + /** * @param string $save_path * @param string $session_id diff --git a/tests/Predis/ClientTest.php b/tests/Predis/ClientTest.php index 826ac43d..085a5397 100644 --- a/tests/Predis/ClientTest.php +++ b/tests/Predis/ClientTest.php @@ -1464,6 +1464,7 @@ class ClientTest extends PredisTestCase * @group cluster * @requiresRedisVersion >= 2.0.0 * @requires PHP >= 7.4 + * @requires extension relay * @return void */ public function testCreatesClusterWithRelayConnection(): void diff --git a/tests/Predis/Command/Redis/HOTKEYS_Test.php b/tests/Predis/Command/Redis/HOTKEYS_Test.php index b80faeca..0c338290 100644 --- a/tests/Predis/Command/Redis/HOTKEYS_Test.php +++ b/tests/Predis/Command/Redis/HOTKEYS_Test.php @@ -72,7 +72,7 @@ class HOTKEYS_Test extends PredisCommandTestCase $this->assertEquals('OK', $redis->hotkeys->start([Container::CPU])); // A single SET's CPU time can round down to 0 and be excluded from tracking. - for ($i = 0; $i < 5; $i++) { + for ($i = 0; $i < 100; $i++) { $redis->set('key', 'value'); } @@ -84,7 +84,7 @@ class HOTKEYS_Test extends PredisCommandTestCase // Starts hotkeys tracking (CPU and NET) $this->assertEquals('OK', $redis->hotkeys->start([Container::CPU, Container::NET])); - for ($i = 0; $i < 5; $i++) { + for ($i = 0; $i < 100; $i++) { $redis->set('key', 'value'); $redis->set('key1', 'value1'); } @@ -149,7 +149,7 @@ class HOTKEYS_Test extends PredisCommandTestCase $this->assertEquals('OK', $redis->hotkeys->start([Container::CPU])); // A single SET's CPU time can round down to 0 and be excluded from tracking. - for ($i = 0; $i < 5; $i++) { + for ($i = 0; $i < 100; $i++) { $redis->set('key', 'value'); } @@ -161,7 +161,7 @@ class HOTKEYS_Test extends PredisCommandTestCase // Starts hotkeys tracking (CPU and NET) $this->assertEquals('OK', $redis->hotkeys->start([Container::CPU, Container::NET])); - for ($i = 0; $i < 5; $i++) { + for ($i = 0; $i < 100; $i++) { $redis->set('key', 'value'); $redis->set('key1', 'value1'); } diff --git a/tests/Predis/Consumer/PubSub/ConsumerTest.php b/tests/Predis/Consumer/PubSub/ConsumerTest.php index 475e15d3..2ff7a191 100644 --- a/tests/Predis/Consumer/PubSub/ConsumerTest.php +++ b/tests/Predis/Consumer/PubSub/ConsumerTest.php @@ -515,6 +515,11 @@ class ConsumerTest extends PredisTestCase '__subkeyspaceevent@0__:hset|test:hash' ); + // Wait until Redis has registered every subscription before publishing. + for ($i = 0; $i < 4; $i++) { + $this->assertSame('subscribe', $pubsub->current()->kind); + } + $producer->hset('test:hash', 'field', 'value'); foreach ($pubsub as $message) { diff --git a/tests/Predis/Session/HandlerTest.php b/tests/Predis/Session/HandlerTest.php new file mode 100644 index 00000000..0f094290 --- /dev/null +++ b/tests/Predis/Session/HandlerTest.php @@ -0,0 +1,77 @@ +createMock(ClientInterface::class); + $client->expects($this->exactly(2))->method('__call') + ->willReturnOnConsecutiveCalls(1, 0); + + $id = (new Handler($client))->create_sid(); + + $this->assertSame(1, preg_match('/^[a-f0-9]{64}$/', $id)); + } + + public function testValidatesExistingAndMissingIds() + { + $client = $this->createMock(ClientInterface::class); + $client->expects($this->exactly(2))->method('__call') + ->with('exists', ['session-id']) + ->willReturnOnConsecutiveCalls(1, 0); + $handler = new Handler($client); + + $this->assertTrue($handler->validateId('session-id')); + $this->assertFalse($handler->validateId('session-id')); + } + + public function testUpdatesTimestampAndPreservesData() + { + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once())->method('__call') + ->with('setex', ['session-id', 120, 'data']); + $handler = new Handler($client, ['gc_maxlifetime' => 120]); + + $this->assertTrue($handler->updateTimestamp('session-id', 'data')); + } + + /** + * @dataProvider sessionIdOperations + */ + public function testRejectsRedisErrorsDuringSessionIdOperations($operation, array $arguments) + { + $client = $this->createMock(ClientInterface::class); + $client->expects($this->once())->method('__call') + ->willReturn(new Error('NOPERM denied')); + $handler = new Handler($client); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage('NOPERM denied'); + $handler->$operation(...$arguments); + } + + public function sessionIdOperations() + { + return [ + ['validateId', ['session-id']], + ['create_sid', []], + ]; + } +}