From eaad2c2b8ee4e0707ea9232c57084313c54d98cb Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Sat, 3 Aug 2024 13:50:23 -0300 Subject: [PATCH 1/6] [src/Console]: Add new adminlte:remove command --- src/AdminLteServiceProvider.php | 4 +- src/Console/AdminLteRemoveCommand.php | 121 ++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 src/Console/AdminLteRemoveCommand.php diff --git a/src/AdminLteServiceProvider.php b/src/AdminLteServiceProvider.php index 2aa95a14..2acbb271 100644 --- a/src/AdminLteServiceProvider.php +++ b/src/AdminLteServiceProvider.php @@ -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; @@ -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, ]); } } diff --git a/src/Console/AdminLteRemoveCommand.php b/src/Console/AdminLteRemoveCommand.php new file mode 100644 index 00000000..849bc009 --- /dev/null +++ b/src/Console/AdminLteRemoveCommand.php @@ -0,0 +1,121 @@ +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.'); + } +} From b4a3c9d8f5c316945123be9d22812caeca00b53e Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Sat, 3 Aug 2024 13:59:05 -0300 Subject: [PATCH 2/6] [src/Console]: Fix style CI in new remove command --- src/Console/AdminLteRemoveCommand.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Console/AdminLteRemoveCommand.php b/src/Console/AdminLteRemoveCommand.php index 849bc009..7c4b2b5d 100644 --- a/src/Console/AdminLteRemoveCommand.php +++ b/src/Console/AdminLteRemoveCommand.php @@ -69,7 +69,6 @@ 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])) { @@ -116,6 +115,6 @@ protected function uninstallPackageResource($resource) // Uninstall the resource. $resource->uninstall(); - $this->info('The resource was successfully removed.'); + $this->info('The resource was successfully removed'); } } From d11d48ad82b277fa31050c4ac9e39cda43a09456 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Sat, 3 Aug 2024 17:24:29 -0300 Subject: [PATCH 3/6] [tests/Console]: Add tests for the remove command --- tests/Console/RemoveTest.php | 146 +++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 tests/Console/RemoveTest.php diff --git a/tests/Console/RemoveTest.php b/tests/Console/RemoveTest.php new file mode 100644 index 00000000..faec3a1b --- /dev/null +++ b/tests/Console/RemoveTest.php @@ -0,0 +1,146 @@ +artisan('adminlte:remove dummy') + ->expectsOutput('The provided resource: dummy is invalid!') + ->assertExitCode(0); + } + + public function testRemoveOnAllResources() + { + // 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) { + $msg = 'This resource is required by the package, '; + $msg .= 'do you really want to uninstall it?'; + + $this->artisan("adminlte:remove {$name}") + ->expectsConfirmation($msg, 'yes'); + } else { + $this->artisan("adminlte:remove {$name}"); + } + + $this->assertFalse($res->installed()); + } + } + + public function testNotConfirmRemoveOnRequiredResources() + { + // 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(), + fn ($r) => $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. + + $msg = 'This resource is required by the package, '; + $msg .= 'do you really want to uninstall it?'; + + $this->artisan("adminlte:remove {$name}") + ->expectsConfirmation($msg, 'no'); + + // Assert the resource is still installed. + + $this->assertTrue($res->installed()); + + // Clear the installed resource. + + $res->uninstall(); + $this->assertFalse($res->installed()); + } + } + + public function testNotConfirmRemoveWithInteractiveFlag() + { + // 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()); + } + } +} From 3c86388fb75abf1e299264938db3ab50056df76b Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Sat, 3 Aug 2024 17:29:00 -0300 Subject: [PATCH 4/6] [tests/Console]: Replace arrow function due to compatibility with old Laravel versions --- tests/Console/RemoveTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Console/RemoveTest.php b/tests/Console/RemoveTest.php index faec3a1b..b3e7ec95 100644 --- a/tests/Console/RemoveTest.php +++ b/tests/Console/RemoveTest.php @@ -64,7 +64,7 @@ public function testNotConfirmRemoveOnRequiredResources() $resources = array_filter( $this->getResources(), - fn ($r) => $r->required + function ($r) { return $r->required; } ); // Test remove command over the required resources, without From c2dba49bdfcfdbc2a543b13b53137aab6a187a63 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Sat, 3 Aug 2024 17:30:36 -0300 Subject: [PATCH 5/6] [tests/Console]: Fix code style in remove command tests --- tests/Console/RemoveTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Console/RemoveTest.php b/tests/Console/RemoveTest.php index b3e7ec95..a17e7cbe 100644 --- a/tests/Console/RemoveTest.php +++ b/tests/Console/RemoveTest.php @@ -64,7 +64,9 @@ public function testNotConfirmRemoveOnRequiredResources() $resources = array_filter( $this->getResources(), - function ($r) { return $r->required; } + function ($r) { + return $r->required; + } ); // Test remove command over the required resources, without From 994a48dd0ba35e145ee7b490ccb78a55ee5128a6 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Sun, 4 Aug 2024 11:20:40 -0300 Subject: [PATCH 6/6] [tests/Console]: Improve remove command tests --- tests/Console/RemoveTest.php | 59 ++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/tests/Console/RemoveTest.php b/tests/Console/RemoveTest.php index a17e7cbe..8c922a04 100644 --- a/tests/Console/RemoveTest.php +++ b/tests/Console/RemoveTest.php @@ -9,7 +9,7 @@ public function testRemoveWithInvalidResource() ->assertExitCode(0); } - public function testRemoveOnAllResources() + public function testRemoveAllResourcesIndividually() { // We can't perfom these tests on old Laravel versions. We need support // for the expect confirmation method. @@ -36,11 +36,11 @@ public function testRemoveOnAllResources() // Remove resource using the artisan command. if ($res->required) { - $msg = 'This resource is required by the package, '; - $msg .= 'do you really want to uninstall it?'; + $confirmMsg = 'This resource is required by the package, '; + $confirmMsg .= 'do you really want to uninstall it?'; $this->artisan("adminlte:remove {$name}") - ->expectsConfirmation($msg, 'yes'); + ->expectsConfirmation($confirmMsg, 'yes'); } else { $this->artisan("adminlte:remove {$name}"); } @@ -49,7 +49,48 @@ public function testRemoveOnAllResources() } } - public function testNotConfirmRemoveOnRequiredResources() + 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. @@ -86,11 +127,11 @@ function ($r) { // Remove resource using the artisan command, but don't confirm the // action. - $msg = 'This resource is required by the package, '; - $msg .= 'do you really want to uninstall it?'; + $confirmMsg = 'This resource is required by the package, '; + $confirmMsg .= 'do you really want to uninstall it?'; $this->artisan("adminlte:remove {$name}") - ->expectsConfirmation($msg, 'no'); + ->expectsConfirmation($confirmMsg, 'no'); // Assert the resource is still installed. @@ -103,7 +144,7 @@ function ($r) { } } - public function testNotConfirmRemoveWithInteractiveFlag() + public function testRemoveInteractiveFlagWithoutConfirmation() { // We can't perfom these tests on old Laravel versions. We need support // for the expect confirmation method.