Skip to content

Commit

Permalink
Tests: refactor for better edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Jul 3, 2023
1 parent 24c0b87 commit 778d379
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 194 deletions.
42 changes: 42 additions & 0 deletions tests/Cases/DI/DbalExtension.cache.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\DI;

use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Contributte\Tester\Utils\Neonkit;
use Nette\DI\Compiler;
use Nette\DI\ServiceCreationException;
use Nettrine\DBAL\DI\DbalExtension;
use Tester\Assert;
use Tests\Toolkit\Tests;
use Tracy\Bridges\Nette\TracyExtension;

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

// Exception (no cache extension)
Toolkit::test(function (): void {
Assert::exception(
function (): void {
ContainerBuilder::of()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addExtension('nettrine.dbal', new DbalExtension());
$compiler->addExtension('nette.tracy', new TracyExtension());
$compiler->addConfig([
'parameters' => [
'tempDir' => Tests::TEMP_PATH,
'appDir' => Tests::APP_PATH,
],
]);
$compiler->addConfig(Neonkit::load(<<<'NEON'
nettrine.dbal:
connection:
driver: pdo_sqlite
NEON
));
})->build();
},
ServiceCreationException::class,
"~^Service 'nettrine\\.dbal\\.configuration' \\(type of Doctrine\\\\DBAL\\\\Configuration\\): Service of type '?Doctrine\\\\Common\\\\Cache\\\\Cache'? not found\.~"
);
});
48 changes: 48 additions & 0 deletions tests/Cases/DI/DbalExtension.debugMode.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\E2E;

use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Contributte\Tester\Utils\Liberator;
use Contributte\Tester\Utils\Neonkit;
use Nette\DI\Compiler;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\DI\DbalExtension;
use Tester\Assert;
use Tests\Toolkit\Tests;
use Tracy\Bridges\Nette\TracyExtension;
use Tracy\Debugger;

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

// Debug mode
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addExtension('nettrine.dbal', new DbalExtension());
$compiler->addExtension('nettrine.cache', new CacheExtension());
$compiler->addExtension('nette.tracy', new TracyExtension());
$compiler->addConfig([
'parameters' => [
'tempDir' => Tests::TEMP_PATH,
'appDir' => Tests::APP_PATH,
],
]);
$compiler->addConfig(Neonkit::load(<<<'NEON'
nettrine.dbal:
connection:
driver: pdo_sqlite
debug:
panel: true
NEON
));
})->build();

call_user_func([$container, 'initialize']);

$blueScreen = Debugger::getBlueScreen();
$panels = Liberator::of($blueScreen)->panels;

Assert::count(1, $panels);
});
22 changes: 22 additions & 0 deletions tests/Cases/DI/DbalExtension.driver.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\DI;

use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Nette\DI\Compiler;
use Nette\DI\InvalidConfigurationException;
use Nettrine\DBAL\DI\DbalExtension;
use Tester\Assert;

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

// Exception (no driver)
Toolkit::test(function (): void {
Assert::exception(function (): void {
ContainerBuilder::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.");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use Contributte\Tester\Utils\Neonkit;
use Doctrine\DBAL\Connection;
use Nette\DI\Compiler;
use Nette\DI\InvalidConfigurationException;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\ConnectionAccessor;
use Nettrine\DBAL\DI\DbalExtension;
use Nettrine\DBAL\Logger\ProfilerLogger;
use Tester\Assert;
use Tests\Fixtures\Driver\TestDriver;

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

Expand All @@ -23,6 +23,9 @@
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(Neonkit::load('
dbal:
configuration:
middlewares:
test: Tests\Fixtures\Middleware\TestMiddleware
connection:
driver: pdo_sqlite
'));
Expand All @@ -36,22 +39,25 @@

/** @var Connection $connection */
$connection = $container->getByType(Connection::class);
Assert::type(TestDriver::class, $connection->getDriver());
});

$connection->getConfiguration()->setMiddlewares(new ProfilerLogger($container->getByType(ConnectionAccessor::class)));

Assert::noError(function () use ($connection): void {
// Orm insert queries have starting index 1, if ExpandArrayParameters would use parameters, it would throw an exception
$connection->getConfiguration()->getSQLLogger()->startQuery('INSERT INTO person (id, lastname, firstname) VALUES (?, ?, ?)', [
1 => 1,
2 => 'John',
3 => 'Doe',
]);
});

Assert::noError(function () use ($connection): void {
$connection->getConfiguration()->getSQLLogger()->startQuery('UPDATE person SET firstname = ?, lastname = ?', [
0 => 'John',
1 => 'Doe',
]);
});
Toolkit::test(function (): void {
Assert::exception(
function (): void {
ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(Neonkit::load('
dbal:
configuration:
middlewares:
- Invalid
'));
})
->build();
},
InvalidConfigurationException::class,
"The key of item 'dbal › configuration › middlewares › 0' expects to be string, 0 given."
);
});
147 changes: 0 additions & 147 deletions tests/Cases/DI/DbalExtension.phpt

This file was deleted.

46 changes: 46 additions & 0 deletions tests/Cases/DI/DbalExtension.platform.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\DI;

use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Contributte\Tester\Utils\Neonkit;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
use Nette\DI\Compiler;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\DI\DbalExtension;
use Tester\Assert;
use Tests\Toolkit\Tests;
use Tracy\Bridges\Nette\TracyExtension;

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

// Server version
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addExtension('nettrine.dbal', new DbalExtension());
$compiler->addExtension('nettrine.cache', new CacheExtension());
$compiler->addExtension('nette.tracy', new TracyExtension());
$compiler->addConfig([
'parameters' => [
'tempDir' => Tests::TEMP_PATH,
'appDir' => Tests::APP_PATH,
],
]);
$compiler->addConfig(Neonkit::load(<<<'NEON'
nettrine.dbal:
connection:
driver: pdo_pgsql
serverVersion: 10.0
NEON
));
})->build();

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

Assert::type(PostgreSQL100Platform::class, $connection->getDatabasePlatform());
Assert::falsey($connection->isConnected());
});
Loading

0 comments on commit 778d379

Please sign in to comment.