From 355faa099fefe8e3dc1f5f7d0efa3320fae7e3d4 Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 13 Feb 2025 17:39:51 +0000 Subject: [PATCH] ClientInterface: update watch command accepting string and string[] (#1476) --- src/ClientInterface.php | 2 +- tests/Predis/Command/Redis/WATCH_Test.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 1775df07..963a0d79 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -334,7 +334,7 @@ use Predis\Response\Status; * @method mixed multi() * @method mixed unwatch() * @method array waitaof(int $numLocal, int $numReplicas, int $timeout) - * @method mixed watch(string $key) + * @method mixed watch(string[]|string $keyOrKeys) * @method mixed eval(string $script, int $numkeys, string ...$keyOrArg = null) * @method mixed eval_ro(string $script, array $keys, ...$argument) * @method mixed evalsha(string $script, int $numkeys, string ...$keyOrArg = null) diff --git a/tests/Predis/Command/Redis/WATCH_Test.php b/tests/Predis/Command/Redis/WATCH_Test.php index 71338734..087a7381 100644 --- a/tests/Predis/Command/Redis/WATCH_Test.php +++ b/tests/Predis/Command/Redis/WATCH_Test.php @@ -90,6 +90,25 @@ class WATCH_Test extends PredisCommandTestCase $this->assertSame('hijacked', $redis1->get('foo')); } + /** + * @group connected + * @requiresRedisVersion >= 2.2.0 + */ + public function testWatchMultipleKeysAsListAbortsTransactionOnExternalWriteOperations(): void + { + $redis1 = $this->getClient(); + $redis2 = $this->getClient(); + + $redis1->mset('foo', 'bar', 'hoge', 'piyo'); + + $this->assertEquals('OK', $redis1->watch(['foo', 'hoge'])); + $this->assertEquals('OK', $redis1->multi()); + $this->assertEquals('QUEUED', $redis1->set('hoge', 'bar')); + $this->assertEquals('OK', $redis2->set('hoge', 'hijacked')); + $this->assertNull($redis1->exec()); + $this->assertSame('hijacked', $redis1->get('hoge')); + } + /** * @group connected * @requiresRedisVersion >= 2.2.0