Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav Pozdnyakov committed Feb 25, 2019
1 parent ebd15c5 commit 0c2c47d
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 113 deletions.
18 changes: 18 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Vados\MigrationRunner;

/**
* Class Helpers
*/
class Helpers
{
/**
* @var bool $value
* @return string
*/
public static function boolToString(bool $value): string
{
return $value ? 'true' : 'false';
}
}
6 changes: 0 additions & 6 deletions src/Start.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 12:18
*/

namespace Vados\MigrationRunner;

Expand Down
8 changes: 1 addition & 7 deletions src/command/Create.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 12:58
*/

namespace Vados\MigrationRunner\command;

Expand Down Expand Up @@ -33,7 +27,7 @@ class Create implements ICommand
public function __construct(array $params)
{
$this->params = $params;
if (array_key_exists(0, $params)) {
if (isset($params[0])) {
$this->migrationName = $params[0];
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/command/Down.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 22.03.2018
* Time: 20:03
*/

namespace Vados\MigrationRunner\command;

use Vados\MigrationRunner\Helpers;
use Vados\MigrationRunner\migration\Migration;
use Vados\MigrationRunner\models\TblMigration;
use Vados\MigrationRunner\providers\PathProvider;
Expand Down Expand Up @@ -36,7 +32,7 @@ public function __construct(array $params)
{
parent::__construct();
$this->params = $params;
if (array_key_exists(0, $params)) {
if (isset($params[0])) {
$this->runCount = (int)$params[0];
}
}
Expand All @@ -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;
}
Expand Down
46 changes: 18 additions & 28 deletions src/command/GenerateConfigWizard.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 19.03.2018
* Time: 21:46
*/

namespace Vados\MigrationRunner\command;

Expand All @@ -16,38 +10,34 @@
*/
class GenerateConfigWizard implements ICommand
{

public function run()
{
$config = [];
echo 'Enter database adapter (default: sqlite): ';
$config['adapter'] = trim(fgets(STDIN));
if (!$config['adapter']) {
$config['adapter'] = 'sqlite';
}
if ($config['adapter'] !== 'sqlite') {
$this->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(), '<?php return ' . var_export($config, true) . ';');
echo 'Config generated in ' . PathProvider::getConfig() . PHP_EOL;
}

/**
* @var $field
* @var $default
*/
protected function getFromStdin(&$field, $default = null)
{
$field = trim(fgets(STDIN));
if (!$field) {
$field = $default;
}
}
}
6 changes: 0 additions & 6 deletions src/command/Help.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 12:27
*/

namespace Vados\MigrationRunner\command;

Expand Down
6 changes: 0 additions & 6 deletions src/command/ICommand.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 12:27
*/

namespace Vados\MigrationRunner\command;

Expand Down
9 changes: 3 additions & 6 deletions src/command/MigrationRun.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 14:45
*/

namespace Vados\MigrationRunner\command;

Expand Down Expand Up @@ -70,6 +64,9 @@ protected function getAppliedMigrations(): array
*/
protected function getExistingMigrations(): array
{
if (!is_dir(PathProvider::getMigrationDir())) {
return [];
}
$migrations = scandir(PathProvider::getMigrationDir());
$ignore = ['.', '..'];
return array_filter($migrations, function($item) use ($ignore) {
Expand Down
34 changes: 18 additions & 16 deletions src/command/Up.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 13:39
*/

namespace Vados\MigrationRunner\command;

use Vados\MigrationRunner\Helpers;
use Vados\MigrationRunner\migration\Migration;
use Vados\MigrationRunner\models\TblMigration;
use Vados\MigrationRunner\providers\PathProvider;
Expand Down Expand Up @@ -35,27 +31,21 @@ public function __construct(array $params)
{
parent::__construct();
$this->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;
}
Expand All @@ -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
Expand Down
6 changes: 0 additions & 6 deletions src/enum/Command.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 12:25
*/

namespace Vados\MigrationRunner\enum;

Expand Down
6 changes: 0 additions & 6 deletions src/enum/TableName.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 14:50
*/

namespace Vados\MigrationRunner\enum;

Expand Down
12 changes: 1 addition & 11 deletions src/migration/Migration.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 13:20
*/

namespace Vados\MigrationRunner\migration;

Expand Down Expand Up @@ -47,11 +41,7 @@ public function safeRun(string $action): bool
$result = $this->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;
Expand Down
6 changes: 0 additions & 6 deletions src/providers/PathProvider.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: vladyslavpozdnyakov
* Date: 17.03.2018
* Time: 14:19
*/

namespace Vados\MigrationRunner\providers;

Expand Down
27 changes: 27 additions & 0 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Vados\MigrationRunner\Tests;

use PHPUnit\Framework\TestCase;
use Vados\MigrationRunner\Helpers;

/**
* Class HelpersTest
* @package Vados\MigrationRunner\Tests
*/
class HelpersTest extends TestCase
{
public function testBoolToStringTrue()
{
$result = Helpers::boolToString(true);
$this->assertIsString($result);
$this->assertEquals('true', $result);
}

public function testBoolToStringFalse()
{
$result = Helpers::boolToString(false);
$this->assertIsString($result);
$this->assertEquals('false', $result);
}
}
8 changes: 8 additions & 0 deletions tests/command/UpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
15 changes: 15 additions & 0 deletions tests/proxy/Up.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Vados\MigrationRunner\Tests\proxy;

/**
* Class UpTest
* @package Vados\MigrationRunner\Tests\proxy
*/
class Up extends \Vados\MigrationRunner\command\Up
{
public function runGetMigrationList(): array
{
return $this->getMigrationsList();
}
}

0 comments on commit 0c2c47d

Please sign in to comment.