From a257c0d6d2b2a4bdddd8201c451c81f2896fc84a Mon Sep 17 00:00:00 2001 From: Oleksandr Khnykin Date: Tue, 6 Dec 2022 17:09:47 +0200 Subject: [PATCH 1/5] add possibility to set zero port for memcache and memcached checkers Signed-off-by: Oleksandr Khnykin --- src/Check/Memcache.php | 4 ++-- src/Check/Memcached.php | 4 ++-- test/MemcacheTest.php | 2 +- test/MemcachedTest.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Check/Memcache.php b/src/Check/Memcache.php index bb7bffb..21159df 100644 --- a/src/Check/Memcache.php +++ b/src/Check/Memcache.php @@ -42,9 +42,9 @@ public function __construct($host = '127.0.0.1', $port = 11211) } $port = (int) $port; - if ($port < 1) { + if ($port < 0) { throw new InvalidArgumentException(sprintf( - 'Invalid port number %d - expecting a positive integer', + 'Invalid port number %d - expecting an unsigned integer', $port )); } diff --git a/src/Check/Memcached.php b/src/Check/Memcached.php index 2d8659a..61b5325 100644 --- a/src/Check/Memcached.php +++ b/src/Check/Memcached.php @@ -42,9 +42,9 @@ public function __construct($host = '127.0.0.1', $port = 11211) } $port = (int) $port; - if ($port < 1) { + if ($port < 0) { throw new InvalidArgumentException(sprintf( - 'Invalid port number %d - expecting a positive integer', + 'Invalid port number %d - expecting an unsigned integer', $port )); } diff --git a/test/MemcacheTest.php b/test/MemcacheTest.php index 1ea3414..5ec9e97 100644 --- a/test/MemcacheTest.php +++ b/test/MemcacheTest.php @@ -19,7 +19,7 @@ public function testHostValidation(): void public function testPortValidation(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("Invalid port number -11211 - expecting a positive integer"); + $this->expectExceptionMessage("Invalid port number -11211 - expecting an unsigned integer"); new Memcache('127.0.0.1', -11211); } } diff --git a/test/MemcachedTest.php b/test/MemcachedTest.php index 174e396..cd96294 100644 --- a/test/MemcachedTest.php +++ b/test/MemcachedTest.php @@ -19,7 +19,7 @@ public function testHostValidation(): void public function testPortValidation(): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage("Invalid port number -11211 - expecting a positive integer"); + $this->expectExceptionMessage("Invalid port number -11211 - expecting an unsigned integer"); new Memcached('127.0.0.1', -11211); } } From d0370ee0af40bec2c3420a1abd65790730e9ba0a Mon Sep 17 00:00:00 2001 From: Oleksandr Khnykin Date: Tue, 6 Dec 2022 17:35:59 +0200 Subject: [PATCH 2/5] change in phpdoc Signed-off-by: Oleksandr Khnykin --- src/Check/Memcached.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Check/Memcached.php b/src/Check/Memcached.php index 61b5325..cb09b28 100644 --- a/src/Check/Memcached.php +++ b/src/Check/Memcached.php @@ -30,7 +30,7 @@ class Memcached extends AbstractCheck * @param string $host * @param int $port * @throws InvalidArgumentException If host is not a string value. - * @throws InvalidArgumentException If port is less than 1. + * @throws InvalidArgumentException If port is less than 0. */ public function __construct($host = '127.0.0.1', $port = 11211) { From d570888afe268e2702b6012d300e71397a6865f2 Mon Sep 17 00:00:00 2001 From: Oleksandr Khnykin Date: Wed, 14 Dec 2022 16:59:09 +0200 Subject: [PATCH 3/5] add tests && docs Signed-off-by: Oleksandr Khnykin --- docs/book/diagnostics.md | 2 ++ src/Check/Memcache.php | 7 +++++-- src/Check/Memcached.php | 7 +++++-- test/MemcacheTest.php | 33 +++++++++++++++++++++++++++++++++ test/MemcachedTest.php | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 4 deletions(-) diff --git a/docs/book/diagnostics.md b/docs/book/diagnostics.md index b609c2a..a2b4227 100644 --- a/docs/book/diagnostics.md +++ b/docs/book/diagnostics.md @@ -336,6 +336,7 @@ use Laminas\Diagnostics\Check\Memcache; $checkLocal = new Memcache('127.0.0.1'); // default port $checkBackup = new Memcache('10.0.30.40', 11212); +$checkSocket = new Memcache('unix:///run/memcached/memcached.sock', 0); ``` ## Memcached @@ -348,6 +349,7 @@ use Laminas\Diagnostics\Check\Memcached; $checkLocal = new Memcached('127.0.0.1'); // default port $checkBackup = new Memcached('10.0.30.40', 11212); +$checkSocket = new Memcached('/run/memcached/memcached.sock', 0); ``` ## MongoDb diff --git a/src/Check/Memcache.php b/src/Check/Memcache.php index 21159df..86e2680 100644 --- a/src/Check/Memcache.php +++ b/src/Check/Memcache.php @@ -28,8 +28,11 @@ class Memcache extends AbstractCheck protected $port; /** - * @param string $host - * @param int $port + * @param string $host The hostname of the memcache server. This parameter may + * also specify other transports like unix:///run/memcached/memcached.sock + * to use UNIX domain sockets, in this case port must also be set to 0. + * @param int $port The port where memcached is listening for connections. + * Set this parameter to 0 when using UNIX domain sockets. * @throws InvalidArgumentException */ public function __construct($host = '127.0.0.1', $port = 11211) diff --git a/src/Check/Memcached.php b/src/Check/Memcached.php index cb09b28..5f446be 100644 --- a/src/Check/Memcached.php +++ b/src/Check/Memcached.php @@ -27,8 +27,11 @@ class Memcached extends AbstractCheck protected $port; /** - * @param string $host - * @param int $port + * @param string $host The hostname of the memcache server. This parameter may + * also specify other transports like /path/to/memcached.sock + * to use UNIX domain sockets, in this case port must also be set to 0. + * @param int $port The port where memcached is listening for connections. + * Set this parameter to 0 when using UNIX domain sockets. * @throws InvalidArgumentException If host is not a string value. * @throws InvalidArgumentException If port is less than 0. */ diff --git a/test/MemcacheTest.php b/test/MemcacheTest.php index 5ec9e97..317b468 100644 --- a/test/MemcacheTest.php +++ b/test/MemcacheTest.php @@ -2,6 +2,7 @@ namespace LaminasTest\Diagnostics; +use Generator; use InvalidArgumentException; use Laminas\Diagnostics\Check\Memcache; use PHPUnit\Framework\TestCase; @@ -22,4 +23,36 @@ public function testPortValidation(): void $this->expectExceptionMessage("Invalid port number -11211 - expecting an unsigned integer"); new Memcache('127.0.0.1', -11211); } + + /** + * @dataProvider providerValidConstructorArguments + */ + public function testConstructor(array $arguments): void + { + new Memcache(...$arguments); + + $this->expectNotToPerformAssertions(); + } + + public function providerValidConstructorArguments(): Generator + { + yield 'no arguments' => [ + [], + ]; + yield 'only host' => [ + ['127.0.0.1'], + ]; + yield 'host and port' => [ + [ + '127.0.0.1', + 11211, + ], + ]; + yield 'unix socket' => [ + [ + 'unix:///run/memcached/memcached.sock', + 0, + ], + ]; + } } diff --git a/test/MemcachedTest.php b/test/MemcachedTest.php index cd96294..f6fc0c2 100644 --- a/test/MemcachedTest.php +++ b/test/MemcachedTest.php @@ -2,6 +2,7 @@ namespace LaminasTest\Diagnostics; +use Generator; use InvalidArgumentException; use Laminas\Diagnostics\Check\Memcached; use PHPUnit\Framework\TestCase; @@ -22,4 +23,36 @@ public function testPortValidation(): void $this->expectExceptionMessage("Invalid port number -11211 - expecting an unsigned integer"); new Memcached('127.0.0.1', -11211); } + + /** + * @dataProvider providerValidConstructorArguments + */ + public function testConstructor(array $arguments): void + { + new Memcached(...$arguments); + + $this->expectNotToPerformAssertions(); + } + + public function providerValidConstructorArguments(): Generator + { + yield 'no arguments' => [ + [], + ]; + yield 'only host' => [ + ['127.0.0.1'], + ]; + yield 'host and port' => [ + [ + '127.0.0.1', + 11211, + ], + ]; + yield 'unix socket' => [ + [ + '/run/memcached/memcached.sock', + 0, + ], + ]; + } } From 13094677c242b669b92e30cd26fe61ea0a265ead Mon Sep 17 00:00:00 2001 From: Oleksandr Khnykin Date: Wed, 14 Dec 2022 17:22:51 +0200 Subject: [PATCH 4/5] fix phpdoc by CR tips Signed-off-by: Oleksandr Khnykin --- src/Check/Memcache.php | 10 +++++----- src/Check/Memcached.php | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Check/Memcache.php b/src/Check/Memcache.php index 86e2680..c1f8d0f 100644 --- a/src/Check/Memcache.php +++ b/src/Check/Memcache.php @@ -28,11 +28,11 @@ class Memcache extends AbstractCheck protected $port; /** - * @param string $host The hostname of the memcache server. This parameter may - * also specify other transports like unix:///run/memcached/memcached.sock - * to use UNIX domain sockets, in this case port must also be set to 0. - * @param int $port The port where memcached is listening for connections. - * Set this parameter to 0 when using UNIX domain sockets. + * @param string $host The hostname of the memcache server. This parameter may + * also specify other transports like unix:///run/memcached/memcached.sock + * to use UNIX domain sockets, in this case port must also be set to 0. + * @param 0|positive-int $port The port where memcached is listening for connections. + * Set this parameter to 0 when using UNIX domain sockets. * @throws InvalidArgumentException */ public function __construct($host = '127.0.0.1', $port = 11211) diff --git a/src/Check/Memcached.php b/src/Check/Memcached.php index 5f446be..0c36d28 100644 --- a/src/Check/Memcached.php +++ b/src/Check/Memcached.php @@ -27,11 +27,11 @@ class Memcached extends AbstractCheck protected $port; /** - * @param string $host The hostname of the memcache server. This parameter may - * also specify other transports like /path/to/memcached.sock - * to use UNIX domain sockets, in this case port must also be set to 0. - * @param int $port The port where memcached is listening for connections. - * Set this parameter to 0 when using UNIX domain sockets. + * @param string $host The hostname of the memcache server. This parameter may + * also specify other transports like /path/to/memcached.sock + * to use UNIX domain sockets, in this case port must also be set to 0. + * @param 0|positive-int $port The port where memcached is listening for connections. + * Set this parameter to 0 when using UNIX domain sockets. * @throws InvalidArgumentException If host is not a string value. * @throws InvalidArgumentException If port is less than 0. */ From c12eebd3b93ce91e7209cd4290ac99fba54bda86 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Thu, 15 Dec 2022 17:58:18 +0100 Subject: [PATCH 5/5] Simplified and documented test types, re-generated baseline --- psalm-baseline.xml | 48 +++++++++++++++++++++++++++++++++++------- test/MemcacheTest.php | 40 ++++++++++++++++++++--------------- test/MemcachedTest.php | 40 ++++++++++++++++++++--------------- 3 files changed, 86 insertions(+), 42 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index a0a1ca2..4c9790d 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -437,18 +437,21 @@ - + + $port < 0 is_string($host) - + $memcache - $stats addServer connect getExtendedStats + + $port + Memcache @@ -457,18 +460,21 @@ - + + $port < 0 is_string($host) - + $memcached - $stats addServer getLastDisconnectedServer getStats + + $port + Memcached @@ -602,9 +608,33 @@ + + $client + + + PredisClient|RedisExtensionClient + + + auth + connect + + + $client + Redis + + RedisException + + + $client + PredisClient|RedisExtensionClient + RedisException + + + array + @@ -1039,7 +1069,8 @@ - + + -11211 ['127.0.0.1'] @@ -1047,7 +1078,8 @@ - + + -11211 ['127.0.0.1'] diff --git a/test/MemcacheTest.php b/test/MemcacheTest.php index 317b468..6f010cd 100644 --- a/test/MemcacheTest.php +++ b/test/MemcacheTest.php @@ -2,7 +2,6 @@ namespace LaminasTest\Diagnostics; -use Generator; use InvalidArgumentException; use Laminas\Diagnostics\Check\Memcache; use PHPUnit\Framework\TestCase; @@ -26,6 +25,7 @@ public function testPortValidation(): void /** * @dataProvider providerValidConstructorArguments + * @param array|array{string}|array{string, positive-int|0} $arguments */ public function testConstructor(array $arguments): void { @@ -34,24 +34,30 @@ public function testConstructor(array $arguments): void $this->expectNotToPerformAssertions(); } - public function providerValidConstructorArguments(): Generator + /** + * @return non-empty-array< + * non-empty-string, + * array{array|array{string}|array{string, positive-int|0}} + * > + */ + public static function providerValidConstructorArguments(): array { - yield 'no arguments' => [ - [], - ]; - yield 'only host' => [ - ['127.0.0.1'], - ]; - yield 'host and port' => [ - [ - '127.0.0.1', - 11211, + return [ + 'no arguments' => [[]], + 'only host' => [ + ['127.0.0.1'], ], - ]; - yield 'unix socket' => [ - [ - 'unix:///run/memcached/memcached.sock', - 0, + 'host and port' => [ + [ + '127.0.0.1', + 11211, + ], + ], + 'unix socket' => [ + [ + 'unix:///run/memcached/memcached.sock', + 0, + ], ], ]; } diff --git a/test/MemcachedTest.php b/test/MemcachedTest.php index f6fc0c2..1849a8c 100644 --- a/test/MemcachedTest.php +++ b/test/MemcachedTest.php @@ -2,7 +2,6 @@ namespace LaminasTest\Diagnostics; -use Generator; use InvalidArgumentException; use Laminas\Diagnostics\Check\Memcached; use PHPUnit\Framework\TestCase; @@ -26,6 +25,7 @@ public function testPortValidation(): void /** * @dataProvider providerValidConstructorArguments + * @param array|array{string}|array{string, positive-int|0} $arguments */ public function testConstructor(array $arguments): void { @@ -34,24 +34,30 @@ public function testConstructor(array $arguments): void $this->expectNotToPerformAssertions(); } - public function providerValidConstructorArguments(): Generator + /** + * @return non-empty-array< + * non-empty-string, + * array{array|array{string}|array{string, positive-int|0}} + * > + */ + public static function providerValidConstructorArguments(): array { - yield 'no arguments' => [ - [], - ]; - yield 'only host' => [ - ['127.0.0.1'], - ]; - yield 'host and port' => [ - [ - '127.0.0.1', - 11211, + return [ + 'no arguments' => [[]], + 'only host' => [ + ['127.0.0.1'], ], - ]; - yield 'unix socket' => [ - [ - '/run/memcached/memcached.sock', - 0, + 'host and port' => [ + [ + '127.0.0.1', + 11211, + ], + ], + 'unix socket' => [ + [ + 'unix:///run/memcached/memcached.sock', + 0, + ], ], ]; }