Skip to content

Commit 7500de1

Browse files
test(remove): testing remove command
1 parent 4490d23 commit 7500de1

File tree

3 files changed

+78
-14
lines changed

3 files changed

+78
-14
lines changed

src/Commands/RemoveComponentCommand.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,15 @@
22

33
namespace Sheaf\Cli\Commands;
44

5-
use Sheaf\Cli\Services\ComponentInstaller;
6-
use Sheaf\Cli\Support\InstallationConfig;
75
use Illuminate\Console\Command;
86
use Illuminate\Support\Arr;
9-
use Illuminate\Support\Str;
107
use Sheaf\Cli\Services\ComponentRemover;
118

129
use function Laravel\Prompts\text;
1310

1411
class RemoveComponentCommand extends Command
1512
{
1613

17-
public function __construct(protected ComponentRemover $componentRemover) {
18-
parent::__construct();
19-
}
2014
/**
2115
* The name and signature of the console command.
2216
*
@@ -39,14 +33,20 @@ public function handle()
3933
{
4034

4135
$componentNames = $this->getComponentName();
36+
$success = Command::SUCCESS;
37+
38+
$componentRemover = new ComponentRemover($this);
4239

4340
foreach ($componentNames as $name) {
4441

4542
$this->banner("Removing all $name files");
4643

47-
$this->componentRemover->remove($name);
44+
$success = $componentRemover->remove($name);
45+
}
46+
47+
if($success === Command::SUCCESS) {
48+
$this->info("+ updated sheaf-lock.json and sheaf.json files");
4849
}
49-
$this->info("+ updated sheaf-lock.json and sheaf.json files");
5050
return Command::SUCCESS;
5151
}
5252

src/Services/ComponentRemover.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,51 @@
33

44
namespace Sheaf\Cli\Services;
55

6+
use Illuminate\Console\Command;
67
use Illuminate\Support\Facades\File;
7-
use Symfony\Component\Console\Output\ConsoleOutput;
8-
8+
use Illuminate\Console\Concerns\InteractsWithIO;
9+
use Laravel\Prompts\Output\ConsoleOutput;
10+
use Symfony\Component\Console\Input\StringInput;
911

1012
class ComponentRemover
1113
{
1214
protected $output;
1315
protected $componentName;
1416

15-
public function __construct()
17+
public function __construct(protected $command)
1618
{
17-
$this->output = new ConsoleOutput();
1819
}
1920

2021
public function remove($name)
2122
{
2223
$this->componentName = $name;
2324

25+
$isExists = $this->checkComponentExistence();
26+
27+
if(!$isExists) {
28+
$this->message("Component is not installed in this project.");
29+
return Command::FAILURE;
30+
}
31+
2432
$this->deleteComponentFiles();
2533

2634
$this->cleaningSheafLock($name);
2735
}
2836

37+
protected function checkComponentExistence()
38+
{
39+
40+
return File::exists(resource_path("views/components/ui/{$this->componentName}")) ||
41+
File::exists(resource_path("views/components/ui/{$this->componentName}.blade.php"));
42+
}
43+
2944
protected function deleteComponentFiles()
3045
{
3146
$componentDirectory = resource_path("views/components/ui/{$this->componentName}");
3247

3348
if (File::isDirectory($componentDirectory)) {
3449
File::deleteDirectory($componentDirectory);
35-
$this->message("+ Deleted directory: $componentDirectory");
50+
$this->message("+ Deleted directory: resources/views/components/ui/{$this->componentName}");
3651
}
3752

3853
$componentFile = resource_path("views/components/ui/{$this->componentName}.blade.php");
@@ -136,6 +151,6 @@ protected function deleteHelperFile($helper)
136151

137152
protected function message(string $message)
138153
{
139-
$this->output->writeln("<fg=green>$message</fg=green>");
154+
$this->command->info("<fg=green>$message</fg=green>");
140155
}
141156
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\File;
4+
5+
it("removes an installed component", function () {
6+
7+
$component = 'separator';
8+
$componentDirectory = resource_path("views/components/ui/$component");
9+
10+
$this->artisan("sheaf:install $component")
11+
->assertExitCode(0)
12+
->run();
13+
14+
15+
expect(File::isDirectory($componentDirectory))->toBeTrue();
16+
17+
$this->view("components.ui.$component.index");
18+
19+
$this->artisan("sheaf:remove $component")
20+
->assertExitCode(0)
21+
->expectsOutputToContain("Deleted directory: resources/views/components/ui/$component")
22+
->run();
23+
24+
expect(File::isDirectory($componentDirectory))->toBeFalse();
25+
26+
$sheafLock = File::get(base_path("sheaf-lock.json"));
27+
28+
expect($sheafLock)->not->toContain("$component");
29+
30+
});
31+
32+
it("quits successfully when attempting to remove a non-installed component", function () {
33+
34+
$component = 'modal';
35+
$componentDirectory = resource_path("views/components/ui/$component");
36+
37+
38+
$this->artisan("sheaf:remove $component")
39+
->assertExitCode(0)
40+
->expectsOutputToContain("Component is not installed in this project.")
41+
->run();
42+
43+
expect(File::isDirectory($componentDirectory))->toBeFalse();
44+
45+
$sheafLock = File::get(base_path("sheaf-lock.json"));
46+
47+
expect($sheafLock)->not->toContain("$component");
48+
49+
});

0 commit comments

Comments
 (0)