From 8ca5e56d2d3b202f888fe5033ed277fbbccb2722 Mon Sep 17 00:00:00 2001 From: Vladyslav Riabchenko Date: Fri, 28 Jun 2019 16:42:19 +0200 Subject: [PATCH] params retrievment --- .travis.yml | 1 - src/Command/AnonymizeCommand.php | 13 ++++++++- src/Command/AnonymizeCommandTrait.php | 29 +++++++++++-------- .../System/Command/AnonymizeCommandTest.php | 2 +- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index 49df6ca..41832c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,6 @@ script: after_success: - bash <(curl -s https://codecov.io/bash) - jobs: include: diff --git a/src/Command/AnonymizeCommand.php b/src/Command/AnonymizeCommand.php index 250a639..d3157a6 100644 --- a/src/Command/AnonymizeCommand.php +++ b/src/Command/AnonymizeCommand.php @@ -2,6 +2,7 @@ namespace WebnetFr\DatabaseAnonymizer\Command; +use Doctrine\DBAL\DBALException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -68,7 +69,17 @@ protected function execute(InputInterface $input, OutputInterface $output) return; } - $connection = $this->getConnectionFromInput($input); + try { + $connection = $this->getConnectionFromInput($input); + } catch (DBALException $e) { + $connection = null; + } + + if (!$connection) { + $output->writeln(sprintf('Unable to establish a connection.')); + + return; + } $configFile = $input->getArgument('config'); $configFilePath = realpath($input->getArgument('config')); diff --git a/src/Command/AnonymizeCommandTrait.php b/src/Command/AnonymizeCommandTrait.php index 8a60675..621abb7 100644 --- a/src/Command/AnonymizeCommandTrait.php +++ b/src/Command/AnonymizeCommandTrait.php @@ -19,28 +19,33 @@ trait AnonymizeCommandTrait { /** - * @param array $params + * @param InputInterface $input * * @throws \Doctrine\DBAL\DBALException * - * @return Connection + * @return Connection|null */ protected function getConnectionFromInput(InputInterface $input) { if ($dbURL = $input->getOption('url')) { - $params = ['url' => $dbURL]; - } else { - $params = [ - 'driver' => $input->getOption('type'), - 'host' => $input->getOption('host'), - 'port' => $input->getOption('port'), - 'dbname' => $input->getOption('database'), - 'user' => $input->getOption('user'), + return $this->getConnection(['url' => $dbURL]); + } elseif (($type = $input->getOption('type')) + && ($host = $input->getOption('host')) + && ($port = $input->getOption('port')) + && ($database = $input->getOption('database')) + && ($user = $input->getOption('user')) + ) { + return $this->getConnection([ + 'driver' => $type, + 'host' => $host, + 'port' => $port, + 'dbname' => $database, + 'user' => $user, 'password' => $input->getOption('password'), - ]; + ]); } - return $this->getConnection($params); + return null; } /** diff --git a/tests/DatabaseAnonymizer/Tests/System/Command/AnonymizeCommandTest.php b/tests/DatabaseAnonymizer/Tests/System/Command/AnonymizeCommandTest.php index b21098d..c4f1759 100644 --- a/tests/DatabaseAnonymizer/Tests/System/Command/AnonymizeCommandTest.php +++ b/tests/DatabaseAnonymizer/Tests/System/Command/AnonymizeCommandTest.php @@ -64,7 +64,7 @@ public function testExecute() $this->assertTrue(is_string($row['firstname'])); $this->assertTrue(is_string($row['lastname'])); $this->assertTrue(is_string($row['birthdate'])); - $this->assertTrue(is_string($row['phone'])); + $this->assertTrue(is_string($row['phone']) || is_null($row['phone'])); $this->assertTrue(is_string($row['password'])); }