Skip to content

Commit

Permalink
Tests: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 3, 2023
1 parent a9e0dd6 commit b1876bf
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 233 deletions.
133 changes: 45 additions & 88 deletions tests/Cases/DI/DbalExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,46 @@
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nette\InvalidStateException;
use Nettrine\Cache\DI\CacheExtension;
use Nette\DI\InvalidConfigurationException;
use Nette\DI\ServiceCreationException;
use Nettrine\DBAL\DI\DbalExtension;
use Nettrine\DBAL\Events\DebugEventManager;
use Nettrine\DBAL\Logger\ProfilerLogger;
use Ninjify\Nunjuck\Toolkit;
use Tester\Assert;
use Tests\Toolkit\DoctrineDeprecations;
use Tests\Toolkit\NeonLoader;
use Tracy\Bridges\Nette\TracyExtension;
use Tests\Toolkit\Container;
use Tests\Toolkit\Helpers;

require_once __DIR__ . '/../../bootstrap.php';

// Debug mode
Toolkit::test(function (): void {
// Ignore deprecations that are impossible to fix while keeping DBAL 2.x support
DoctrineDeprecations::ignoreDeprecations(
'https://github.com/doctrine/dbal/pull/4967',
'https://github.com/doctrine/dbal/pull/4620'
);

$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('tracy', new TracyExtension());
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(NeonLoader::load('
dbal:
connection:
driver: pdo_sqlite
$container = Container::of()
->withDefaults()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addConfig(Helpers::neon(<<<'NEON'
nettrine.dbal:
debug:
panel: true
'));
$compiler->addConfig([
'parameters' => [
'tempDir' => TEMP_DIR,
],
]);
}, __FILE__ . '1');

/** @var Container $container */
$container = new $class();

/** @var Connection $connection */
$connection = $container->getByType(Connection::class);
NEON
));
})->build();

Assert::type(DebugEventManager::class, $connection->getEventManager());
Assert::type(ProfilerLogger::class, $container->getByType(ProfilerLogger::class));
});

// Server version
Toolkit::test(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(NeonLoader::load('
dbal:
$container = Container::of()
->withDefaults()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addConfig(Helpers::neon(<<<'NEON'
nettrine.dbal:
connection:
driver: pdo_pgsql
serverVersion: 10.0
'));
$compiler->addConfig([
'parameters' => [
'tempDir' => TEMP_DIR,
],
]);
}, __FILE__ . '2');

/** @var Container $container */
$container = new $class();
NEON
));
})->build();

/** @var Connection $connection */
$connection = $container->getByType(Connection::class);
Expand All @@ -89,27 +58,19 @@

// Types
Toolkit::test(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(NeonLoader::load('
dbal:
$container = Container::of()
->withDefaults()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addConfig(Helpers::neon(<<<'NEON'
nettrine.dbal:
connection:
driver: pdo_pgsql
types:
foo: { class: Doctrine\DBAL\Types\StringType }
bar: Doctrine\DBAL\Types\IntegerType
'));
$compiler->addConfig([
'parameters' => [
'tempDir' => TEMP_DIR,
],
]);
}, __FILE__ . '3');

/** @var Container $container */
$container = new $class();
NEON
));
})->build();

/** @var Connection $connection */
$connection = $container->getByType(Connection::class);
Expand All @@ -123,32 +84,28 @@
Toolkit::test(function (): void {
Assert::exception(
function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(NeonLoader::load('
dbal:
Container::of()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addExtension('nettrine.dbal', new DbalExtension());
$compiler->addConfig(Helpers::neon(<<<'NEON'
nettrine.dbal:
connection:
driver: pdo_sqlite
'));
}, __FILE__ . '4');

new $class();
NEON
));
})->build();
},
InvalidStateException::class,
"~^Service 'dbal\\.configuration' \\(type of Doctrine\\\\DBAL\\\\Configuration\\): Service of type '?Doctrine\\\\Common\\\\Cache\\\\Cache'? not found\.~"
ServiceCreationException::class,
"~^Service 'nettrine\\.dbal\\.configuration' \\(type of Doctrine\\\\DBAL\\\\Configuration\\): Service of type '?Doctrine\\\\Common\\\\Cache\\\\Cache'? not found\.~"
);
});


// Exception (no driver)
Toolkit::test(function (): void {
Assert::exception(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('dbal', new DbalExtension());
}, __FILE__ . '5');

new $class();
}, InvalidStateException::class, "The mandatory item 'dbal › connection › driver' is missing.");
Container::of()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addExtension('nettrine.dbal', new DbalExtension());
})->build();
}, InvalidConfigurationException::class, "The mandatory item 'nettrine.dbal › connection › driver' is missing.");
});
12 changes: 2 additions & 10 deletions tests/Cases/E2E/ProfilerLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,16 @@
use Nettrine\DBAL\Logger\ProfilerLogger;
use Ninjify\Nunjuck\Toolkit;
use Tester\Assert;
use Tests\Toolkit\DoctrineDeprecations;
use Tests\Toolkit\NeonLoader;
use Tests\Toolkit\Helpers;

require_once __DIR__ . '/../../bootstrap.php';

Toolkit::test(function (): void {
// Ignore deprecations that are impossible to fix while keeping DBAL 2.x support
DoctrineDeprecations::ignoreDeprecations(
'https://github.com/doctrine/dbal/pull/4967',
'https://github.com/doctrine/dbal/pull/4620',
'https://github.com/doctrine/dbal/pull/4578'
);

$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(NeonLoader::load('
$compiler->addConfig(Helpers::neon('
dbal:
connection:
driver: pdo_sqlite
Expand Down
12 changes: 2 additions & 10 deletions tests/Cases/E2E/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,18 @@
use Nettrine\DBAL\DI\DbalExtension;
use Ninjify\Nunjuck\Toolkit;
use Tester\Assert;
use Tests\Toolkit\DoctrineDeprecations;
use Tests\Toolkit\NeonLoader;
use Tests\Toolkit\Helpers;
use Tracy\Bridges\Nette\TracyExtension;

require_once __DIR__ . '/../../bootstrap.php';

Toolkit::test(function (): void {
// Ignore deprecations that are impossible to fix while keeping DBAL 2.x support
DoctrineDeprecations::ignoreDeprecations(
'https://github.com/doctrine/dbal/pull/4967',
'https://github.com/doctrine/dbal/pull/4620',
'https://github.com/doctrine/dbal/pull/4578'
);

$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('tracy', new TracyExtension());
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(NeonLoader::load('
$compiler->addConfig(Helpers::neon('
dbal:
connection:
driver: pdo_sqlite
Expand Down
11 changes: 2 additions & 9 deletions tests/Cases/Events/EventManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,16 @@
use Ninjify\Nunjuck\Toolkit;
use Tester\Assert;
use Tests\Fixtures\Subscriber\PostConnectSubscriber;
use Tests\Toolkit\DoctrineDeprecations;
use Tests\Toolkit\NeonLoader;
use Tests\Toolkit\Helpers;

require_once __DIR__ . '/../../bootstrap.php';

Toolkit::test(function (): void {
// Ignore deprecations that are impossible to fix while keeping DBAL 2.x support
DoctrineDeprecations::ignoreDeprecations(
'https://github.com/doctrine/dbal/pull/4967',
'https://github.com/doctrine/dbal/pull/4620'
);

$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(NeonLoader::load('
$compiler->addConfig(Helpers::neon('
dbal:
connection:
driver: pdo_sqlite
Expand Down
90 changes: 90 additions & 0 deletions tests/Toolkit/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php declare(strict_types = 1);

namespace Tests\Toolkit;

use Nette\DI\Compiler;
use Nette\DI\Container as NetteContainer;
use Nette\DI\ContainerLoader;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\DI\DbalExtension;
use Tracy\Bridges\Nette\TracyExtension;

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('nettrine.dbal', new DbalExtension());
$compiler->addExtension('nettrine.cache', new CacheExtension());
$compiler->addExtension('nette.tracy', new TracyExtension());
};

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('
nettrine.dbal:
connection:
driver: pdo_sqlite
'));
};

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();
}

}
Loading

0 comments on commit b1876bf

Please sign in to comment.