Skip to content

Commit

Permalink
Add new adminlte:remove command (#1299)
Browse files Browse the repository at this point in the history
* [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
dfsmania authored Aug 4, 2024
1 parent 8643840 commit b254870
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/AdminLteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use JeroenNoten\LaravelAdminLte\Console\AdminLteInstallCommand;
use JeroenNoten\LaravelAdminLte\Console\AdminLtePluginCommand;
use JeroenNoten\LaravelAdminLte\Console\AdminLteRemoveCommand;
use JeroenNoten\LaravelAdminLte\Console\AdminLteStatusCommand;
use JeroenNoten\LaravelAdminLte\Console\AdminLteUpdateCommand;
use JeroenNoten\LaravelAdminLte\View\Components\Form;
Expand Down Expand Up @@ -157,9 +158,10 @@ private function registerCommands()
if ($this->app->runningInConsole()) {
$this->commands([
AdminLteInstallCommand::class,
AdminLtePluginCommand::class,
AdminLteRemoveCommand::class,
AdminLteStatusCommand::class,
AdminLteUpdateCommand::class,
AdminLtePluginCommand::class,
]);
}
}
Expand Down
120 changes: 120 additions & 0 deletions src/Console/AdminLteRemoveCommand.php
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');
}
}
189 changes: 189 additions & 0 deletions tests/Console/RemoveTest.php
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());
}
}
}

0 comments on commit b254870

Please sign in to comment.