diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 72cfeed..a92323f 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,10 +1,5 @@ - - - - - diff --git a/src/RedisAuthenticationInfo.php b/src/RedisAuthenticationInfo.php index d029450..8c2f3c3 100644 --- a/src/RedisAuthenticationInfo.php +++ b/src/RedisAuthenticationInfo.php @@ -19,32 +19,16 @@ private function __construct( ) { } - public static function fromRedisClusterOptions(RedisClusterOptions $options): self|null + public static function fromOptions(RedisClusterOptions|RedisOptions $options): self|null { $username = $options->getUser(); $password = $options->getPassword(); - if ($password === '') { + if ($password === null || $password === '') { return null; } - if ($username === '') { - return new self(null, $password); - } - - return new self($username, $password); - } - - public static function fromRedisOptions(RedisOptions $options): self|null - { - $username = $options->getUser(); - $password = $options->getPassword(); - - if ($password === null) { - return null; - } - - if ($username === null) { + if ($username === null || $username === '') { return new self(null, $password); } @@ -56,7 +40,7 @@ public static function fromRedisOptions(RedisOptions $options): self|null * * @return array{0:non-empty-string,1?:non-empty-string} */ - public function toRedisAuthInfo(): array + public function toArray(): array { if ($this->username === null) { return [$this->password]; diff --git a/src/RedisClusterResourceManager.php b/src/RedisClusterResourceManager.php index f676f74..bc0dffc 100644 --- a/src/RedisClusterResourceManager.php +++ b/src/RedisClusterResourceManager.php @@ -48,7 +48,7 @@ public function getResource(): RedisClusterFromExtension private function createRedisResource(RedisClusterOptions $options): RedisClusterFromExtension { - $authentication = RedisAuthenticationInfo::fromRedisClusterOptions($options); + $authenticationInfo = RedisAuthenticationInfo::fromOptions($options); if ($options->hasName()) { return $this->createRedisResourceFromName( @@ -56,7 +56,7 @@ private function createRedisResource(RedisClusterOptions $options): RedisCluster $options->getTimeout(), $options->getReadTimeout(), $options->isPersistent(), - $authentication, + $authenticationInfo, $options->getSslContext() ); } @@ -74,7 +74,7 @@ private function createRedisResource(RedisClusterOptions $options): RedisCluster $options->getTimeout(), $options->getReadTimeout(), $options->isPersistent(), - $authentication?->toRedisAuthInfo(), + $authenticationInfo?->toArray(), $options->getSslContext()?->toSslContextArray() ); } @@ -102,7 +102,7 @@ private function createRedisResourceFromName( $timeout = $options->getTimeout($name, $fallbackTimeout); $readTimeout = $options->getReadTimeout($name, $fallbackReadTimeout); $password = $options->getPasswordByName($name, ''); - $authentication = $password === '' ? $fallbackAuthentication?->toRedisAuthInfo() : $password; + $authentication = $password === '' ? $fallbackAuthentication?->toArray() : $password; /** * Psalm currently (<= 5.26.1) uses an outdated (phpredis < 5.3.2) constructor signature for the RedisCluster diff --git a/src/RedisResourceManager.php b/src/RedisResourceManager.php index d8d3cd6..24ba111 100644 --- a/src/RedisResourceManager.php +++ b/src/RedisResourceManager.php @@ -57,14 +57,14 @@ private function createRedisFromExtension(RedisOptions $options): RedisFromExten $port = $server['port'] ?? self::DEFAULT_REDIS_PORT; } - $authentication = RedisAuthenticationInfo::fromRedisOptions($options); + $authenticationInfo = RedisAuthenticationInfo::fromOptions($options); $resourceOptions = [ 'host' => $host, 'port' => $port, 'connectTimeout' => $server['timeout'] ?? null, 'persistent' => $options->getPersistentId() ?? $options->isPersistent(), - 'auth' => $authentication?->toRedisAuthInfo(), + 'auth' => $authenticationInfo?->toArray(), ]; $resource = new RedisFromExtension(array_filter($resourceOptions, fn (mixed $value) => $value !== null)); diff --git a/test/unit/RedisAuthProviderTest.php b/test/unit/RedisAuthProviderTest.php deleted file mode 100644 index d97bbbd..0000000 --- a/test/unit/RedisAuthProviderTest.php +++ /dev/null @@ -1,70 +0,0 @@ - $user, 'password' => $password])); - /** @psalm-suppress InternalMethod,InternalClass We are explicitly testing internal method here */ - $actualAuthentication = RedisAuthenticationInfo::fromRedisOptions($options); - /** @psalm-suppress InternalMethod We are explicitly testing internal method here */ - self::assertEquals($expectedAuthentication, $actualAuthentication?->toRedisAuthInfo()); - } - - /** - * @psalm-suppress PossiblyUnusedMethod PHPUnit psalm plugin does not yet support attributes - * @psalm-return non-empty-array - */ - public static function validAuthenticationInfo(): array - { - return [ - 'user and password' => [ - self::DUMMY_USER, - self::DUMMY_PASSWORD, - [self::DUMMY_USER, self::DUMMY_PASSWORD], - ], - 'only password (user is empty string)' => [ - '', - self::DUMMY_PASSWORD, - [self::DUMMY_PASSWORD], - ], - 'no authentication provided (empty strings)' => [ - '', - '', - null, - ], - 'only password (user is null)' => [ - null, - self::DUMMY_PASSWORD, - [self::DUMMY_PASSWORD], - ], - 'no authentication provided (null values)' => [ - null, - null, - null, - ], - ]; - } -} diff --git a/test/unit/RedisAuthenticationInfoTest.php b/test/unit/RedisAuthenticationInfoTest.php new file mode 100644 index 0000000..8809f0f --- /dev/null +++ b/test/unit/RedisAuthenticationInfoTest.php @@ -0,0 +1,100 @@ + $user, 'password' => $password], + fn($element) => $element !== null + )); + /** @psalm-suppress InternalMethod,InternalClass We are explicitly testing an internal method here */ + $actualAuthentication = RedisAuthenticationInfo::fromOptions($options); + /** @psalm-suppress InternalMethod We are explicitly testing an internal method here */ + self::assertEquals($expectedAuthentication, $actualAuthentication?->toArray()); + } + + #[DataProvider('authenticationInfo')] + public function testUserAndPasswordCombinationsForRedisCluster( + ?string $user, + ?string $password, + array|null $expectedAuthentication + ): void { + $options = new RedisClusterOptions(array_filter( + ['user' => $user, 'password' => $password, 'name' => 'test'], + fn($element) => $element !== null + )); + /** @psalm-suppress InternalMethod,InternalClass We are explicitly testing an internal method here */ + $actualAuthentication = RedisAuthenticationInfo::fromOptions($options); + /** @psalm-suppress InternalMethod We are explicitly testing an internal method here */ + self::assertEquals($expectedAuthentication, $actualAuthentication?->toArray()); + } + + /** + * @psalm-suppress PossiblyUnusedMethod PHPUnit psalm plugin does not yet support attributes + * @psalm-return non-empty-array + */ + public static function authenticationInfo(): array + { + return [ + 'user and password' => [ + self::DUMMY_USER, + self::DUMMY_PASSWORD, + [self::DUMMY_USER, self::DUMMY_PASSWORD], + ], + 'only password (user is empty string)' => [ + '', + self::DUMMY_PASSWORD, + [self::DUMMY_PASSWORD], + ], + 'only password (user is null)' => [ + null, + self::DUMMY_PASSWORD, + [self::DUMMY_PASSWORD], + ], + 'no authentication provided (empty strings)' => [ + '', + '', + null, + ], + 'no authentication provided (null values)' => [ + null, + null, + null, + ], + 'user without a password (password is empty string)' => [ + self::DUMMY_USER, + '', + null, + ], + 'user without a password (password is null)' => [ + self::DUMMY_USER, + null, + null, + ], + ]; + } +}