From 1a1b82797d176214296e307ebee0e1aecd4147c6 Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Tue, 15 Oct 2024 19:14:17 +0530 Subject: [PATCH 01/12] feat: Add pan:delete command with ID-specific deletion. --- .../Console/Commands/PanDeleteCommand.php | 40 +++++++++++++++++++ .../Laravel/Providers/PanServiceProvider.php | 2 + .../DatabaseAnalyticsRepository.php | 9 +++++ src/Contracts/AnalyticsRepository.php | 5 +++ 4 files changed, 56 insertions(+) create mode 100644 src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php diff --git a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php new file mode 100644 index 0000000..3268975 --- /dev/null +++ b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php @@ -0,0 +1,40 @@ +option('id'); + + if (! $id) { + $this->error('Please specify--id'); + } + + $repository->delete((int) $id); + $this->info('Analytic has been deleted.'); + } +} diff --git a/src/Adapters/Laravel/Providers/PanServiceProvider.php b/src/Adapters/Laravel/Providers/PanServiceProvider.php index 07cda67..4593e90 100644 --- a/src/Adapters/Laravel/Providers/PanServiceProvider.php +++ b/src/Adapters/Laravel/Providers/PanServiceProvider.php @@ -9,6 +9,7 @@ use Illuminate\Support\ServiceProvider; use Pan\Adapters\Laravel\Console\Commands\InstallPanCommand; use Pan\Adapters\Laravel\Console\Commands\PanCommand; +use Pan\Adapters\Laravel\Console\Commands\PanDeleteCommand; use Pan\Adapters\Laravel\Console\Commands\PanFlushCommand; use Pan\Adapters\Laravel\Http\Controllers\EventController; use Pan\Adapters\Laravel\Http\Middleware\InjectJavascriptLibrary; @@ -83,6 +84,7 @@ private function registerCommands(): void InstallPanCommand::class, PanCommand::class, PanFlushCommand::class, + PanDeleteCommand::class, ]); } } diff --git a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php index 191928f..36b4680 100644 --- a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php +++ b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php @@ -58,4 +58,13 @@ public function flush(): void { DB::table('pan_analytics')->truncate(); } + + /** + * Delete a specific analytic by ID. + */ + public function delete(int $id): void + { + DB::table('pan_analytics')->where('id', $id)->delete(); + + } } diff --git a/src/Contracts/AnalyticsRepository.php b/src/Contracts/AnalyticsRepository.php index ca04ffe..ef48741 100644 --- a/src/Contracts/AnalyticsRepository.php +++ b/src/Contracts/AnalyticsRepository.php @@ -28,4 +28,9 @@ public function increment(string $name, EventType $event): void; * Flush all analytics. */ public function flush(): void; + + /** + * Delete a specific analytic by ID. + */ + public function delete(int $id): void; } From fd323c27ffbc96a996821b8b1eecf35319755d44 Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Tue, 15 Oct 2024 19:14:50 +0530 Subject: [PATCH 02/12] feat: Add pan:delete test case. --- .../Feature/Laravel/Console/PanDeleteTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/Feature/Laravel/Console/PanDeleteTest.php diff --git a/tests/Feature/Laravel/Console/PanDeleteTest.php b/tests/Feature/Laravel/Console/PanDeleteTest.php new file mode 100644 index 0000000..472ba8b --- /dev/null +++ b/tests/Feature/Laravel/Console/PanDeleteTest.php @@ -0,0 +1,22 @@ +repository = mock(AnalyticsRepository::class)->makePartial(); + app()->instance(AnalyticsRepository::class, $this->repository); +}); + +it('deletes a specific analytic by ID', function (): void { + $this->repository->shouldReceive('delete')->once()->with(1)->andReturn(); + + $command = artisan('pan:delete', ['--id' => 1]); + + expect($command->exitCode())->toBe(0); + expect($command->output())->toContain('Analytic has been deleted.'); +}); + +it('fails when no option is provided', function (): void { + $command = artisan('pan:delete'); + + expect($command->exitCode())->toBe(1); + expect($command->output())->toContain('Please specify --id option.'); +}); From c73b246d0e60496a63fc0f299a48687ba511c524 Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Thu, 17 Oct 2024 10:51:46 +0530 Subject: [PATCH 03/12] Update src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php Co-authored-by: Taghwo Millionaire <40868373+taghwo@users.noreply.github.com> --- src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php index 3268975..31052c7 100644 --- a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php +++ b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php @@ -31,7 +31,7 @@ public function handle(AnalyticsRepository $repository): void $id = $this->option('id'); if (! $id) { - $this->error('Please specify--id'); + $this->error('Please specify --id='); } $repository->delete((int) $id); From 71658fbcd0fad237080bb6e2de80b3cd264fab8d Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Thu, 17 Oct 2024 10:51:55 +0530 Subject: [PATCH 04/12] Update src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php Co-authored-by: Taghwo Millionaire <40868373+taghwo@users.noreply.github.com> --- .../Laravel/Repositories/DatabaseAnalyticsRepository.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php index 36b4680..9c2d5a7 100644 --- a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php +++ b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php @@ -60,6 +60,8 @@ public function flush(): void } /** + * {@inheritdoc} + */ * Delete a specific analytic by ID. */ public function delete(int $id): void From 16ffb1250ae8475277a8710d969547cfb51afea1 Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Thu, 17 Oct 2024 10:54:20 +0530 Subject: [PATCH 05/12] Update PanDeleteCommand.php --- src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php index 31052c7..3f42438 100644 --- a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php +++ b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php @@ -21,7 +21,7 @@ class PanDeleteCommand extends Command * * @var string */ - protected $description = 'Delete analytics by ID or all at once'; + protected $description = 'Delete analytics by ID.'; /** * Execute the console command. From 466ff8ee41e86206eb3ff122b90caab860a200c1 Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Thu, 17 Oct 2024 15:15:34 +0530 Subject: [PATCH 06/12] Update command description and modify conditional statement. --- .../Laravel/Console/Commands/PanDeleteCommand.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php index 3f42438..f4e7a9f 100644 --- a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php +++ b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php @@ -14,7 +14,7 @@ class PanDeleteCommand extends Command * * @var string */ - protected $signature = 'pan:delete {--id= : The ID of the analytic to delete}'; + protected $signature = 'pan:delete {id : The ID of the analytic to delete}'; /** * The console command description. @@ -28,13 +28,13 @@ class PanDeleteCommand extends Command */ public function handle(AnalyticsRepository $repository): void { - $id = $this->option('id'); + $id = (int) $this->argument('id'); - if (! $id) { - $this->error('Please specify --id='); + if ($id > 0) { + $repository->delete($id); + $this->info('Analytic has been deleted.'); } - $repository->delete((int) $id); - $this->info('Analytic has been deleted.'); + $this->error('The ID must be a positive integer.'); } } From 85231bc7628341ea82cefe1897e8aae70af8934b Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Thu, 17 Oct 2024 15:16:24 +0530 Subject: [PATCH 07/12] Update comment. --- .../Laravel/Repositories/DatabaseAnalyticsRepository.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php index 9c2d5a7..36b4680 100644 --- a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php +++ b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php @@ -60,8 +60,6 @@ public function flush(): void } /** - * {@inheritdoc} - */ * Delete a specific analytic by ID. */ public function delete(int $id): void From 8c7277288d2853d3a4a5cbf6c6dcb94ddacd3b32 Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Thu, 17 Oct 2024 15:20:55 +0530 Subject: [PATCH 08/12] Refactor PanDeleteTest. --- .../Feature/Laravel/Console/PanDeleteTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/Feature/Laravel/Console/PanDeleteTest.php b/tests/Feature/Laravel/Console/PanDeleteTest.php index 472ba8b..2ace97d 100644 --- a/tests/Feature/Laravel/Console/PanDeleteTest.php +++ b/tests/Feature/Laravel/Console/PanDeleteTest.php @@ -1,5 +1,7 @@ repository = mock(AnalyticsRepository::class)->makePartial(); app()->instance(AnalyticsRepository::class, $this->repository); @@ -8,15 +10,13 @@ it('deletes a specific analytic by ID', function (): void { $this->repository->shouldReceive('delete')->once()->with(1)->andReturn(); - $command = artisan('pan:delete', ['--id' => 1]); - - expect($command->exitCode())->toBe(0); - expect($command->output())->toContain('Analytic has been deleted.'); + $this->artisan('pan:delete', ['id' => 1]) + ->assertExitCode(0) + ->expectsOutputToContain('Analytic has been deleted.'); }); -it('fails when no option is provided', function (): void { - $command = artisan('pan:delete'); - - expect($command->exitCode())->toBe(1); - expect($command->output())->toContain('Please specify --id option.'); +it('fails when no argument is provided', function (): void { + $this->artisan('pan:delete') + ->assertExitCode(1) + ->assertOutputContains('Not enough arguments (missing: "id").'); }); From accfc1075e918cd5ab40c69d91cd347728ba84c3 Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Thu, 17 Oct 2024 16:56:56 +0530 Subject: [PATCH 09/12] Refactor delete method to improve readability. Removed the intermediate variable for more concise code. --- .../Laravel/Console/Commands/PanDeleteCommand.php | 9 +++++---- .../Laravel/Repositories/DatabaseAnalyticsRepository.php | 7 ++++--- src/Contracts/AnalyticsRepository.php | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php index f4e7a9f..7e1def3 100644 --- a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php +++ b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php @@ -31,10 +31,11 @@ public function handle(AnalyticsRepository $repository): void $id = (int) $this->argument('id'); if ($id > 0) { - $repository->delete($id); - $this->info('Analytic has been deleted.'); + $this->info( + $repository->delete($id) + ); + } else { + $this->error('Analytic ID must be a positive integer.'); } - - $this->error('The ID must be a positive integer.'); } } diff --git a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php index 36b4680..86b0ef5 100644 --- a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php +++ b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php @@ -62,9 +62,10 @@ public function flush(): void /** * Delete a specific analytic by ID. */ - public function delete(int $id): void + public function delete(int $id): string { - DB::table('pan_analytics')->where('id', $id)->delete(); - + return DB::table('pan_analytics')->where('id', $id)->delete() + ? "Analytic has been deleted." + : "Analytic not found or already deleted."; } } diff --git a/src/Contracts/AnalyticsRepository.php b/src/Contracts/AnalyticsRepository.php index ef48741..648dfaf 100644 --- a/src/Contracts/AnalyticsRepository.php +++ b/src/Contracts/AnalyticsRepository.php @@ -32,5 +32,5 @@ public function flush(): void; /** * Delete a specific analytic by ID. */ - public function delete(int $id): void; + public function delete(int $id): string; } From 5737e56b3fd3f1c0d936729be6d01caece7bec86 Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Thu, 17 Oct 2024 17:51:52 +0530 Subject: [PATCH 10/12] Refactor test case and add non-existent analytic test case. --- database/database.sqlite | 0 .../Feature/Laravel/Console/PanDeleteTest.php | 22 ++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 database/database.sqlite diff --git a/database/database.sqlite b/database/database.sqlite new file mode 100644 index 0000000..e69de29 diff --git a/tests/Feature/Laravel/Console/PanDeleteTest.php b/tests/Feature/Laravel/Console/PanDeleteTest.php index 2ace97d..e92d7c2 100644 --- a/tests/Feature/Laravel/Console/PanDeleteTest.php +++ b/tests/Feature/Laravel/Console/PanDeleteTest.php @@ -8,15 +8,31 @@ }); it('deletes a specific analytic by ID', function (): void { - $this->repository->shouldReceive('delete')->once()->with(1)->andReturn(); + $this->repository + ->expects('delete') + ->with(1) + ->once() + ->andReturn('Analytic has been deleted.'); $this->artisan('pan:delete', ['id' => 1]) ->assertExitCode(0) - ->expectsOutputToContain('Analytic has been deleted.'); + ->expectsOutput('Analytic has been deleted.'); }); it('fails when no argument is provided', function (): void { $this->artisan('pan:delete') ->assertExitCode(1) - ->assertOutputContains('Not enough arguments (missing: "id").'); + ->expectsOutput('Not enough arguments (missing: "id").'); +}); + +it('handles non-existent analytic gracefully', function (): void { + $this->repository + ->expects('delete') + ->with(26) + ->once() + ->andReturn('Analytic not found or already deleted.'); + + $this->artisan('pan:delete', ['id' => 26]) + ->assertExitCode(0) + ->expectsOutput('Analytic not found or already deleted.'); }); From c08db892eb93f84c3f3dd87f7b7392d0798822f0 Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Thu, 17 Oct 2024 18:28:17 +0530 Subject: [PATCH 11/12] Refactor delete command and test case. --- .../Console/Commands/PanDeleteCommand.php | 22 ++++++++++++++----- .../DatabaseAnalyticsRepository.php | 6 ++--- src/Contracts/AnalyticsRepository.php | 2 +- .../Feature/Laravel/Console/PanDeleteTest.php | 16 +++++++------- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php index 7e1def3..d206b89 100644 --- a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php +++ b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php @@ -30,12 +30,24 @@ public function handle(AnalyticsRepository $repository): void { $id = (int) $this->argument('id'); - if ($id > 0) { - $this->info( - $repository->delete($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('Analytic ID must be a positive integer.'); + $this->error('Record not found or already deleted.'); } } + + /** + * Check if the ID is invalid. + */ + private function isInvalidId(int $id): bool + { + return $id <= 0; + } } diff --git a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php index 86b0ef5..95de54a 100644 --- a/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php +++ b/src/Adapters/Laravel/Repositories/DatabaseAnalyticsRepository.php @@ -62,10 +62,8 @@ public function flush(): void /** * Delete a specific analytic by ID. */ - public function delete(int $id): string + public function delete(int $id): int { - return DB::table('pan_analytics')->where('id', $id)->delete() - ? "Analytic has been deleted." - : "Analytic not found or already deleted."; + return DB::table('pan_analytics')->where('id', $id)->delete(); } } diff --git a/src/Contracts/AnalyticsRepository.php b/src/Contracts/AnalyticsRepository.php index 648dfaf..29af0ee 100644 --- a/src/Contracts/AnalyticsRepository.php +++ b/src/Contracts/AnalyticsRepository.php @@ -32,5 +32,5 @@ public function flush(): void; /** * Delete a specific analytic by ID. */ - public function delete(int $id): string; + public function delete(int $id): int; } diff --git a/tests/Feature/Laravel/Console/PanDeleteTest.php b/tests/Feature/Laravel/Console/PanDeleteTest.php index e92d7c2..1131439 100644 --- a/tests/Feature/Laravel/Console/PanDeleteTest.php +++ b/tests/Feature/Laravel/Console/PanDeleteTest.php @@ -12,17 +12,17 @@ ->expects('delete') ->with(1) ->once() - ->andReturn('Analytic has been deleted.'); + ->andReturn(1); $this->artisan('pan:delete', ['id' => 1]) - ->assertExitCode(0) - ->expectsOutput('Analytic has been deleted.'); + ->expectsOutput('Analytic has been deleted.') + ->assertExitCode(0); }); it('fails when no argument is provided', function (): void { $this->artisan('pan:delete') - ->assertExitCode(1) - ->expectsOutput('Not enough arguments (missing: "id").'); + ->expectsOutput('Not enough arguments (missing: "id").') + ->assertExitCode(1); }); it('handles non-existent analytic gracefully', function (): void { @@ -30,9 +30,9 @@ ->expects('delete') ->with(26) ->once() - ->andReturn('Analytic not found or already deleted.'); + ->andReturn(0); $this->artisan('pan:delete', ['id' => 26]) - ->assertExitCode(0) - ->expectsOutput('Analytic not found or already deleted.'); + ->expectsOutput('Record not found or already deleted.') + ->assertExitCode(0); }); From dc94c819c5064361f7e053ecf958e613581764cc Mon Sep 17 00:00:00 2001 From: Ruchit Patel Date: Fri, 18 Oct 2024 10:17:07 +0530 Subject: [PATCH 12/12] Add missing final keyword. --- src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php index d206b89..1b4f0d6 100644 --- a/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php +++ b/src/Adapters/Laravel/Console/Commands/PanDeleteCommand.php @@ -7,7 +7,7 @@ use Illuminate\Console\Command; use Pan\Contracts\AnalyticsRepository; -class PanDeleteCommand extends Command +final class PanDeleteCommand extends Command { /** * The name and signature of the console command.