Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new adminlte:remove command #1299

Merged
merged 6 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}