-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new adminlte:remove command (#1299)
* [src/Console]: Add new adminlte:remove command * [src/Console]: Fix style CI in new remove command * [tests/Console]: Add tests for the remove command * [tests/Console]: Replace arrow function due to compatibility with old Laravel versions * [tests/Console]: Fix code style in remove command tests * [tests/Console]: Improve remove command tests
- Loading branch information
Showing
3 changed files
with
312 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace JeroenNoten\LaravelAdminLte\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use JeroenNoten\LaravelAdminLte\Console\PackageResources\AdminlteAssetsResource; | ||
use JeroenNoten\LaravelAdminLte\Console\PackageResources\AuthRoutesResource; | ||
use JeroenNoten\LaravelAdminLte\Console\PackageResources\AuthViewsResource; | ||
use JeroenNoten\LaravelAdminLte\Console\PackageResources\BladeComponentsResource; | ||
use JeroenNoten\LaravelAdminLte\Console\PackageResources\ConfigResource; | ||
use JeroenNoten\LaravelAdminLte\Console\PackageResources\LayoutViewsResource; | ||
use JeroenNoten\LaravelAdminLte\Console\PackageResources\TranslationsResource; | ||
|
||
class AdminLteRemoveCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'adminlte:remove | ||
{resource* : The resource to uninstall: assets, config, translations, auth_views, auth_routes, main_views or components} | ||
{--force : To force the uninstall procedure without warnings alerts} | ||
{--interactive : To allow the uninstall process guide you through it}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Uninstalls one or more specified resources'; | ||
|
||
/** | ||
* Array with all the available package resources. | ||
* | ||
* @var array | ||
*/ | ||
protected $pkgResources; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
// Fill the array with the available package resources. | ||
|
||
$this->pkgResources = [ | ||
'assets' => new AdminlteAssetsResource(), | ||
'config' => new ConfigResource(), | ||
'translations' => new TranslationsResource(), | ||
'main_views' => new LayoutViewsResource(), | ||
'auth_views' => new AuthViewsResource(), | ||
'auth_routes' => new AuthRoutesResource(), | ||
'components' => new BladeComponentsResource(), | ||
]; | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
*/ | ||
public function handle() | ||
{ | ||
// Read and verify the resources to be uninstalled. | ||
|
||
foreach ($this->argument('resource') as $res) { | ||
// Check if resource is valid. | ||
|
||
if (! isset($this->pkgResources[$res])) { | ||
$this->comment("The provided resource: {$res} is invalid!"); | ||
|
||
continue; | ||
} | ||
|
||
// Uninstall the resource. | ||
|
||
$this->uninstallPackageResource($res); | ||
} | ||
} | ||
|
||
/** | ||
* Uninstalls a package resource. | ||
* | ||
* @param string $resource The keyword of the resource to uninstall | ||
* @return void | ||
*/ | ||
protected function uninstallPackageResource($resource) | ||
{ | ||
$removeMsg = 'Do you really want to uninstall the resource: :res?'; | ||
$removeMsg = str_replace(':res', $resource, $removeMsg); | ||
$resource = $this->pkgResources[$resource]; | ||
|
||
// Check if the --interactive option is enabled. | ||
|
||
if ($this->option('interactive') && ! $this->confirm($removeMsg)) { | ||
return; | ||
} | ||
|
||
// Check whether to warn for uninstalling a required resource. | ||
|
||
$requiredWarnMsg = 'This resource is required by the package, '; | ||
$requiredWarnMsg .= 'do you really want to uninstall it?'; | ||
|
||
$shouldWarn = ! $this->option('force') && $resource->required; | ||
|
||
if ($shouldWarn && ! $this->confirm($requiredWarnMsg)) { | ||
return; | ||
} | ||
|
||
// Uninstall the resource. | ||
|
||
$resource->uninstall(); | ||
$this->info('The resource was successfully removed'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
<?php | ||
|
||
class RemoveTest extends CommandTestCase | ||
{ | ||
public function testRemoveWithInvalidResource() | ||
{ | ||
$this->artisan('adminlte:remove dummy') | ||
->expectsOutput('The provided resource: dummy is invalid!') | ||
->assertExitCode(0); | ||
} | ||
|
||
public function testRemoveAllResourcesIndividually() | ||
{ | ||
// We can't perfom these tests on old Laravel versions. We need support | ||
// for the expect confirmation method. | ||
|
||
if (! $this->canExpectsConfirmation()) { | ||
$this->assertTrue(true); | ||
|
||
return; | ||
} | ||
|
||
// Test remove command over the available resources. | ||
|
||
foreach ($this->getResources() as $name => $res) { | ||
// Ensure the required vendor assets exists, if needed. | ||
|
||
if ($name === 'assets') { | ||
$this->installVendorAssets(); | ||
} | ||
|
||
// Ensure the target resource is installed. | ||
|
||
$res->install(); | ||
|
||
// Remove resource using the artisan command. | ||
|
||
if ($res->required) { | ||
$confirmMsg = 'This resource is required by the package, '; | ||
$confirmMsg .= 'do you really want to uninstall it?'; | ||
|
||
$this->artisan("adminlte:remove {$name}") | ||
->expectsConfirmation($confirmMsg, 'yes'); | ||
} else { | ||
$this->artisan("adminlte:remove {$name}"); | ||
} | ||
|
||
$this->assertFalse($res->installed()); | ||
} | ||
} | ||
|
||
public function testRemoveAllResourcesAtOnceWithForceFlag() | ||
{ | ||
// We can't perfom these tests on old Laravel versions. We need support | ||
// for the expect confirmation method. | ||
|
||
if (! $this->canExpectsConfirmation()) { | ||
$this->assertTrue(true); | ||
|
||
return; | ||
} | ||
|
||
// Install all the available resources and collect their names. | ||
|
||
$resNames = []; | ||
|
||
foreach ($this->getResources() as $name => $res) { | ||
$resNames[] = $name; | ||
|
||
// Ensure the required vendor assets exists, if needed. | ||
|
||
if ($name === 'assets') { | ||
$this->installVendorAssets(); | ||
} | ||
|
||
// Ensure the target resource is installed. | ||
|
||
$res->install(); | ||
} | ||
|
||
// Test remove all resources at once. | ||
|
||
$resNames = implode(' ', $resNames); | ||
$this->artisan("adminlte:remove --force {$resNames}"); | ||
|
||
// Control that all resources were removed. | ||
|
||
foreach ($this->getResources() as $res) { | ||
$this->assertFalse($res->installed()); | ||
} | ||
} | ||
|
||
public function testRemoveOnRequiredResourcesWithoutConfirmation() | ||
{ | ||
// We can't perfom these tests on old Laravel versions. We need support | ||
// for the expect confirmation method. | ||
|
||
if (! $this->canExpectsConfirmation()) { | ||
$this->assertTrue(true); | ||
|
||
return; | ||
} | ||
|
||
// Get set of required resources. | ||
|
||
$resources = array_filter( | ||
$this->getResources(), | ||
function ($r) { | ||
return $r->required; | ||
} | ||
); | ||
|
||
// Test remove command over the required resources, without | ||
// confirming the action. | ||
|
||
foreach ($resources as $name => $res) { | ||
// Ensure the required vendor assets exists, if needed. | ||
|
||
if ($name === 'assets') { | ||
$this->installVendorAssets(); | ||
} | ||
|
||
// Ensure the target resource is installed. | ||
|
||
$res->install(); | ||
|
||
// Remove resource using the artisan command, but don't confirm the | ||
// action. | ||
|
||
$confirmMsg = 'This resource is required by the package, '; | ||
$confirmMsg .= 'do you really want to uninstall it?'; | ||
|
||
$this->artisan("adminlte:remove {$name}") | ||
->expectsConfirmation($confirmMsg, 'no'); | ||
|
||
// Assert the resource is still installed. | ||
|
||
$this->assertTrue($res->installed()); | ||
|
||
// Clear the installed resource. | ||
|
||
$res->uninstall(); | ||
$this->assertFalse($res->installed()); | ||
} | ||
} | ||
|
||
public function testRemoveInteractiveFlagWithoutConfirmation() | ||
{ | ||
// We can't perfom these tests on old Laravel versions. We need support | ||
// for the expect confirmation method. | ||
|
||
if (! $this->canExpectsConfirmation()) { | ||
$this->assertTrue(true); | ||
|
||
return; | ||
} | ||
|
||
// Test remove command over the resources when using --interactive. | ||
|
||
foreach ($this->getResources() as $name => $res) { | ||
$confirmMsg = 'Do you really want to uninstall the resource: :res?'; | ||
$confirmMsg = str_replace(':res', $name, $confirmMsg); | ||
|
||
// Ensure the required vendor assets exists, if needed. | ||
|
||
if ($name === 'assets') { | ||
$this->installVendorAssets(); | ||
} | ||
|
||
// Ensure the target resource is installed. | ||
|
||
$res->install(); | ||
|
||
// Test with --interactive option and respond with NO. | ||
|
||
$this->artisan("adminlte:remove {$name} --interactive") | ||
->expectsConfirmation($confirmMsg, 'no'); | ||
|
||
// Assert the resource is still installed. | ||
|
||
$this->assertTrue($res->installed()); | ||
|
||
// Clear the installed resource. | ||
|
||
$res->uninstall(); | ||
$this->assertFalse($res->installed()); | ||
} | ||
} | ||
} |