Skip to content

Commit

Permalink
Tests: cover compiler extension with more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Nov 4, 2021
1 parent 89dbf59 commit 53f0af1
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 9 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\Cases\\Redis\\": "tests"
"Tests\\Cases\\": "tests/cases",
"Tests\\Toolkit\\": "tests/toolkit"
}
},
"minimum-stability": "dev",
Expand Down
9 changes: 5 additions & 4 deletions ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<!-- Specific rules -->
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="rootNamespaces" type="array" value="
src=>Contributte\Redis,
tests=>Tests\Contributte\Redis
"/>
<property name="rootNamespaces" type="array">
<element key="src" value="Contributte\Redis"/>
<element key="tests/cases" value="Tests\Cases"/>
<element key="tests/toolkit" value="Tests\Toolkit"/>
</property>
</properties>
</rule>

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/Caching/RedisStorage.phpt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\Redis\Caching;
namespace Tests\Cases\Caching;

use Contributte\Redis\Caching\RedisStorage;
use Mockery;
Expand Down
133 changes: 133 additions & 0 deletions tests/cases/DI/RedisExtension.phpt
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);
});
2 changes: 1 addition & 1 deletion tests/cases/E2E/Predis.phpt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\Redis\Caching;
namespace Tests\Cases\Caching;

use Contributte\Redis\Caching\RedisJournal;
use Contributte\Redis\Caching\RedisStorage;
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/Serializer/DefaultSerializer.phpt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\Redis\Serializer;
namespace Tests\Cases\Serializer;

use Contributte\Redis\Serializer\DefaultSerializer;
use Ninjify\Nunjuck\Toolkit;
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/Serializer/IgbinarySerializer.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @phpExtension igbinary
*/

namespace Tests\Cases\Redis\Serializer;
namespace Tests\Cases\Serializer;

use Contributte\Redis\Serializer\IgbinarySerializer;
use Ninjify\Nunjuck\Toolkit;
Expand Down
87 changes: 87 additions & 0 deletions tests/toolkit/Container.php
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();
}

}
19 changes: 19 additions & 0 deletions tests/toolkit/Helpers.php
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));
}

}
82 changes: 82 additions & 0 deletions tests/toolkit/Liberator.php
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);
}

}
11 changes: 11 additions & 0 deletions tests/toolkit/Tests.php
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';

}

0 comments on commit 53f0af1

Please sign in to comment.