Skip to content

Commit

Permalink
refactoring of the authentication helper (part 2)
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Brabants <[email protected]>
  • Loading branch information
robin-brabants committed Nov 25, 2024
1 parent 0a07dbd commit b0e021e
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 101 deletions.
5 changes: 0 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
<file src="src/Exception/InvalidRedisClusterConfigurationException.php">
<PossiblyUnusedMethod>
<code><![CDATA[fromInvalidSeedsConfiguration]]></code>
</PossiblyUnusedMethod>
</file>
<file src="src/Redis.php">
<InvalidOperand>
<code><![CDATA[$pttl]]></code>
Expand Down
24 changes: 4 additions & 20 deletions src/RedisAuthenticationInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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];
Expand Down
8 changes: 4 additions & 4 deletions src/RedisClusterResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ public function getResource(): RedisClusterFromExtension

private function createRedisResource(RedisClusterOptions $options): RedisClusterFromExtension
{
$authentication = RedisAuthenticationInfo::fromRedisClusterOptions($options);
$authenticationInfo = RedisAuthenticationInfo::fromOptions($options);

if ($options->hasName()) {
return $this->createRedisResourceFromName(
$options->getName(),
$options->getTimeout(),
$options->getReadTimeout(),
$options->isPersistent(),
$authentication,
$authenticationInfo,
$options->getSslContext()
);
}
Expand All @@ -74,7 +74,7 @@ private function createRedisResource(RedisClusterOptions $options): RedisCluster
$options->getTimeout(),
$options->getReadTimeout(),
$options->isPersistent(),
$authentication?->toRedisAuthInfo(),
$authenticationInfo?->toArray(),
$options->getSslContext()?->toSslContextArray()
);
}
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/RedisResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
70 changes: 0 additions & 70 deletions test/unit/RedisAuthProviderTest.php

This file was deleted.

100 changes: 100 additions & 0 deletions test/unit/RedisAuthenticationInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Cache\Storage\Adapter;

use Laminas\Cache\Storage\Adapter\RedisAuthenticationInfo;
use Laminas\Cache\Storage\Adapter\RedisClusterOptions;
use Laminas\Cache\Storage\Adapter\RedisOptions;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

use function array_filter;

final class RedisAuthenticationInfoTest extends TestCase
{
private const DUMMY_USER = 'user';
private const DUMMY_PASSWORD = 'password';

#[DataProvider('authenticationInfo')]
public function testUserAndPasswordCombinationsForRedis(
?string $user,
?string $password,
array|null $expectedAuthentication
): void {
$options = new RedisOptions(array_filter(
['user' => $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<non-empty-string, array{
* 0:string|null,
* 1:string|null,
* 2:array{0: non-empty-string, 1?: non-empty-string}|null
* }>
*/
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,
],
];
}
}

0 comments on commit b0e021e

Please sign in to comment.