diff --git a/tests/Cases/DI/DbalExtension.cache.phpt b/tests/Cases/DI/DbalExtension.cache.phpt new file mode 100644 index 0000000..dd8c372 --- /dev/null +++ b/tests/Cases/DI/DbalExtension.cache.phpt @@ -0,0 +1,42 @@ +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\.~" + ); +}); diff --git a/tests/Cases/DI/DbalExtension.debugMode.phpt b/tests/Cases/DI/DbalExtension.debugMode.phpt new file mode 100644 index 0000000..08b6c87 --- /dev/null +++ b/tests/Cases/DI/DbalExtension.debugMode.phpt @@ -0,0 +1,48 @@ +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); +}); diff --git a/tests/Cases/DI/DbalExtension.driver.phpt b/tests/Cases/DI/DbalExtension.driver.phpt new file mode 100644 index 0000000..c4fcde6 --- /dev/null +++ b/tests/Cases/DI/DbalExtension.driver.phpt @@ -0,0 +1,22 @@ +withCompiler(static function (Compiler $compiler): void { + $compiler->addExtension('nettrine.dbal', new DbalExtension()); + })->build(); + }, InvalidConfigurationException::class, "The mandatory item 'nettrine.dbal › connection › driver' is missing."); +}); diff --git a/tests/Cases/E2E/ProfilerLoggerTest.php b/tests/Cases/DI/DbalExtension.middlewares.phpt similarity index 55% rename from tests/Cases/E2E/ProfilerLoggerTest.php rename to tests/Cases/DI/DbalExtension.middlewares.phpt index 7c827bb..feced77 100644 --- a/tests/Cases/E2E/ProfilerLoggerTest.php +++ b/tests/Cases/DI/DbalExtension.middlewares.phpt @@ -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'; @@ -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 ')); @@ -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." + ); }); diff --git a/tests/Cases/DI/DbalExtension.phpt b/tests/Cases/DI/DbalExtension.phpt deleted file mode 100644 index c8fd55c..0000000 --- a/tests/Cases/DI/DbalExtension.phpt +++ /dev/null @@ -1,147 +0,0 @@ -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(); - - Assert::type(ProfilerLogger::class, $container->getByType(ProfilerLogger::class)); -}); - -// 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()); -}); - -// Types -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 - types: - foo: { class: Doctrine\DBAL\Types\StringType } - bar: Doctrine\DBAL\Types\IntegerType - NEON - )); - })->build(); - - /** @var Connection $connection */ - $connection = $container->getByType(Connection::class); - - Assert::type(Connection::class, $connection); - Assert::type(StringType::class, Type::getType('foo')); - Assert::type(IntegerType::class, Type::getType('bar')); -}); - -// 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\.~" - ); -}); - -// 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."); -}); diff --git a/tests/Cases/DI/DbalExtension.platform.phpt b/tests/Cases/DI/DbalExtension.platform.phpt new file mode 100644 index 0000000..8b5157a --- /dev/null +++ b/tests/Cases/DI/DbalExtension.platform.phpt @@ -0,0 +1,46 @@ +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()); +}); diff --git a/tests/Cases/DI/DbalExtension.types.phpt b/tests/Cases/DI/DbalExtension.types.phpt new file mode 100644 index 0000000..2846d34 --- /dev/null +++ b/tests/Cases/DI/DbalExtension.types.phpt @@ -0,0 +1,51 @@ +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 + types: + foo: { class: Doctrine\DBAL\Types\StringType } + bar: Doctrine\DBAL\Types\IntegerType + NEON + )); + })->build(); + + /** @var Connection $connection */ + $connection = $container->getByType(Connection::class); + + Assert::type(Connection::class, $connection); + Assert::type(StringType::class, Type::getType('foo')); + Assert::type(IntegerType::class, Type::getType('bar')); +}); diff --git a/tests/Cases/E2E/QueryTest.php b/tests/Cases/E2E/QueryTest.phpt similarity index 100% rename from tests/Cases/E2E/QueryTest.php rename to tests/Cases/E2E/QueryTest.phpt diff --git a/tests/Fixtures/Driver/TestDriver.php b/tests/Fixtures/Driver/TestDriver.php new file mode 100644 index 0000000..a8be544 --- /dev/null +++ b/tests/Fixtures/Driver/TestDriver.php @@ -0,0 +1,23 @@ +connection = parent::connect($params); + + return $this->connection; + } + +} diff --git a/tests/Fixtures/Middleware/TestMiddleware.php b/tests/Fixtures/Middleware/TestMiddleware.php new file mode 100644 index 0000000..f600052 --- /dev/null +++ b/tests/Fixtures/Middleware/TestMiddleware.php @@ -0,0 +1,17 @@ +events[] = $args; - } - - /** - * @return string[] - */ - public function getSubscribedEvents(): array - { - return [Events::postConnect]; - } - -}