Skip to content

Commit

Permalink
Merge pull request #105 from PavelJurasek/php8-3
Browse files Browse the repository at this point in the history
Fix: Config schema normalization is not processed twice
  • Loading branch information
Spamercz authored Aug 24, 2021
2 parents 9f0a6bf + 90670fc commit 6402655
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Kdyby/Redis/DI/Config/ClientSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function __construct(\Nette\DI\ContainerBuilder $builder)

public function normalize($value, \Nette\Schema\Context $context)
{
$value = $this->getSchema()->normalize($value, $context);

if (\array_key_exists('host', $value) && $value['host'][0] === '/') {
$value['port'] = NULL; // sockets have no ports

Expand All @@ -36,7 +38,6 @@ public function complete($value, \Nette\Schema\Context $context)
{
$value = $this->expandParameters($value);

$value = $this->getSchema()->normalize($value, $context);
$value = $this->getSchema()->complete($value, $context);

return $value;
Expand Down
17 changes: 15 additions & 2 deletions src/Kdyby/Redis/DI/Config/RedisSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function complete($value, \Nette\Schema\Context $context)
{
$value = $this->expandParameters((array) $value);

$value = $this->normalize($value, $context);
$value = $this->getSchema()->complete($value, $context);

return $value;
Expand Down Expand Up @@ -89,7 +88,21 @@ private function getSchema(): \Nette\Schema\Schema
'session' => \Nette\Schema\Expect::bool(FALSE),
'clients' => \Nette\Schema\Expect::arrayOf(
new \Kdyby\Redis\DI\Config\ClientSchema($this->builder)
)->default([]),
)->default([
NULL => [
'host' => '127.0.0.1',
'port' => \Kdyby\Redis\RedisClient::DEFAULT_PORT,
'timeout' => 10,
'database' => 0,
'auth' => NULL,
'persistent' => FALSE,
'connectionAttempts' => 1,
'lockDuration' => 15,
'lockAcquireTimeout' => FALSE,
'debugger' => $this->builder->parameters['debugMode'],
'versionCheck' => TRUE,
],
]),
]);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Kdyby/Redis/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class RedisClient implements \ArrayAccess
private $host;

/**
* @var int
* @var ?int
*/
private $port;

Expand Down Expand Up @@ -218,14 +218,14 @@ class RedisClient implements \ArrayAccess

/**
* @param string $host
* @param int $port
* @param ?int $port
* @param int $database
* @param int $timeout
* @param string $auth
* @param bool $persistent
* @throws \Kdyby\Redis\Exception\MissingExtensionException
*/
public function __construct(string $host = '127.0.0.1', int $port = 6379, int $database = 0, int $timeout = 10, ?string $auth = NULL, bool $persistent = FALSE)
public function __construct(string $host = '127.0.0.1', ?int $port = 6379, int $database = 0, int $timeout = 10, ?string $auth = NULL, bool $persistent = FALSE)
{
$this->host = $host;
$this->port = $port;
Expand Down
20 changes: 20 additions & 0 deletions tests/KdybyTests/Redis/ExtensionTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ class ExtensionTest extends \Tester\TestCase
Assert::false($dic->hasService('redis.cacheStorage'));
}

public function testOfflineConfiguration(): void
{
$config = $this->createConfig();
$config->addConfig(__DIR__ . '/files/offline.neon');
$dic = $config->createContainer();
Assert::true($dic->getService('redis.client') instanceof Kdyby\Redis\RedisClient);
Assert::false($dic->hasService('redis.cacheJournal'));
Assert::false($dic->hasService('redis.cacheStorage'));
}

public function testSocketConfiguration(): void
{
$config = $this->createConfig();
$config->addConfig(__DIR__ . '/files/socket.neon');
$dic = $config->createContainer();
Assert::true($dic->getService('redis.client') instanceof Kdyby\Redis\RedisClient);
Assert::false($dic->hasService('redis.cacheJournal'));
Assert::false($dic->hasService('redis.cacheStorage'));
}

}

(new ExtensionTest())->run();
6 changes: 6 additions & 0 deletions tests/KdybyTests/Redis/files/offline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
redis:
journal: false
storage: false
session: false
versionCheck: false
host: localhost
6 changes: 6 additions & 0 deletions tests/KdybyTests/Redis/files/socket.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
redis:
journal: false
storage: false
session: false
versionCheck: false
host: /tmp/redis.sock

0 comments on commit 6402655

Please sign in to comment.