Gozer: Refactoring and tests

This commit is contained in:
Ismo Vuorinen
2017-02-14 13:47:33 +02:00
parent 5fe7412588
commit f14a1d8769
3 changed files with 105 additions and 37 deletions

View File

@@ -22,7 +22,8 @@
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit" : "4.*", "phpunit/phpunit" : "4.*",
"orchestra/testbench": "~3.0" "orchestra/testbench": "~3.0",
"doctrine/dbal": "~2.3"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@@ -4,6 +4,7 @@ namespace Superhelio\Commands\Commands;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class Gozer extends Command class Gozer extends Command
{ {
@@ -21,6 +22,11 @@ class Gozer extends Command
*/ */
protected $description = 'Force delete database tables that has your table prefix'; protected $description = 'Force delete database tables that has your table prefix';
/**
* @var string Database table prefix
*/
private $dbPrefix = '';
/** /**
* Create a new command instance. * Create a new command instance.
*/ */
@@ -56,34 +62,20 @@ class Gozer extends Command
$tables = []; $tables = [];
$dbPrefix = trim(DB::getTablePrefix()); $this->dbPrefix = $this->getDatabasePrefix();
$confirmationQuestion = 'Delete all of your database tables?'; $confirmationQuestion = 'Delete all of your database tables?';
if (!empty($dbPrefix)) { if (!empty($this->dbPrefix)) {
$confirmationQuestion = sprintf( $confirmationQuestion = sprintf(
'Delete your tables that begin with %s*', 'Delete your tables that begin with %s*',
$dbPrefix $this->dbPrefix
); );
} }
if ($this->confirm($confirmationQuestion)) { 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; $connection = $this->getConnection();
} $tables = $this->getTables($connection);
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. * Reject tables that do not have specified table prefix.
@@ -92,9 +84,7 @@ class Gozer extends Command
* *
* @var \Illuminate\Support\Collection $tables * @var \Illuminate\Support\Collection $tables
*/ */
$tables = collect($tables)->reject(function ($table) use ($dbPrefix) { $tables = $this->getFilteredTables($tables);
return !starts_with($table, $dbPrefix);
});
/** /**
* Check that we got at least one table, bail out if not * 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 */ /** Fancy pants progress bar to see your tables get destroyed */
$bar = $this->output->createProgressBar($tables->count()); $bar = $this->output->createProgressBar($tables->count());
\Illuminate\Support\Facades\Schema::disableForeignKeyConstraints(); Schema::disableForeignKeyConstraints();
$tables->each(function ($table) use ($bar, $connection) { $tables->each(function ($table) use ($bar, $connection) {
/** Drop the table */ /** Drop the table */
@@ -132,7 +122,7 @@ class Gozer extends Command
$bar->advance(); $bar->advance();
}); });
\Illuminate\Support\Facades\Schema::enableForeignKeyConstraints(); Schema::enableForeignKeyConstraints();
/** Progress bar is now finished */ /** Progress bar is now finished */
$bar->finish(); $bar->finish();
@@ -147,4 +137,59 @@ class Gozer extends Command
return true; 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);
});
}
} }

View File

@@ -1,9 +1,12 @@
<?php <?php
namespace Superhelio\Commands; namespace Superhelio\Commands;
use ReflectionClass;
use Superhelio\Commands\Commands\Gozer; use Superhelio\Commands\Commands\Gozer;
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
class GozerTest extends \Orchestra\Testbench\TestCase class GozerTest extends \Orchestra\Testbench\TestCase
{ {
@@ -15,10 +18,11 @@ class GozerTest extends \Orchestra\Testbench\TestCase
parent::setUp(); parent::setUp();
$this->artisan('migrate', ['--database' => 'testbench']); $this->artisan('migrate', ['--database' => 'testbench']);
} }
/** /**
* Define environment setup. * Define environment setup.
* *
* @param \Illuminate\Foundation\Application $app * @param \Illuminate\Foundation\Application $app
* *
* @return void * @return void
*/ */
@@ -27,19 +31,20 @@ class GozerTest extends \Orchestra\Testbench\TestCase
// Setup default database to use sqlite :memory: // Setup default database to use sqlite :memory:
$app['config']->set('database.default', 'testbench'); $app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [ $app['config']->set('database.connections.testbench', [
'driver' => 'sqlite', 'driver' => 'sqlite',
'database' => ':memory:', 'database' => ':memory:',
'prefix' => '', 'prefix' => 'gozerTest__',
]); ]);
} }
/** /**
* Get package providers. At a minimum this is the package being tested, but also * Get package providers.
* would include packages upon which our package depends, e.g. Cartalyst/Sentry * At a minimum this is the package being tested, but also
* In a normal app environment these would be added to the 'providers' array in * would include packages upon which our package depends.
* the config/app.php file. * 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 * @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', 'name' => 'User name',
'email' => 'hello@gozer.dev', 'email' => 'hello@gozer.dev',
'password' => bcrypt('123') '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('hello@gozer.dev', $users->email);
$this->assertEquals('User name', $users->name); $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'));
} }
} }