From f14a1d8769634bc157e09d3df42040b38c5938ce Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Tue, 14 Feb 2017 13:47:33 +0200 Subject: [PATCH] Gozer: Refactoring and tests --- composer.json | 3 +- src/Commands/Gozer.php | 93 +++++++++++++++++++++++++++++++----------- tests/GozerTest.php | 46 +++++++++++++++------ 3 files changed, 105 insertions(+), 37 deletions(-) diff --git a/composer.json b/composer.json index 41c862f..227ad04 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ }, "require-dev": { "phpunit/phpunit" : "4.*", - "orchestra/testbench": "~3.0" + "orchestra/testbench": "~3.0", + "doctrine/dbal": "~2.3" }, "autoload": { "psr-4": { diff --git a/src/Commands/Gozer.php b/src/Commands/Gozer.php index 6105394..ae22003 100644 --- a/src/Commands/Gozer.php +++ b/src/Commands/Gozer.php @@ -4,6 +4,7 @@ namespace Superhelio\Commands\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Schema; class Gozer extends Command { @@ -21,6 +22,11 @@ class Gozer extends Command */ protected $description = 'Force delete database tables that has your table prefix'; + /** + * @var string Database table prefix + */ + private $dbPrefix = ''; + /** * Create a new command instance. */ @@ -56,34 +62,20 @@ class Gozer extends Command $tables = []; - $dbPrefix = trim(DB::getTablePrefix()); + $this->dbPrefix = $this->getDatabasePrefix(); $confirmationQuestion = 'Delete all of your database tables?'; - if (!empty($dbPrefix)) { + if (!empty($this->dbPrefix)) { $confirmationQuestion = sprintf( 'Delete your tables that begin with %s*', - $dbPrefix + $this->dbPrefix ); } if ($this->confirm($confirmationQuestion)) { - try { - /** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $connection */ - $connection = app('db')->connection()->getDoctrineSchemaManager(); - } catch (\Exception $e) { - $this->error($e->getMessage()); - return false; - } - - try { - /** @var array $tables */ - $tables = $connection->listTableNames(); - } catch (\Exception $e) { - $this->error($e->getMessage()); - - return false; - } + $connection = $this->getConnection(); + $tables = $this->getTables($connection); /** * Reject tables that do not have specified table prefix. @@ -92,9 +84,7 @@ class Gozer extends Command * * @var \Illuminate\Support\Collection $tables */ - $tables = collect($tables)->reject(function ($table) use ($dbPrefix) { - return !starts_with($table, $dbPrefix); - }); + $tables = $this->getFilteredTables($tables); /** * Check that we got at least one table, bail out if not @@ -122,7 +112,7 @@ class Gozer extends Command /** Fancy pants progress bar to see your tables get destroyed */ $bar = $this->output->createProgressBar($tables->count()); - \Illuminate\Support\Facades\Schema::disableForeignKeyConstraints(); + Schema::disableForeignKeyConstraints(); $tables->each(function ($table) use ($bar, $connection) { /** Drop the table */ @@ -132,7 +122,7 @@ class Gozer extends Command $bar->advance(); }); - \Illuminate\Support\Facades\Schema::enableForeignKeyConstraints(); + Schema::enableForeignKeyConstraints(); /** Progress bar is now finished */ $bar->finish(); @@ -147,4 +137,59 @@ class Gozer extends Command return true; } + + /** + * @return bool|\Doctrine\DBAL\Schema\AbstractSchemaManager + */ + private function getConnection() + { + try { + /** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $connection */ + $connection = app('db')->connection()->getDoctrineSchemaManager(); + } catch (\Exception $e) { + $this->error($e->getMessage()); + + return false; + } + + return $connection; + } + + /** + * @param \Doctrine\DBAL\Schema\AbstractSchemaManager $connection + * + * @return array|bool + */ + private function getTables(\Doctrine\DBAL\Schema\AbstractSchemaManager $connection) + { + try { + /** @var array $tables */ + $tables = $connection->listTableNames(); + } catch (\Exception $e) { + $this->error($e->getMessage()); + + return false; + } + + return $tables; + } + + private function getDatabasePrefix() + { + return trim(DB::getTablePrefix()); + } + + /** + * @param array $tables + * + * @return \Illuminate\Support\Collection + */ + private function getFilteredTables($tables = []) + { + $prefix = $this->dbPrefix; + + return collect($tables)->reject(function ($table) use ($prefix) { + return !starts_with($table, $prefix); + }); + } } diff --git a/tests/GozerTest.php b/tests/GozerTest.php index 53a325e..b2467d1 100644 --- a/tests/GozerTest.php +++ b/tests/GozerTest.php @@ -1,9 +1,12 @@ artisan('migrate', ['--database' => 'testbench']); } + /** * Define environment setup. * - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app * * @return void */ @@ -27,19 +31,20 @@ class GozerTest extends \Orchestra\Testbench\TestCase // Setup default database to use sqlite :memory: $app['config']->set('database.default', 'testbench'); $app['config']->set('database.connections.testbench', [ - 'driver' => 'sqlite', + 'driver' => 'sqlite', 'database' => ':memory:', - 'prefix' => '', + 'prefix' => 'gozerTest__', ]); } /** - * Get package providers. At a minimum this is the package being tested, but also - * would include packages upon which our package depends, e.g. Cartalyst/Sentry - * In a normal app environment these would be added to the 'providers' array in - * the config/app.php file. + * Get package providers. + * At a minimum this is the package being tested, but also + * would include packages upon which our package depends. + * In a normal app environment these would be added to + * the 'providers' array in the config/app.php file. * - * @param \Illuminate\Foundation\Application $app + * @param \Illuminate\Foundation\Application $app * * @return array */ @@ -51,17 +56,34 @@ class GozerTest extends \Orchestra\Testbench\TestCase ]; } - public function testGozerTest() + public function test_database_is_there_and_functions() { - \DB::table('users')->insert([ + DB::table('users')->insert([ 'name' => 'User name', 'email' => 'hello@gozer.dev', 'password' => bcrypt('123') ]); - $users = \DB::table('users')->where('id', '=', 1)->first(); + $users = DB::table('users')->where('id', '=', 1)->first(); $this->assertEquals('hello@gozer.dev', $users->email); $this->assertEquals('User name', $users->name); - $this->assertTrue(\Hash::check('123', $users->password)); + $this->assertTrue(Hash::check('123', $users->password)); + } + + public function test_dbal_is_installed() + { + $this->assertTrue(class_exists('\\Doctrine\\DBAL\\Schema\\Schema')); + } + + public function test_gozer_is_installed() + { + $this->assertTrue(class_exists('\\Superhelio\\Commands\\Commands\\Gozer')); + } + + public function test_gozer_has_required_methods_and_properties() + { + $gozer = new ReflectionClass('\\Superhelio\\Commands\\Commands\\Gozer'); + $this->assertTrue($gozer->hasMethod('handle')); + $this->assertTrue($gozer->hasProperty('dbPrefix')); } }