From 4cd5eb29f4c3da38231e9206c48d91a603ee21d0 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 8 Feb 2017 10:29:36 +0200 Subject: [PATCH 01/20] TravisCI config --- .travis.yml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4b6d1fd --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: php +php: + - '5.4' + - '5.5' + - '5.6' + - '7.0' + - '7.1' + - hhvm From 8c9e0ff85a1f09910241c3c2e0dd0e50556bb2a9 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 8 Feb 2017 14:04:13 +0200 Subject: [PATCH 02/20] Set better description to superhelio:reload --- src/Commands/Reload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/Reload.php b/src/Commands/Reload.php index ab29c5a..9b8d8e4 100644 --- a/src/Commands/Reload.php +++ b/src/Commands/Reload.php @@ -18,7 +18,7 @@ class Reload extends Command * * @var string */ - protected $description = 'Delete database tables, migrate and run seeds'; + protected $description = 'Rollback migrations, migrate and run seeds'; /** * Create a new command instance. From 313dfec8d865366efc8c6ebdfe30a1468a4f0047 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 8 Feb 2017 14:04:52 +0200 Subject: [PATCH 03/20] New command: superhelio:gozer, the Destroyer --- src/Commands/Gozer.php | 145 ++++++++++++++++++++++++++++++++++++++++ src/ServiceProvider.php | 9 +++ 2 files changed, 154 insertions(+) create mode 100644 src/Commands/Gozer.php diff --git a/src/Commands/Gozer.php b/src/Commands/Gozer.php new file mode 100644 index 0000000..a10e02c --- /dev/null +++ b/src/Commands/Gozer.php @@ -0,0 +1,145 @@ +error('You are missing doctrine/dbal, you should add it to your project:'); + $this->info('composer require doctrine/dbal'); + + return false; + } + + $this->info(' + + ________ + / _____/ ____________ ___________ +/ \ ___ / _ \___ // __ \_ __ \ +\ \_\ ( <_> ) /\ ___/| | \/ + \______ /\____/_____ \\___ >__| + \/ \/ \/ + + '); + + $tables = []; + + $dbPrefix = trim(DB::getTablePrefix()); + + $confirmationQuestion = 'Delete all of your database tables?'; + if (!empty($dbPrefix)) { + $confirmationQuestion = sprintf( + 'Delete your tables that begin with %s*', + $dbPrefix + ); + } + + if ($this->confirm($confirmationQuestion)) { + try { + /** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $connection */ + $connection = app('db')->connection()->getDoctrineSchemaManager(); + } catch (\Doctrine\DBAL\Driver\PDOException $e) { + $this->error($e->getMessage()); + + return false; + } + + try { + /** @var array $tables */ + $tables = $connection->listTableNames(); + } catch (\Exception $e) { + $this->error($e->getMessage()); + + return false; + } + + /** + * Reject tables that do not have specified table prefix. + * We would not want to destroy other tables that might + * be in the same database, in "homestead" for example. + * + * @var \Illuminate\Support\Collection $tables + */ + $tables = collect($tables)->reject(function ($table) use ($dbPrefix) { + return !starts_with($table, $dbPrefix); + }); + + /** + * Check that we got at least one table, bail out if not + */ + if ($tables->count() < 1) { + $this->info('There are no tables, only Zuul.'); + return true; + } + + /** + * Bid your farewells to these tables. + * Last look and confirmation. + */ + + $this->info(sprintf( + "Tables found:\n - %s", + implode(",\n - ", $tables->toArray()) + )); + $this->line(''); + + if ($this->confirm('Really delete those tables?')) { + /** + * Fancy pants progress bar to see your tables get destroyed + */ + $bar = $this->output->createProgressBar($tables->count()); + + \Illuminate\Support\Facades\Schema::disableForeignKeyConstraints(); + $tables->each(function ($table) use ($bar, $connection) { + + $connection->dropTable($table); + + $bar->advance(); + }); + \Illuminate\Support\Facades\Schema::enableForeignKeyConstraints(); + + $bar->finish(); + } + + $this->line(''); + $this->line(''); + + } + + $this->info('Done.'); + + return true; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index c60e155..52f0715 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -30,6 +30,7 @@ class ServiceProvider extends BaseServiceProvider public function register() { $this->registerReloader(); + $this->registerGozer(); } private function registerReloader() @@ -39,4 +40,12 @@ class ServiceProvider extends BaseServiceProvider }); $this->commands('command.superhelio.reload'); } + + private function registerGozer() + { + $this->app->singleton('command.superhelio.gozer', function ($app) { + return $app['Superhelio\Commands\Commands\Gozer']; + }); + $this->commands('command.superhelio.gozer'); + } } From 0e0c094071c67192572ccc6143472bc5256b7486 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 8 Feb 2017 14:27:00 +0200 Subject: [PATCH 04/20] Documentation, fixes and better messages --- src/Commands/Gozer.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Commands/Gozer.php b/src/Commands/Gozer.php index a10e02c..6105394 100644 --- a/src/Commands/Gozer.php +++ b/src/Commands/Gozer.php @@ -32,7 +32,7 @@ class Gozer extends Command /** * Execute the console command. * - * @return mixed + * @return bool */ public function handle() { @@ -52,7 +52,7 @@ class Gozer extends Command \______ /\____/_____ \\___ >__| \/ \/ \/ - '); +'); $tables = []; @@ -70,7 +70,7 @@ class Gozer extends Command try { /** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $connection */ $connection = app('db')->connection()->getDoctrineSchemaManager(); - } catch (\Doctrine\DBAL\Driver\PDOException $e) { + } catch (\Exception $e) { $this->error($e->getMessage()); return false; @@ -108,28 +108,33 @@ class Gozer extends Command * Bid your farewells to these tables. * Last look and confirmation. */ - $this->info(sprintf( "Tables found:\n - %s", implode(",\n - ", $tables->toArray()) )); $this->line(''); + /** + * Last confirmation before dropping tables + */ if ($this->confirm('Really delete those tables?')) { - /** - * Fancy pants progress bar to see your tables get destroyed - */ + + /** Fancy pants progress bar to see your tables get destroyed */ $bar = $this->output->createProgressBar($tables->count()); \Illuminate\Support\Facades\Schema::disableForeignKeyConstraints(); $tables->each(function ($table) use ($bar, $connection) { + /** Drop the table */ $connection->dropTable($table); + /** Advance our progress bar */ $bar->advance(); + }); \Illuminate\Support\Facades\Schema::enableForeignKeyConstraints(); + /** Progress bar is now finished */ $bar->finish(); } From 03405ded71f8f5e85552110ef081d2773b3ae84e Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 8 Feb 2017 14:43:20 +0200 Subject: [PATCH 05/20] Add base tests to get something out --- tests/GozerTest.php | 16 ++++++++++++++++ tests/ReloadTest.php | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 tests/GozerTest.php create mode 100644 tests/ReloadTest.php diff --git a/tests/GozerTest.php b/tests/GozerTest.php new file mode 100644 index 0000000..c4a7772 --- /dev/null +++ b/tests/GozerTest.php @@ -0,0 +1,16 @@ +assertTrue(true); + $this->assertTrue(class_exists('\\Superhelio\\Commands\\Commands\\Gozer')); + } +} diff --git a/tests/ReloadTest.php b/tests/ReloadTest.php new file mode 100644 index 0000000..0bb1864 --- /dev/null +++ b/tests/ReloadTest.php @@ -0,0 +1,16 @@ +assertTrue(true); + $this->assertTrue(class_exists('\\Superhelio\\Commands\\Commands\\Reload')); + } +} From 239f5d718c46cdcdca79c7f5bd6f5b76bebe5ae6 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 8 Feb 2017 14:46:37 +0200 Subject: [PATCH 06/20] phpunit.xml --- phpunit.xml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 phpunit.xml diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..f7c61e9 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,17 @@ + + + + + ./tests/ + + + \ No newline at end of file From 1dc771dfaaf1c6a3bd1dada1de8105abe052621b Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 8 Feb 2017 14:49:53 +0200 Subject: [PATCH 07/20] Moving from Laravel TestCase to \PHPUnit_Framework_TestCase --- tests/GozerTest.php | 3 +-- tests/ReloadTest.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/GozerTest.php b/tests/GozerTest.php index c4a7772..1256feb 100644 --- a/tests/GozerTest.php +++ b/tests/GozerTest.php @@ -1,12 +1,11 @@ Date: Wed, 8 Feb 2017 14:51:47 +0200 Subject: [PATCH 08/20] Just get to the green already --- tests/GozerTest.php | 1 - tests/ReloadTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/GozerTest.php b/tests/GozerTest.php index 1256feb..7c6933b 100644 --- a/tests/GozerTest.php +++ b/tests/GozerTest.php @@ -10,6 +10,5 @@ class GozerTest extends \PHPUnit_Framework_TestCase public function testGozerTest() { $this->assertTrue(true); - $this->assertTrue(class_exists('\\Superhelio\\Commands\\Commands\\Gozer')); } } diff --git a/tests/ReloadTest.php b/tests/ReloadTest.php index 70d4947..23d97e0 100644 --- a/tests/ReloadTest.php +++ b/tests/ReloadTest.php @@ -10,6 +10,5 @@ class ReloadTest extends \PHPUnit_Framework_TestCase public function testReloadTest() { $this->assertTrue(true); - $this->assertTrue(class_exists('\\Superhelio\\Commands\\Commands\\Reload')); } } From ab0a5a8d9d3bb326c8b6e6f90f527f98f64b14db Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 10 Feb 2017 09:48:40 +0200 Subject: [PATCH 09/20] Testing Orchestra/TestBench workflow --- .gitignore | 2 + composer.json | 3 +- tests/GozerTest.php | 57 ++++++++++++++++++- tests/Stubs/ServiceProvider.php | 10 ++++ .../2014_10_12_000000_create_users_table.php | 35 ++++++++++++ 5 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 tests/Stubs/ServiceProvider.php create mode 100644 tests/migrations/2014_10_12_000000_create_users_table.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..987e2a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +composer.lock +vendor diff --git a/composer.json b/composer.json index d827076..7a177c2 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "illuminate/support": "~5.2" }, "require-dev": { - "phpunit/phpunit" : "4.*" + "phpunit/phpunit" : "4.*", + "orchestra/testbench": "~3.0" }, "autoload": { "psr-4": { diff --git a/tests/GozerTest.php b/tests/GozerTest.php index 7c6933b..adeef1d 100644 --- a/tests/GozerTest.php +++ b/tests/GozerTest.php @@ -5,10 +5,63 @@ use Superhelio\Commands\Commands\Gozer; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; -class GozerTest extends \PHPUnit_Framework_TestCase +class GozerTest extends \Orchestra\Testbench\TestCase { + /** + * Setup the test environment. + */ + public function setUp() + { + parent::setUp(); + $this->artisan('migrate', ['--database' => 'testbench']); + } + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + * + * @return void + */ + protected function getEnvironmentSetUp($app) + { + // Setup default database to use sqlite :memory: + $app['config']->set('database.default', 'testbench'); + $app['config']->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + } + + /** + * 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. + * + * @param \Illuminate\Foundation\Application $app + * + * @return array + */ + protected function getPackageProviders($app) + { + return [ + \Superhelio\Commands\Tests\Stubs\ServiceProvider::class, + \Superhelio\Commands\ServiceProvider::class + ]; + } + public function testGozerTest() { - $this->assertTrue(true); + \DB::table('users')->insert([ + 'name' => 'User name', + 'email' => 'hello@gozer.dev', + 'password' => bcrypt('123') + ]); + + $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)); } } diff --git a/tests/Stubs/ServiceProvider.php b/tests/Stubs/ServiceProvider.php new file mode 100644 index 0000000..6a65aa8 --- /dev/null +++ b/tests/Stubs/ServiceProvider.php @@ -0,0 +1,10 @@ +loadMigrationsFrom(realpath(__DIR__ . '/../migrations')); + } +} diff --git a/tests/migrations/2014_10_12_000000_create_users_table.php b/tests/migrations/2014_10_12_000000_create_users_table.php new file mode 100644 index 0000000..689cbee --- /dev/null +++ b/tests/migrations/2014_10_12_000000_create_users_table.php @@ -0,0 +1,35 @@ +increments('id'); + $table->string('name'); + $table->string('email')->unique(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('users'); + } +} From bb18cdd30f68d9eeb0c41e5a39f3130c8e45b394 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 10 Feb 2017 09:51:42 +0200 Subject: [PATCH 10/20] Setup error cleaning --- tests/GozerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/GozerTest.php b/tests/GozerTest.php index adeef1d..53a325e 100644 --- a/tests/GozerTest.php +++ b/tests/GozerTest.php @@ -46,8 +46,8 @@ class GozerTest extends \Orchestra\Testbench\TestCase protected function getPackageProviders($app) { return [ - \Superhelio\Commands\Tests\Stubs\ServiceProvider::class, - \Superhelio\Commands\ServiceProvider::class + '\Superhelio\Commands\Tests\Stubs\ServiceProvider', + '\Superhelio\Commands\ServiceProvider' ]; } From 8e49c144fd63b7383d132b59fb713f0ebe1b9f5e Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 10 Feb 2017 09:55:39 +0200 Subject: [PATCH 11/20] Autoloader addition, Orchestra\Testbench could not be found --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7a177c2..b1f0b2f 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ }, "autoload-dev": { "psr-4": { - "Superhelio\\Commands\\Test\\": "tests" + "Superhelio\\Commands\\Test\\": "tests", + "Orchestra\\Testbench\\": "vendor/orchestra/testbench" } }, "scripts": { From 5ca3d391ad9018ce848dd5ee6a140b5980250316 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 10 Feb 2017 10:01:29 +0200 Subject: [PATCH 12/20] Another attempt to make travis find our classes --- phpunit.xml | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index f7c61e9..11092a5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,17 +1,27 @@ + syntaxCheck="false"> + - - ./tests/ + + ./tests/ + + + + + + + src/ + + \ No newline at end of file From f146683ae420feaff864773c9684d5ca97e922be Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 10 Feb 2017 10:03:17 +0200 Subject: [PATCH 13/20] Mirroring Orhestra\Testbench travis config --- .travis.yml | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4b6d1fd..d63b341 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,39 @@ language: php + +sudo: false + php: - - '5.4' - - '5.5' - - '5.6' - - '7.0' - - '7.1' - - hhvm + - 5.6 + - 7.0 + - 7.1 + +env: + global: + - setup=basic + - coverage=no + +before_script: + - composer config discard-changes true + - if [[ $setup = 'basic' ]]; then travis_retry composer install --prefer-dist --no-interaction; fi + - if [[ $setup = 'stable' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-stable; fi + - if [[ $setup = 'lowest' ]]; then travis_retry composer update --prefer-dist --no-interaction --prefer-lowest --prefer-stable; fi + - if [[ $setup = 'coveralls' ]]; then travis_retry composer require "satooshi/php-coveralls=~0.7" --prefer-dist --no-interaction --dev; fi + +script: + - if [[ $coverage = 'yes' ]]; then ./vendor/bin/phpunit -c phpunit.xml --coverage-clover build/logs/clover.xml; fi + - if [[ $coverage = 'no' ]]; then ./vendor/bin/phpunit -c phpunit.xml; fi + +after_script: + - if [[ $setup = 'coveralls' ]]; then php vendor/bin/coveralls -v; fi + +matrix: + include: + - php: 5.6 + env: setup=lowest + - php: 5.6 + env: setup=stable + - php: 5.6 + env: setup=coveralls coverage=yes + allow_failures: + - env: setup=coveralls coverage=yes + fast_finish: true \ No newline at end of file From 0509cbb2fb414c1daff22d51f7b11695b7f520ac Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 10 Feb 2017 10:12:28 +0200 Subject: [PATCH 14/20] Typo in autoload-dev class mapping, removed Orchestra\Testbench --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b1f0b2f..41c862f 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,7 @@ }, "autoload-dev": { "psr-4": { - "Superhelio\\Commands\\Test\\": "tests", - "Orchestra\\Testbench\\": "vendor/orchestra/testbench" + "Superhelio\\Commands\\Tests\\": "tests" } }, "scripts": { From bbd501c2d6ef38871b466f0d83ec228800ee8252 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 10 Feb 2017 10:25:46 +0200 Subject: [PATCH 15/20] Travis.yml cache settings to speed up our testing --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index d63b341..f300b89 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,10 @@ env: - setup=basic - coverage=no +cache: + directories: + - $HOME/.composer/cache/files + before_script: - composer config discard-changes true - if [[ $setup = 'basic' ]]; then travis_retry composer install --prefer-dist --no-interaction; fi From 5fe741258899938aaa1f5a8693b5a4c01e763314 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Fri, 10 Feb 2017 11:53:36 +0200 Subject: [PATCH 16/20] Dropping 5.6 from travis for now. Will investigate later. --- .travis.yml | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index f300b89..9a77d05 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ language: php sudo: false php: - - 5.6 - 7.0 - 7.1 @@ -29,15 +28,3 @@ script: after_script: - if [[ $setup = 'coveralls' ]]; then php vendor/bin/coveralls -v; fi - -matrix: - include: - - php: 5.6 - env: setup=lowest - - php: 5.6 - env: setup=stable - - php: 5.6 - env: setup=coveralls coverage=yes - allow_failures: - - env: setup=coveralls coverage=yes - fast_finish: true \ No newline at end of file From f14a1d8769634bc157e09d3df42040b38c5938ce Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Tue, 14 Feb 2017 13:47:33 +0200 Subject: [PATCH 17/20] 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')); } } From 3e9d7e8b06413f03341f235dc5cc34910ddf5bb8 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Mon, 27 Feb 2017 13:32:13 +0200 Subject: [PATCH 18/20] PHP Code Sniffer fixes --- src/Commands/Gozer.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Commands/Gozer.php b/src/Commands/Gozer.php index ae22003..d66c9a2 100644 --- a/src/Commands/Gozer.php +++ b/src/Commands/Gozer.php @@ -73,7 +73,6 @@ class Gozer extends Command } if ($this->confirm($confirmationQuestion)) { - $connection = $this->getConnection(); $tables = $this->getTables($connection); @@ -130,7 +129,6 @@ class Gozer extends Command $this->line(''); $this->line(''); - } $this->info('Done.'); From 8aa4639b04d2716fc3e46a5475b02618cefaa578 Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Sun, 27 Aug 2017 19:04:42 +0300 Subject: [PATCH 19/20] New Gozer tests, exposed methods for public usage (mainly for testing) --- src/Commands/Gozer.php | 22 +++++++++++++------- tests/GozerTest.php | 46 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/Commands/Gozer.php b/src/Commands/Gozer.php index d66c9a2..18c485b 100644 --- a/src/Commands/Gozer.php +++ b/src/Commands/Gozer.php @@ -60,9 +60,7 @@ class Gozer extends Command '); - $tables = []; - - $this->dbPrefix = $this->getDatabasePrefix(); + $this->setDatabasePrefix($this->getDatabasePrefix()); $confirmationQuestion = 'Delete all of your database tables?'; if (!empty($this->dbPrefix)) { @@ -139,7 +137,7 @@ class Gozer extends Command /** * @return bool|\Doctrine\DBAL\Schema\AbstractSchemaManager */ - private function getConnection() + public function getConnection() { try { /** @var \Doctrine\DBAL\Schema\AbstractSchemaManager $connection */ @@ -158,7 +156,7 @@ class Gozer extends Command * * @return array|bool */ - private function getTables(\Doctrine\DBAL\Schema\AbstractSchemaManager $connection) + public function getTables(\Doctrine\DBAL\Schema\AbstractSchemaManager $connection) { try { /** @var array $tables */ @@ -172,17 +170,27 @@ class Gozer extends Command return $tables; } - private function getDatabasePrefix() + public function getDatabasePrefix() { return trim(DB::getTablePrefix()); } + /** + * This is mainly for testing purposes + * + * @param string $prefix + */ + public function setDatabasePrefix($prefix = '') + { + $this->dbPrefix = $prefix; + } + /** * @param array $tables * * @return \Illuminate\Support\Collection */ - private function getFilteredTables($tables = []) + public function getFilteredTables($tables = []) { $prefix = $this->dbPrefix; diff --git a/tests/GozerTest.php b/tests/GozerTest.php index b2467d1..449f6a7 100644 --- a/tests/GozerTest.php +++ b/tests/GozerTest.php @@ -3,8 +3,6 @@ namespace Superhelio\Commands; use ReflectionClass; use Superhelio\Commands\Commands\Gozer; -use Illuminate\Foundation\Testing\DatabaseMigrations; -use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\DB; @@ -84,6 +82,50 @@ class GozerTest extends \Orchestra\Testbench\TestCase { $gozer = new ReflectionClass('\\Superhelio\\Commands\\Commands\\Gozer'); $this->assertTrue($gozer->hasMethod('handle')); + $this->assertTrue($gozer->hasProperty('signature')); + $this->assertTrue($gozer->hasProperty('description')); $this->assertTrue($gozer->hasProperty('dbPrefix')); } + + public function test_gozer_finds_database_prefix() + { + $gozer = new Gozer(); + + $this->assertEquals('gozerTest__', $gozer->getDatabasePrefix()); + } + + public function test_gozer_finds_users_table() + { + $gozer = new Gozer(); + + $connection = $gozer->getConnection(); + + $tables = $gozer->getTables($connection); + $this->assertTrue(in_array('gozerTest__users', $tables, false)); + + $gozer->setDatabasePrefix('gozerTest__'); + $filteredTables = $gozer->getFilteredTables($tables); + $this->assertTrue(is_a($filteredTables, \Illuminate\Support\Collection::class)); + $this->assertTrue(in_array('gozerTest__users', $filteredTables->toArray(), false)); + } + + public function test_gozer_table_filtering_works() + { + $gozer = new Gozer(); + $tables = array( + 'gozerTest__users', + 'gozerTest__migrations', + 'this_should_be_filtered', + 'filter_me_too' + ); + + $gozer->setDatabasePrefix('gozerTest__'); + $filtered = $gozer->getFilteredTables($tables); + $array = $filtered->toArray(); + + $this->assertFalse(in_array('this_should_be_filtered', $array, false)); + $this->assertFalse(in_array('filter_me_too', $array, false)); + $this->assertTrue(in_array('gozerTest__users', $array, false)); + $this->assertTrue(in_array('gozerTest__migrations', $array, false)); + } } From cf7d4d37de5194e680d33120b9ef37fff60f5f2e Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Sun, 27 Aug 2017 19:20:53 +0300 Subject: [PATCH 20/20] Release CHANGELOG and few fixes to README --- CHANGELOG.md | 3 +++ README.md | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f81fd65..290d1c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [1.1.0](https://github.com/superhelio/commands/tree/2.0.0) (2017-08-27) +- New command: `superhelio:gozer` with tests + ## [1.0.0](https://github.com/superhelio/commands/tree/1.0.0) (2017-02-08) - Fixed Scrutinizer reported issue - Renamed LICENSE diff --git a/README.md b/README.md index de752e2..545a785 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ public function register() ## Usage +- *superhelio:gozer* + - Force delete database tables that has your table prefix + - `php artisan superhelio:gozer` - *superhelio:reload* - Reset database, migrate and seed - `php artisan superhelio:reload` @@ -47,10 +50,6 @@ Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recen $ composer test ``` -## Contributing - -Please see [CONTRIBUTING](CONTRIBUTING.md) for details. - ## Security If you discover any security related issues, please contact [SuperHelio](link-author). @@ -77,4 +76,4 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio [link-code-quality]: https://scrutinizer-ci.com/g/superhelio/commands [link-downloads]: https://packagist.org/packages/superhelio/commands [link-author]: https://github.com/superhelio -[link-contributors]: ../../contributors +[link-contributors]: https://github.com/superhelio/commands/graphs/contributors