From 0c2c47db0c40f6ea87f89ad13be8d8526e7d89a6 Mon Sep 17 00:00:00 2001 From: Vladyslav Pozdnyakov Date: Mon, 25 Feb 2019 20:05:35 +0200 Subject: [PATCH] Refactoring --- src/Helpers.php | 18 +++++++++++ src/Start.php | 6 ---- src/command/Create.php | 8 +---- src/command/Down.php | 13 +++----- src/command/GenerateConfigWizard.php | 46 +++++++++++----------------- src/command/Help.php | 6 ---- src/command/ICommand.php | 6 ---- src/command/MigrationRun.php | 9 ++---- src/command/Up.php | 34 ++++++++++---------- src/enum/Command.php | 6 ---- src/enum/TableName.php | 6 ---- src/migration/Migration.php | 12 +------- src/providers/PathProvider.php | 6 ---- tests/HelpersTest.php | 27 ++++++++++++++++ tests/command/UpTest.php | 8 +++++ tests/proxy/Up.php | 15 +++++++++ 16 files changed, 113 insertions(+), 113 deletions(-) create mode 100644 src/Helpers.php create mode 100644 tests/HelpersTest.php create mode 100644 tests/proxy/Up.php diff --git a/src/Helpers.php b/src/Helpers.php new file mode 100644 index 0000000..c6e68f0 --- /dev/null +++ b/src/Helpers.php @@ -0,0 +1,18 @@ +params = $params; - if (array_key_exists(0, $params)) { + if (isset($params[0])) { $this->migrationName = $params[0]; } } diff --git a/src/command/Down.php b/src/command/Down.php index 02f57ae..9fb4192 100644 --- a/src/command/Down.php +++ b/src/command/Down.php @@ -1,12 +1,8 @@ params = $params; - if (array_key_exists(0, $params)) { + if (isset($params[0])) { $this->runCount = (int)$params[0]; } } @@ -58,8 +54,7 @@ public function run() /** @var TblMigration $migration */ echo "Migration {$migration->getMigration()}: "; $result = $this->down($migration); - echo $result ? 'true' : 'false'; - echo PHP_EOL; + echo Helpers::boolToString($result) . PHP_EOL; if (!$result) { break; } diff --git a/src/command/GenerateConfigWizard.php b/src/command/GenerateConfigWizard.php index 8aa8b72..2b0e9b6 100644 --- a/src/command/GenerateConfigWizard.php +++ b/src/command/GenerateConfigWizard.php @@ -1,10 +1,4 @@ getFromStdin($config['adapter'], 'sqlite'); + if ('sqlite' !== $config['adapter']) { echo 'Enter database host (default: localhost): '; - $config['host'] = trim(fgets(STDIN)); - if (!$config['host']) { - $config['host'] = 'localhost'; - } + $this->getFromStdin($config['host'], 'localhost'); echo 'Enter username (default: root): '; - $config['username'] = trim(fgets(STDIN)); - if (!$config['username']) { - $config['username'] = 'root'; - } + $this->getFromStdin($config['username'], 'root'); echo 'Enter password (default: root): '; - $config['password'] = trim(fgets(STDIN)); - if (!$config['password']) { - $config['password'] = 'root'; - } + $this->getFromStdin($config['password'], 'root'); } echo 'Enter database name (default: phalcon): '; - $config['dbname'] = trim(fgets(STDIN)); - if (!$config['dbname']) { - $config['dbname'] = 'phalcon'; - } + $this->getFromStdin($config['dbname'], 'phalcon'); file_put_contents(PathProvider::getConfig(), 'params = $params; - if (array_key_exists(0, $params)) { + if (isset($params[0])) { $this->runCount = (int)$params[0]; } } public function run() { - $migrations = $this->getNewMigrations(); - if ($this->runCount !== 0) { - $migrations = array_slice($migrations, 0, $this->runCount); - } + $migrations = $this->getMigrationsList(); if (!empty($migrations)) { - foreach ($migrations as $migration) { - echo $migration . PHP_EOL; - } + echo implode(PHP_EOL, $migrations); if ($this->actionConfirmation('Apply the above migrations?')) { foreach ($migrations as $migration) { echo "Migration $migration: "; $result = $this->up($migration); - echo $result ? 'true' : 'false'; - echo PHP_EOL; + echo Helpers::boolToString($result) . PHP_EOL; if (!$result) { break; } @@ -64,6 +54,18 @@ public function run() } } + /** + * @return array + */ + protected function getMigrationsList(): array + { + $migrations = $this->getNewMigrations(); + if (0 !== $this->runCount) { + $migrations = array_slice($migrations, 0, $this->runCount); + } + return $migrations; + } + /** * @param string $migration * @return bool diff --git a/src/enum/Command.php b/src/enum/Command.php index f5cacb8..e8715d5 100644 --- a/src/enum/Command.php +++ b/src/enum/Command.php @@ -1,10 +1,4 @@ down(); break; } - if ($result) { - $this->dbInstance->commit(); - } else { - $this->dbInstance->rollback(); - } + $result ? $this->dbInstance->commit() : $this->dbInstance->rollback(); return $result; } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; diff --git a/src/providers/PathProvider.php b/src/providers/PathProvider.php index 3d2cf57..b5762e8 100644 --- a/src/providers/PathProvider.php +++ b/src/providers/PathProvider.php @@ -1,10 +1,4 @@ assertIsString($result); + $this->assertEquals('true', $result); + } + + public function testBoolToStringFalse() + { + $result = Helpers::boolToString(false); + $this->assertIsString($result); + $this->assertEquals('false', $result); + } +} \ No newline at end of file diff --git a/tests/command/UpTest.php b/tests/command/UpTest.php index 31aa932..1a673c8 100644 --- a/tests/command/UpTest.php +++ b/tests/command/UpTest.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\TestCase; use Vados\MigrationRunner\command\ICommand; use Vados\MigrationRunner\command\Up; +use Vados\MigrationRunner\Tests\proxy\Up as UpProxy; /** * Class UpTest @@ -46,4 +47,11 @@ public function testUpMigrationDoesntExist() $upMethod->setAccessible(true); $this->assertFalse($upMethod->invokeArgs($instance, ['not_exist'])); } + + public function testGetMigrationList() + { + $instance = new UpProxy([]); + $list = $instance->runGetMigrationList(); + $this->assertIsArray($list); + } } \ No newline at end of file diff --git a/tests/proxy/Up.php b/tests/proxy/Up.php new file mode 100644 index 0000000..83b4538 --- /dev/null +++ b/tests/proxy/Up.php @@ -0,0 +1,15 @@ +getMigrationsList(); + } +} \ No newline at end of file