mirror of
https://github.com/superhelio/commands.git
synced 2026-01-26 03:33:59 +00:00
Gozer: Refactoring and tests
This commit is contained in:
@@ -22,7 +22,8 @@
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit" : "4.*",
|
||||
"orchestra/testbench": "~3.0"
|
||||
"orchestra/testbench": "~3.0",
|
||||
"doctrine/dbal": "~2.3"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
<?php
|
||||
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;
|
||||
|
||||
class GozerTest extends \Orchestra\Testbench\TestCase
|
||||
{
|
||||
@@ -15,10 +18,11 @@ class GozerTest extends \Orchestra\Testbench\TestCase
|
||||
parent::setUp();
|
||||
$this->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'));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user