Skip to content

Commit

Permalink
add exception if given connection has an unknown driver
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal committed Dec 28, 2022
1 parent 8726e2e commit ed41437
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 18 deletions.
19 changes: 9 additions & 10 deletions src/Command/DumpSqlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cake\Database\Driver\Sqlite;
use Cake\Datasource\ConnectionManager;
use Cake\Datasource\Exception\MissingDatasourceConfigException;
use CakeDumpSql\Error\UnknownDriverException;

/**
* DumpSQL command.
Expand Down Expand Up @@ -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
{
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/Error/UnknownDriverException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace CakeDumpSql\Error;

class UnknownDriverException extends \Exception
{
}
8 changes: 0 additions & 8 deletions src/Sql/SqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ public function setDataOnly(bool $dataOnly): void
$this->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
Expand Down
27 changes: 27 additions & 0 deletions tests/TestCase/Command/DumpSqlCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions tests/test_app/src/Driver/MyDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace TestApp\Driver;

use Cake\Database\Driver;
use Cake\Database\Driver\SqlDialectTrait;
use Cake\Database\Schema\MysqlSchemaDialect;
use Cake\Database\Schema\SchemaDialect;

class MyDriver extends Driver
{
use SqlDialectTrait;

public function connect(): bool
{
return true;
}

public function enabled(): bool
{
return true;
}

public function schemaDialect(): SchemaDialect
{
return new MysqlSchemaDialect($this);
}

public function disableForeignKeySQL(): string
{
return "";
}

public function enableForeignKeySQL(): string
{
return "";
}

public function supportsDynamicConstraints(): bool
{
return true;
}
}

0 comments on commit ed41437

Please sign in to comment.