From ac5e78198876c4ed481379adabebf54ce263cdab Mon Sep 17 00:00:00 2001 From: "M. D" Date: Fri, 29 Mar 2024 11:18:32 +0100 Subject: [PATCH 1/7] Add tests for the default delete actions (bulk and regular) --- src/Commands/FilamentResourceTestsCommand.php | 42 +++++++++++++++++++ stubs/BulkDeleting.stub | 12 ++++++ stubs/BulkDeletingSoftDeletes.stub | 12 ++++++ stubs/Deleting.stub | 8 ++++ stubs/DeletingSoftDeletes.stub | 8 ++++ 5 files changed, 82 insertions(+) create mode 100644 stubs/BulkDeleting.stub create mode 100644 stubs/BulkDeletingSoftDeletes.stub create mode 100644 stubs/Deleting.stub create mode 100644 stubs/DeletingSoftDeletes.stub diff --git a/src/Commands/FilamentResourceTestsCommand.php b/src/Commands/FilamentResourceTestsCommand.php index 312330c1..590f4c80 100644 --- a/src/Commands/FilamentResourceTestsCommand.php +++ b/src/Commands/FilamentResourceTestsCommand.php @@ -132,6 +132,26 @@ protected function hasSoftDeletes(Resource $resource): bool return method_exists($resource->getModel(), 'bootSoftDeletes'); } + protected function getResourceTableActions(Resource $resource): Collection + { + return collect($this->getResourceTable($resource)->getFlatActions()); + } + + protected function getResourceTableActionNames(Resource $resource): Collection + { + return $this->getResourceTableActions($resource)->map(fn ($action) => $action->getName()); + } + + protected function getResourceTableBulkActions(Resource $resource): Collection + { + return collect($this->getResourceTable($resource)->getFlatBulkActions()); + } + + protected function getResourceTableBulkActionNames(Resource $resource): Collection + { + return $this->getResourceTableBulkActions($resource)->map(fn ($action) => $action->getName()); + } + protected function getStubs(Resource $resource): array { // Base stubs that are always included @@ -166,6 +186,28 @@ protected function getStubs(Resource $resource): array $stubs[] = 'Trashed'; } + // Delete Action + if ($this->getResourceTableActionNames($resource)->contains('delete') && $this->getTableColumns($resource)->isNotEmpty()) { + + if (! $this->hasSoftDeletes($resource)) { + $stubs[] = 'Deleting'; + } else { + $stubs[] = 'DeletingSoftDeletes'; + } + + } + + // Bulk Delete Action + if ($this->getResourceTableBulkActionNames($resource)->contains('delete') && $this->getTableColumns($resource)->isNotEmpty()) { + + if (! $this->hasSoftDeletes($resource)) { + $stubs[] = 'BulkDeleting'; + } else { + $stubs[] = 'BulkDeletingSoftDeletes'; + } + + } + // Return the stubs return $stubs; } diff --git a/stubs/BulkDeleting.stub b/stubs/BulkDeleting.stub new file mode 100644 index 00000000..14d46380 --- /dev/null +++ b/stubs/BulkDeleting.stub @@ -0,0 +1,12 @@ +it('can bulk delete records', function () { + $records = $MODEL_SINGULAR_NAME$::factory()->count(3)->create(); + + livewire(List$MODEL_PLURAL_NAME$::class) + ->callTableBulkAction(\Filament\Tables\Actions\DeleteBulkAction::class, $records); + + foreach ($records as $record) { + $this->assertModelMissing($record); + } + + expect($MODEL_SINGULAR_NAME$::count())->toBe(0); +}); diff --git a/stubs/BulkDeletingSoftDeletes.stub b/stubs/BulkDeletingSoftDeletes.stub new file mode 100644 index 00000000..61fdde6c --- /dev/null +++ b/stubs/BulkDeletingSoftDeletes.stub @@ -0,0 +1,12 @@ +it('can bulk soft delete records', function () { + $records = $MODEL_SINGULAR_NAME$::factory()->count(3)->create(); + + livewire(List$MODEL_PLURAL_NAME$::class) + ->callTableBulkAction(\Filament\Tables\Actions\DeleteBulkAction::class, $records); + + foreach ($records as $record) { + $this->assertSoftDeleted($record); + } + + expect($MODEL_SINGULAR_NAME$::count())->toBe(0); +}); diff --git a/stubs/Deleting.stub b/stubs/Deleting.stub new file mode 100644 index 00000000..d3c72a41 --- /dev/null +++ b/stubs/Deleting.stub @@ -0,0 +1,8 @@ +it('can delete records', function () { + $record = $MODEL_SINGULAR_NAME$::factory()->create(); + + livewire(List$MODEL_PLURAL_NAME$::class) + ->callTableAction(\Filament\Tables\Actions\DeleteAction::class, $record); + + $this->assertModelMissing($record); +}); diff --git a/stubs/DeletingSoftDeletes.stub b/stubs/DeletingSoftDeletes.stub new file mode 100644 index 00000000..1bfb319b --- /dev/null +++ b/stubs/DeletingSoftDeletes.stub @@ -0,0 +1,8 @@ +it('can soft delete records', function () { + $record = $MODEL_SINGULAR_NAME$::factory()->create(); + + livewire(List$MODEL_PLURAL_NAME$::class) + ->callTableAction(\Filament\Tables\Actions\DeleteAction::class, $record); + + $this->assertSoftDeleted($record); +}); From 2eba0496cde10d22c09fac2b901dee0cca97a5af Mon Sep 17 00:00:00 2001 From: "M. D" Date: Fri, 29 Mar 2024 12:24:42 +0100 Subject: [PATCH 2/7] Remove check for columns --- src/Commands/FilamentResourceTestsCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Commands/FilamentResourceTestsCommand.php b/src/Commands/FilamentResourceTestsCommand.php index 590f4c80..08f3b4e3 100644 --- a/src/Commands/FilamentResourceTestsCommand.php +++ b/src/Commands/FilamentResourceTestsCommand.php @@ -187,7 +187,7 @@ protected function getStubs(Resource $resource): array } // Delete Action - if ($this->getResourceTableActionNames($resource)->contains('delete') && $this->getTableColumns($resource)->isNotEmpty()) { + if ($this->getResourceTableActionNames($resource)->contains('delete')) { if (! $this->hasSoftDeletes($resource)) { $stubs[] = 'Deleting'; @@ -198,7 +198,7 @@ protected function getStubs(Resource $resource): array } // Bulk Delete Action - if ($this->getResourceTableBulkActionNames($resource)->contains('delete') && $this->getTableColumns($resource)->isNotEmpty()) { + if ($this->getResourceTableBulkActionNames($resource)->contains('delete')) { if (! $this->hasSoftDeletes($resource)) { $stubs[] = 'BulkDeleting'; From 476c375bda72b48e9e2600ed9782bb18ddfd2509 Mon Sep 17 00:00:00 2001 From: "M. D" Date: Fri, 29 Mar 2024 12:37:28 +0100 Subject: [PATCH 3/7] Shorten stub determination --- src/Commands/FilamentResourceTestsCommand.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Commands/FilamentResourceTestsCommand.php b/src/Commands/FilamentResourceTestsCommand.php index 08f3b4e3..c64ca747 100644 --- a/src/Commands/FilamentResourceTestsCommand.php +++ b/src/Commands/FilamentResourceTestsCommand.php @@ -188,24 +188,12 @@ protected function getStubs(Resource $resource): array // Delete Action if ($this->getResourceTableActionNames($resource)->contains('delete')) { - - if (! $this->hasSoftDeletes($resource)) { - $stubs[] = 'Deleting'; - } else { - $stubs[] = 'DeletingSoftDeletes'; - } - + $stubs[] = ! $this->hasSoftDeletes($resource) ? 'Deleting' : 'DeletingSoftDeletes'; } // Bulk Delete Action if ($this->getResourceTableBulkActionNames($resource)->contains('delete')) { - - if (! $this->hasSoftDeletes($resource)) { - $stubs[] = 'BulkDeleting'; - } else { - $stubs[] = 'BulkDeletingSoftDeletes'; - } - + $stubs[] = ! $this->hasSoftDeletes($resource) ? 'BulkDeleting' : 'BulkDeletingSoftDeletes'; } // Return the stubs From e30f79cf305348291533c4e6902d1b3fe79a578e Mon Sep 17 00:00:00 2001 From: Dennis Elsinga Date: Fri, 29 Mar 2024 12:40:30 +0100 Subject: [PATCH 4/7] refactor: Remove unnecessary code for getting resource table columns --- src/Commands/FilamentResourceTestsCommand.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Commands/FilamentResourceTestsCommand.php b/src/Commands/FilamentResourceTestsCommand.php index c64ca747..6d80aadf 100644 --- a/src/Commands/FilamentResourceTestsCommand.php +++ b/src/Commands/FilamentResourceTestsCommand.php @@ -157,9 +157,6 @@ protected function getStubs(Resource $resource): array // Base stubs that are always included $stubs = ['Base', 'RenderPage']; - // Get the columns of the resource table - $columns = collect($this->getResourceTable($resource)->getColumns()); - // Add additional stubs based on the columns if ($this->getTableColumns($resource)->isNotEmpty()) { $stubs[] = 'HasColumn'; @@ -186,12 +183,12 @@ protected function getStubs(Resource $resource): array $stubs[] = 'Trashed'; } - // Delete Action + // Check if there is a delete action if ($this->getResourceTableActionNames($resource)->contains('delete')) { $stubs[] = ! $this->hasSoftDeletes($resource) ? 'Deleting' : 'DeletingSoftDeletes'; } - // Bulk Delete Action + // Check if there is a bulk delete action if ($this->getResourceTableBulkActionNames($resource)->contains('delete')) { $stubs[] = ! $this->hasSoftDeletes($resource) ? 'BulkDeleting' : 'BulkDeletingSoftDeletes'; } From b862211406de530982d5a3375c957be0a5b362f6 Mon Sep 17 00:00:00 2001 From: "M. D" Date: Fri, 29 Mar 2024 12:44:46 +0100 Subject: [PATCH 5/7] Fix intendentation --- stubs/BulkDeleting.stub | 12 ++++++------ stubs/BulkDeletingSoftDeletes.stub | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/stubs/BulkDeleting.stub b/stubs/BulkDeleting.stub index 14d46380..b293fb50 100644 --- a/stubs/BulkDeleting.stub +++ b/stubs/BulkDeleting.stub @@ -1,12 +1,12 @@ it('can bulk delete records', function () { $records = $MODEL_SINGULAR_NAME$::factory()->count(3)->create(); - livewire(List$MODEL_PLURAL_NAME$::class) - ->callTableBulkAction(\Filament\Tables\Actions\DeleteBulkAction::class, $records); + livewire(List$MODEL_PLURAL_NAME$::class) + ->callTableBulkAction(\Filament\Tables\Actions\DeleteBulkAction::class, $records); - foreach ($records as $record) { - $this->assertModelMissing($record); - } + foreach ($records as $record) { + $this->assertModelMissing($record); + } - expect($MODEL_SINGULAR_NAME$::count())->toBe(0); + expect($MODEL_SINGULAR_NAME$::count())->toBe(0); }); diff --git a/stubs/BulkDeletingSoftDeletes.stub b/stubs/BulkDeletingSoftDeletes.stub index 61fdde6c..f8e9d284 100644 --- a/stubs/BulkDeletingSoftDeletes.stub +++ b/stubs/BulkDeletingSoftDeletes.stub @@ -1,12 +1,12 @@ it('can bulk soft delete records', function () { $records = $MODEL_SINGULAR_NAME$::factory()->count(3)->create(); - livewire(List$MODEL_PLURAL_NAME$::class) - ->callTableBulkAction(\Filament\Tables\Actions\DeleteBulkAction::class, $records); + livewire(List$MODEL_PLURAL_NAME$::class) + ->callTableBulkAction(\Filament\Tables\Actions\DeleteBulkAction::class, $records); - foreach ($records as $record) { - $this->assertSoftDeleted($record); - } + foreach ($records as $record) { + $this->assertSoftDeleted($record); + } - expect($MODEL_SINGULAR_NAME$::count())->toBe(0); + expect($MODEL_SINGULAR_NAME$::count())->toBe(0); }); From 9f82dc1cb640596a72565e25f2a119cedba2cb3e Mon Sep 17 00:00:00 2001 From: "M. D" Date: Fri, 29 Mar 2024 12:57:12 +0100 Subject: [PATCH 6/7] Fix count expectation --- stubs/BulkDeleting.stub | 2 +- stubs/BulkDeletingSoftDeletes.stub | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stubs/BulkDeleting.stub b/stubs/BulkDeleting.stub index b293fb50..db285210 100644 --- a/stubs/BulkDeleting.stub +++ b/stubs/BulkDeleting.stub @@ -8,5 +8,5 @@ it('can bulk delete records', function () { $this->assertModelMissing($record); } - expect($MODEL_SINGULAR_NAME$::count())->toBe(0); + expect($MODEL_SINGULAR_NAME$::find($records->pluck('id')))->toBeEmpty(); }); diff --git a/stubs/BulkDeletingSoftDeletes.stub b/stubs/BulkDeletingSoftDeletes.stub index f8e9d284..17bb7a40 100644 --- a/stubs/BulkDeletingSoftDeletes.stub +++ b/stubs/BulkDeletingSoftDeletes.stub @@ -8,5 +8,5 @@ it('can bulk soft delete records', function () { $this->assertSoftDeleted($record); } - expect($MODEL_SINGULAR_NAME$::count())->toBe(0); + expect($MODEL_SINGULAR_NAME$::find($records->pluck('id')))->toBeEmpty(); }); From fae239c191aedbe0a0b83492099e6e314c32bfbc Mon Sep 17 00:00:00 2001 From: "M. D" Date: Fri, 29 Mar 2024 13:03:13 +0100 Subject: [PATCH 7/7] Add `DeleteAction` and `DeleteBulkAction` to base --- stubs/Base.stub | 2 ++ stubs/BulkDeleting.stub | 2 +- stubs/BulkDeletingSoftDeletes.stub | 2 +- stubs/Deleting.stub | 2 +- stubs/DeletingSoftDeletes.stub | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/stubs/Base.stub b/stubs/Base.stub index 4435aac5..ec304f9a 100644 --- a/stubs/Base.stub +++ b/stubs/Base.stub @@ -1,6 +1,8 @@ count(3)->create(); livewire(List$MODEL_PLURAL_NAME$::class) - ->callTableBulkAction(\Filament\Tables\Actions\DeleteBulkAction::class, $records); + ->callTableBulkAction(DeleteBulkAction::class, $records); foreach ($records as $record) { $this->assertModelMissing($record); diff --git a/stubs/BulkDeletingSoftDeletes.stub b/stubs/BulkDeletingSoftDeletes.stub index 17bb7a40..85f2c8e9 100644 --- a/stubs/BulkDeletingSoftDeletes.stub +++ b/stubs/BulkDeletingSoftDeletes.stub @@ -2,7 +2,7 @@ it('can bulk soft delete records', function () { $records = $MODEL_SINGULAR_NAME$::factory()->count(3)->create(); livewire(List$MODEL_PLURAL_NAME$::class) - ->callTableBulkAction(\Filament\Tables\Actions\DeleteBulkAction::class, $records); + ->callTableBulkAction(DeleteBulkAction::class, $records); foreach ($records as $record) { $this->assertSoftDeleted($record); diff --git a/stubs/Deleting.stub b/stubs/Deleting.stub index d3c72a41..61daa7ec 100644 --- a/stubs/Deleting.stub +++ b/stubs/Deleting.stub @@ -2,7 +2,7 @@ it('can delete records', function () { $record = $MODEL_SINGULAR_NAME$::factory()->create(); livewire(List$MODEL_PLURAL_NAME$::class) - ->callTableAction(\Filament\Tables\Actions\DeleteAction::class, $record); + ->callTableAction(DeleteAction::class, $record); $this->assertModelMissing($record); }); diff --git a/stubs/DeletingSoftDeletes.stub b/stubs/DeletingSoftDeletes.stub index 1bfb319b..2efb95a7 100644 --- a/stubs/DeletingSoftDeletes.stub +++ b/stubs/DeletingSoftDeletes.stub @@ -2,7 +2,7 @@ it('can soft delete records', function () { $record = $MODEL_SINGULAR_NAME$::factory()->create(); livewire(List$MODEL_PLURAL_NAME$::class) - ->callTableAction(\Filament\Tables\Actions\DeleteAction::class, $record); + ->callTableAction(DeleteAction::class, $record); $this->assertSoftDeleted($record); });