From ed4143764ac4393cbd2af6432d4988e774ad6e70 Mon Sep 17 00:00:00 2001 From: Kevin Pfeifer Date: Wed, 28 Dec 2022 21:29:25 +0100 Subject: [PATCH] add exception if given connection has an unknown driver --- src/Command/DumpSqlCommand.php | 19 ++++---- src/Error/UnknownDriverException.php | 8 ++++ src/Sql/SqlBase.php | 8 ---- tests/TestCase/Command/DumpSqlCommandTest.php | 27 ++++++++++++ tests/test_app/src/Driver/MyDriver.php | 44 +++++++++++++++++++ 5 files changed, 88 insertions(+), 18 deletions(-) create mode 100644 src/Error/UnknownDriverException.php create mode 100644 tests/test_app/src/Driver/MyDriver.php diff --git a/src/Command/DumpSqlCommand.php b/src/Command/DumpSqlCommand.php index dc2a1c8..70643fe 100644 --- a/src/Command/DumpSqlCommand.php +++ b/src/Command/DumpSqlCommand.php @@ -12,6 +12,7 @@ use Cake\Database\Driver\Sqlite; use Cake\Datasource\ConnectionManager; use Cake\Datasource\Exception\MissingDatasourceConfigException; +use CakeDumpSql\Error\UnknownDriverException; /** * DumpSQL command. @@ -46,6 +47,7 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar * @param \Cake\Console\Arguments $args The command arguments. * @param \Cake\Console\ConsoleIo $io The console io * @return int The exit code + * @throws \CakeDumpSql\Error\UnknownDriverException */ public function execute(Arguments $args, ConsoleIo $io): int { @@ -61,29 +63,26 @@ public function execute(Arguments $args, ConsoleIo $io): int return self::CODE_ERROR; } - $result = ''; $driver = $connection->getDriver(); switch (get_class($driver)) { case Mysql::class: $object = new \CakeDumpSql\Sql\MySQL($connection->config()); - $object->setIo($io); - $object->setDataOnly($dataOnly); - $result = $object->dump(); break; case Sqlite::class: $object = new \CakeDumpSql\Sql\Sqlite($connection->config()); - $object->setIo($io); - $object->setDataOnly($dataOnly); - $result = $object->dump(); break; case Postgres::class: $object = new \CakeDumpSql\Sql\PostgreSQL($connection->config()); - $object->setIo($io); - $object->setDataOnly($dataOnly); - $result = $object->dump(); break; + default: + $message = sprintf('Unknown driver "%s" given.', get_class($driver)); + throw new UnknownDriverException($message); } + $object->setIo($io); + $object->setDataOnly($dataOnly); + $result = $object->dump(); + if ($gzip) { if (function_exists('gzencode')) { $result = gzencode($result, 9); diff --git a/src/Error/UnknownDriverException.php b/src/Error/UnknownDriverException.php new file mode 100644 index 0000000..530ad37 --- /dev/null +++ b/src/Error/UnknownDriverException.php @@ -0,0 +1,8 @@ +dataOnly = $dataOnly; } - /** - * @return \Cake\Console\ConsoleIo - */ - public function getIo(): ConsoleIo - { - return $this->io; - } - /** * @param \Cake\Console\ConsoleIo $io The IO instance from the command * @return void diff --git a/tests/TestCase/Command/DumpSqlCommandTest.php b/tests/TestCase/Command/DumpSqlCommandTest.php index 7a7623b..8e6bdc5 100644 --- a/tests/TestCase/Command/DumpSqlCommandTest.php +++ b/tests/TestCase/Command/DumpSqlCommandTest.php @@ -4,12 +4,15 @@ namespace CakeDumpSql\Test\TestCase\Command; use Cake\Console\TestSuite\ConsoleIntegrationTestTrait; +use Cake\Database\Connection; use Cake\Database\Driver\Mysql; use Cake\Database\Driver\Postgres; use Cake\Database\Driver\Sqlite; use Cake\Datasource\ConnectionManager; use Cake\I18n\FrozenTime; use Cake\TestSuite\TestCase; +use CakeDumpSql\Error\UnknownDriverException; +use TestApp\Driver\MyDriver; class DumpSqlCommandTest extends TestCase { @@ -99,6 +102,30 @@ public function testCommandGzipped(): void $this->assertExitCode(0); } + public function testUnknownConnectionName(): void + { + $this->exec('dump_sql unknown'); + $this->assertErrorContains('The datasource configuration "unknown" was not found.'); + $this->assertExitCode(1); + } + + public function testUnknownDriver(): void + { + $this->expectException(UnknownDriverException::class); + $this->expectExceptionMessage('Unknown driver "TestApp\Driver\MyDriver" given.'); + ConnectionManager::setConfig('unknown', [ + 'className' => Connection::class, + 'driver' => MyDriver::class, + 'persistent' => false, + 'timezone' => 'UTC', + 'flags' => [], + 'cacheMetadata' => false, + 'log' => false, + 'quoteIdentifiers' => false, + ]); + $this->exec('dump_sql unknown'); + } + private function isDBType(string $type, string $connection = 'default'): bool { $connection = ConnectionManager::get($connection); diff --git a/tests/test_app/src/Driver/MyDriver.php b/tests/test_app/src/Driver/MyDriver.php new file mode 100644 index 0000000..c66b210 --- /dev/null +++ b/tests/test_app/src/Driver/MyDriver.php @@ -0,0 +1,44 @@ +