-
-
Notifications
You must be signed in to change notification settings - Fork 60
feat: Add pan:delete command with ID-specific deletion and test case. #10
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
base: main
Are you sure you want to change the base?
Changes from all commits
1a1b827
fd323c2
c73b246
71658fb
16ffb12
9d10c3e
466ff8e
85231bc
8c72772
54011e7
accfc10
5737e56
c08db89
dc94c81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Pan\Adapters\Laravel\Console\Commands; | ||
|
|
||
| use Illuminate\Console\Command; | ||
| use Pan\Contracts\AnalyticsRepository; | ||
|
|
||
| final class PanDeleteCommand extends Command | ||
| { | ||
| /** | ||
| * The name and signature of the console command. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $signature = 'pan:delete {id : The ID of the analytic to delete}'; | ||
|
|
||
| /** | ||
| * The console command description. | ||
| * | ||
| * @var string | ||
| */ | ||
| protected $description = 'Delete analytics by ID.'; | ||
|
|
||
| /** | ||
| * Execute the console command. | ||
| */ | ||
| public function handle(AnalyticsRepository $repository): void | ||
| { | ||
| $id = (int) $this->argument('id'); | ||
|
|
||
| if ($this->isInvalidId($id)) { | ||
| $this->error('Analytic ID must be greater than 0.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if ($repository->delete($id) !== 0) { | ||
| $this->info('Analytic has been deleted.'); | ||
| } else { | ||
| $this->error('Record not found or already deleted.'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check if the ID is invalid. | ||
| */ | ||
| private function isInvalidId(int $id): bool | ||
| { | ||
| return $id <= 0; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
|
|
||
| use Pan\Contracts\AnalyticsRepository; | ||
|
|
||
| beforeEach(function (): void { | ||
| $this->repository = mock(AnalyticsRepository::class)->makePartial(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the test here, the way it's broken down. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ruchit288 can you assert the database is missing the ID after deleting in these tests instead of the mock? Another way would be, leave the command class test as it is, and have a separate test for the delete of method of the repository (assert database missing the ID deleted). You can check the merged branches, look at the feature test cases on how you can insert some data into the table. That would even prevent hard coding the |
||
| app()->instance(AnalyticsRepository::class, $this->repository); | ||
| }); | ||
|
|
||
| it('deletes a specific analytic by ID', function (): void { | ||
| $this->repository | ||
| ->expects('delete') | ||
| ->with(1) | ||
| ->once() | ||
| ->andReturn(1); | ||
|
|
||
| $this->artisan('pan:delete', ['id' => 1]) | ||
| ->expectsOutput('Analytic has been deleted.') | ||
| ->assertExitCode(0); | ||
| }); | ||
|
|
||
| it('fails when no argument is provided', function (): void { | ||
| $this->artisan('pan:delete') | ||
| ->expectsOutput('Not enough arguments (missing: "id").') | ||
| ->assertExitCode(1); | ||
| }); | ||
|
|
||
| it('handles non-existent analytic gracefully', function (): void { | ||
| $this->repository | ||
| ->expects('delete') | ||
| ->with(26) | ||
| ->once() | ||
| ->andReturn(0); | ||
|
|
||
| $this->artisan('pan:delete', ['id' => 26]) | ||
| ->expectsOutput('Record not found or already deleted.') | ||
| ->assertExitCode(0); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.