-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: cover compiler extension with more tests
- Loading branch information
Showing
11 changed files
with
343 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Cases\DI; | ||
|
||
use Contributte\Redis\Caching\RedisJournal; | ||
use Contributte\Redis\Caching\RedisStorage; | ||
use Contributte\Redis\DI\RedisExtension; | ||
use Nette\Bridges\CacheDI\CacheExtension; | ||
use Nette\DI\Compiler; | ||
use Ninjify\Nunjuck\Toolkit; | ||
use Predis\Client; | ||
use Tester\Assert; | ||
use Tests\Toolkit\Container; | ||
use Tests\Toolkit\Helpers; | ||
use Tests\Toolkit\Liberator; | ||
use Tests\Toolkit\Tests; | ||
|
||
require_once __DIR__ . '/../../bootstrap.php'; | ||
|
||
// Connection | ||
Toolkit::test(function (): void { | ||
$container = Container::of() | ||
->withDefaults() | ||
->build(); | ||
|
||
Assert::type(Client::class, $container->getByName('redis.connection.default.client')); | ||
Assert::falsey($container->hasService('redis.connection.default.journal')); | ||
Assert::falsey($container->hasService('redis.connection.default.storage')); | ||
}); | ||
|
||
// Client + Storage + Journal | ||
Toolkit::test(function (): void { | ||
$container = Container::of() | ||
->withCompiler(function (Compiler $compiler): void { | ||
$compiler->addExtension('redis', new RedisExtension()); | ||
$compiler->addExtension('caching', new CacheExtension(Tests::TEMP_PATH)); | ||
$compiler->addConfig(Helpers::neon(' | ||
redis: | ||
connection: | ||
default: | ||
uri: tcp://127.0.0.1:6379 | ||
storage: true | ||
')); | ||
}) | ||
->build(); | ||
|
||
Assert::type(Client::class, $container->getByName('redis.connection.default.client')); | ||
Assert::type(RedisJournal::class, $container->getByName('redis.connection.default.journal')); | ||
Assert::type(RedisStorage::class, $container->getByName('redis.connection.default.storage')); | ||
}); | ||
|
||
// Client + Storage + Journal | ||
Toolkit::test(function (): void { | ||
$container = Container::of() | ||
->withCompiler(function (Compiler $compiler): void { | ||
$compiler->addExtension('redis', new RedisExtension()); | ||
$compiler->addExtension('caching', new CacheExtension(Tests::TEMP_PATH)); | ||
$compiler->addConfig(Helpers::neon(' | ||
redis: | ||
connection: | ||
default: | ||
uri: tcp://127.0.0.1:6379 | ||
storage: true | ||
')); | ||
}) | ||
->build(); | ||
|
||
Assert::type(Client::class, $container->getByName('redis.connection.default.client')); | ||
Assert::type(RedisJournal::class, $container->getByName('redis.connection.default.journal')); | ||
Assert::type(RedisStorage::class, $container->getByName('redis.connection.default.storage')); | ||
}); | ||
|
||
// Multiple connections | ||
Toolkit::test(function (): void { | ||
$container = Container::of() | ||
->withCompiler(function (Compiler $compiler): void { | ||
$compiler->addExtension('redis', new RedisExtension()); | ||
$compiler->addExtension('caching', new CacheExtension(Tests::TEMP_PATH)); | ||
$compiler->addConfig(Helpers::neon(' | ||
redis: | ||
connection: | ||
default: | ||
uri: tcp://127.0.0.1:1111 | ||
second: | ||
uri: tcp://127.0.0.2:2222 | ||
')); | ||
}) | ||
->build(); | ||
|
||
Assert::type(Client::class, $container->getByName('redis.connection.default.client')); | ||
Assert::falsey($container->hasService('redis.connection.default.journal')); | ||
Assert::falsey($container->hasService('redis.connection.default.storage')); | ||
|
||
Assert::type(Client::class, $container->getByName('redis.connection.second.client')); | ||
Assert::falsey($container->hasService('redis.connection.second.journal')); | ||
Assert::falsey($container->hasService('redis.connection.second.storage')); | ||
}); | ||
|
||
// Client + Storage + Journal | ||
Toolkit::test(function (): void { | ||
$container = Container::of() | ||
->withCompiler(function (Compiler $compiler): void { | ||
$compiler->addExtension('redis', new RedisExtension()); | ||
$compiler->addExtension('caching', new CacheExtension(Tests::TEMP_PATH)); | ||
$compiler->addConfig(Helpers::neon(' | ||
redis: | ||
connection: | ||
default: | ||
uri: tcp://127.0.0.1:1111 | ||
storage: true | ||
second: | ||
uri: tcp://127.0.0.2:2222 | ||
storage: true | ||
')); | ||
}) | ||
->build(); | ||
|
||
Assert::type(Client::class, $container->getByName('redis.connection.default.client')); | ||
Assert::type(RedisJournal::class, $container->getByName('redis.connection.default.journal')); | ||
Assert::type(RedisStorage::class, $container->getByName('redis.connection.default.storage')); | ||
|
||
Assert::type(Client::class, $container->getByName('redis.connection.second.client')); | ||
Assert::type(RedisJournal::class, $container->getByName('redis.connection.second.journal')); | ||
Assert::type(RedisStorage::class, $container->getByName('redis.connection.second.storage')); | ||
|
||
/** @var RedisStorage $storage1 */ | ||
$storage1 = $container->getByName('redis.connection.default.storage'); | ||
/** @var RedisStorage $storage2 */ | ||
$storage2 = $container->getByName('redis.connection.second.storage'); | ||
|
||
Assert::notSame(Liberator::of($storage1)->client, Liberator::of($storage2)->client); | ||
Assert::notSame(Liberator::of($storage1)->journal, Liberator::of($storage2)->journal); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Toolkit; | ||
|
||
use Contributte\Redis\DI\RedisExtension; | ||
use Nette\DI\Compiler; | ||
use Nette\DI\Container as NetteContainer; | ||
use Nette\DI\ContainerLoader; | ||
|
||
final class Container | ||
{ | ||
|
||
/** @var string */ | ||
private $key; | ||
|
||
/** @var callable[] */ | ||
private $onCompile = []; | ||
|
||
public function __construct(string $key) | ||
{ | ||
$this->key = $key; | ||
} | ||
|
||
public static function of(?string $key = null): Container | ||
{ | ||
return new static($key ?? uniqid(random_bytes(16))); | ||
} | ||
|
||
public function withDefaults(): Container | ||
{ | ||
$this->withDefaultExtensions(); | ||
$this->withDefaultParameters(); | ||
|
||
return $this; | ||
} | ||
|
||
public function withDefaultExtensions(): Container | ||
{ | ||
$this->onCompile[] = function (Compiler $compiler): void { | ||
$compiler->addExtension('redis', new RedisExtension()); | ||
}; | ||
|
||
return $this; | ||
} | ||
|
||
public function withDefaultParameters(): Container | ||
{ | ||
$this->onCompile[] = function (Compiler $compiler): void { | ||
$compiler->addConfig([ | ||
'parameters' => [ | ||
'tempDir' => Tests::TEMP_PATH, | ||
'appDir' => Tests::APP_PATH, | ||
], | ||
]); | ||
$compiler->addConfig(Helpers::neon(' | ||
redis: | ||
connection: | ||
default: | ||
uri: tcp://127.0.0.1:6379 | ||
')); | ||
}; | ||
|
||
return $this; | ||
} | ||
|
||
public function withCompiler(callable $cb): Container | ||
{ | ||
$this->onCompile[] = function (Compiler $compiler) use ($cb): void { | ||
$cb($compiler); | ||
}; | ||
|
||
return $this; | ||
} | ||
|
||
public function build(): NetteContainer | ||
{ | ||
$loader = new ContainerLoader(Tests::TEMP_PATH, true); | ||
$class = $loader->load(function (Compiler $compiler): void { | ||
foreach ($this->onCompile as $cb) { | ||
$cb($compiler); | ||
} | ||
}, $this->key); | ||
|
||
return new $class(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Toolkit; | ||
|
||
use Nette\DI\Config\Adapters\NeonAdapter; | ||
use Nette\Neon\Neon; | ||
|
||
final class Helpers | ||
{ | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public static function neon(string $str): array | ||
{ | ||
return (new NeonAdapter())->process((array) Neon::decode($str)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Toolkit; | ||
|
||
use ReflectionClass; | ||
|
||
final class Liberator | ||
{ | ||
|
||
/** @var object */ | ||
private $object; | ||
|
||
/** @var ReflectionClass */ | ||
private $class; | ||
|
||
/** | ||
* @param object $object | ||
*/ | ||
public function __construct($object, string $class) | ||
{ | ||
$this->object = $object; | ||
$this->class = new ReflectionClass($class); | ||
} | ||
|
||
/** | ||
* @param object $object | ||
*/ | ||
public static function of($object): self | ||
{ | ||
return new static($object, get_class($object)); | ||
} | ||
|
||
/** | ||
* @param object $object | ||
*/ | ||
public static function ofClass($object, string $class): self | ||
{ | ||
return new static($object, $class); | ||
} | ||
|
||
public function __isset(string $name): bool | ||
{ | ||
if (!$this->class->hasProperty($name)) { | ||
return false; | ||
} | ||
|
||
$property = $this->class->getProperty($name); | ||
$property->setAccessible(true); | ||
|
||
return /* $property->isInitialized($this->object) &&*/ $property->getValue($this->object) !== null; | ||
} | ||
|
||
public function __get(string $name) | ||
{ | ||
$property = $this->class->getProperty($name); | ||
$property->setAccessible(true); | ||
|
||
return $property->getValue($this->object); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function __set(string $name, $value): void | ||
{ | ||
$property = $this->class->getProperty($name); | ||
$property->setAccessible(true); | ||
$property->setValue($this->object, $value); | ||
} | ||
|
||
/** | ||
* @param mixed[] $args | ||
*/ | ||
public function __call(string $name, array $args = []) | ||
{ | ||
$method = $this->class->getMethod($name); | ||
$method->setAccessible(true); | ||
|
||
return $method->invokeArgs($this->object, $args); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Tests\Toolkit; | ||
|
||
final class Tests | ||
{ | ||
|
||
public const APP_PATH = __DIR__ . '/..'; | ||
public const TEMP_PATH = __DIR__ . '/../tmp'; | ||
|
||
} |