From 91cb8ed24d10887b8e724cd20befb664029e102f Mon Sep 17 00:00:00 2001 From: "M. D" Date: Mon, 1 Apr 2024 14:20:34 +0200 Subject: [PATCH 1/8] Add test for column descriptions --- src/Commands/FilamentResourceTestsCommand.php | 49 +++++++++++++++++++ stubs/Description.stub | 8 +++ 2 files changed, 57 insertions(+) create mode 100644 stubs/Description.stub diff --git a/src/Commands/FilamentResourceTestsCommand.php b/src/Commands/FilamentResourceTestsCommand.php index d6df61da..dc75ab03 100644 --- a/src/Commands/FilamentResourceTestsCommand.php +++ b/src/Commands/FilamentResourceTestsCommand.php @@ -138,6 +138,32 @@ protected function getInitiallyVisibleColumns(Resource $resource): Collection ->filter(fn ($column) => ! $column->isToggledHiddenByDefault()); } + protected function getDescriptionAboveColumns(Resource $resource): Collection + { + return $this->getTableColumns($resource) + ->filter(fn ($column) => $column->getDescriptionAbove()); + } + + protected function getDescriptionBelowColumns(Resource $resource): Collection + { + return $this->getTableColumns($resource) + ->filter(fn ($column) => $column->getDescriptionBelow()); + } + + protected function getTableColumnDescription(Resource $resource): array + { + return $this->getTableColumns($resource) + ->filter(fn($column) => $column->getDescriptionAbove() || $column->getDescriptionBelow()) + ->mapWithKeys(fn($column) => [ + $column->getName() => [ + 'column' => $column->getName(), + 'description' => $column->getDescriptionAbove() ?? $column->getDescriptionBelow(), + 'position' => $column->getDescriptionAbove() ? 'above' : 'below', + ] + ])->toArray(); + } + + protected function hasSoftDeletes(Resource $resource): bool { return method_exists($resource->getModel(), 'bootSoftDeletes'); @@ -202,6 +228,12 @@ protected function getStubs(Resource $resource): array $stubs[] = 'IndividuallySearchColumn'; } + + if ($this->getDescriptionAboveColumns($resource)->isNotEmpty() || $this->getDescriptionBelowColumns($resource)->isNotEmpty()) { + $stubs[] = 'Description'; + } + + // Check that trashed columns are not displayed by default if ($this->hasSoftDeletes($resource) && $this->getTableColumns($resource)->isNotEmpty()) { $stubs[] = 'Trashed'; @@ -360,6 +392,22 @@ protected function convertDoubleQuotedArrayString(string $string): string ->replace(',', ', '); } + protected function transformToPestDataset(array $source, array $keys): string { + $transformed = []; + + foreach ($source as $item) { + $transformedItem = []; + foreach ($keys as $key) { + if (isset($item[$key])) { + $transformedItem[] = $item[$key]; + } + } + $transformed[] = $transformedItem; + } + + return $this->convertDoubleQuotedArrayString(json_encode($transformed, JSON_UNESCAPED_UNICODE)); + } + protected function getStubVariables(Resource $resource): array { $resourceModel = $resource->getModel(); @@ -385,6 +433,7 @@ protected function getStubVariables(Resource $resource): array 'MODEL_IMPORT' => $modelImport, 'MODEL_SINGULAR_NAME' => str($resourceModel)->afterLast('\\'), 'MODEL_PLURAL_NAME' => str($resourceModel)->afterLast('\\')->plural(), + 'RESOURCE_TABLE_COLUMNS_DESCRIPTIONS' => $this->transformToPestDataset($this->getTableColumnDescription($resource), ['column','description', 'position']), ], $converted); } diff --git a/stubs/Description.stub b/stubs/Description.stub new file mode 100644 index 00000000..848123bb --- /dev/null +++ b/stubs/Description.stub @@ -0,0 +1,8 @@ +it('has the correct descriptions above and below', function (string $column, string $description, string $position) { + + $record = $MODEL_SINGULAR_NAME$::factory()->create(); + + livewire(List$MODEL_PLURAL_NAME$::class) + ->assertTableColumnHasDescription($column, $description, $record, $position) + ->assertTableColumnDoesNotHaveDescription($column, $description, $record, ($position === 'above') ? 'below' : 'above'); +})->with($RESOURCE_TABLE_COLUMNS_DESCRIPTIONS$); From 2ac1a3cbdce883e2f63a53e8e39417095b7fd315 Mon Sep 17 00:00:00 2001 From: dissto Date: Mon, 1 Apr 2024 12:21:01 +0000 Subject: [PATCH 2/8] Pint: Fix code style issues --- src/Commands/FilamentResourceTestsCommand.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Commands/FilamentResourceTestsCommand.php b/src/Commands/FilamentResourceTestsCommand.php index dc75ab03..e08fb554 100644 --- a/src/Commands/FilamentResourceTestsCommand.php +++ b/src/Commands/FilamentResourceTestsCommand.php @@ -153,17 +153,16 @@ protected function getDescriptionBelowColumns(Resource $resource): Collection protected function getTableColumnDescription(Resource $resource): array { return $this->getTableColumns($resource) - ->filter(fn($column) => $column->getDescriptionAbove() || $column->getDescriptionBelow()) - ->mapWithKeys(fn($column) => [ + ->filter(fn ($column) => $column->getDescriptionAbove() || $column->getDescriptionBelow()) + ->mapWithKeys(fn ($column) => [ $column->getName() => [ 'column' => $column->getName(), 'description' => $column->getDescriptionAbove() ?? $column->getDescriptionBelow(), 'position' => $column->getDescriptionAbove() ? 'above' : 'below', - ] + ], ])->toArray(); } - protected function hasSoftDeletes(Resource $resource): bool { return method_exists($resource->getModel(), 'bootSoftDeletes'); @@ -228,12 +227,10 @@ protected function getStubs(Resource $resource): array $stubs[] = 'IndividuallySearchColumn'; } - if ($this->getDescriptionAboveColumns($resource)->isNotEmpty() || $this->getDescriptionBelowColumns($resource)->isNotEmpty()) { $stubs[] = 'Description'; } - // Check that trashed columns are not displayed by default if ($this->hasSoftDeletes($resource) && $this->getTableColumns($resource)->isNotEmpty()) { $stubs[] = 'Trashed'; @@ -392,7 +389,8 @@ protected function convertDoubleQuotedArrayString(string $string): string ->replace(',', ', '); } - protected function transformToPestDataset(array $source, array $keys): string { + protected function transformToPestDataset(array $source, array $keys): string + { $transformed = []; foreach ($source as $item) { @@ -433,7 +431,7 @@ protected function getStubVariables(Resource $resource): array 'MODEL_IMPORT' => $modelImport, 'MODEL_SINGULAR_NAME' => str($resourceModel)->afterLast('\\'), 'MODEL_PLURAL_NAME' => str($resourceModel)->afterLast('\\')->plural(), - 'RESOURCE_TABLE_COLUMNS_DESCRIPTIONS' => $this->transformToPestDataset($this->getTableColumnDescription($resource), ['column','description', 'position']), + 'RESOURCE_TABLE_COLUMNS_DESCRIPTIONS' => $this->transformToPestDataset($this->getTableColumnDescription($resource), ['column', 'description', 'position']), ], $converted); } From 19d000c711f231a9c79aa76d0fd95dcce7d30566 Mon Sep 17 00:00:00 2001 From: "M. D" Date: Mon, 1 Apr 2024 15:01:53 +0200 Subject: [PATCH 3/8] Refactor into individual tests/stubs --- src/Commands/FilamentResourceTestsCommand.php | 36 ++++++++++++------- stubs/Description.stub | 8 ----- stubs/DescriptionAbove.stub | 6 ++++ stubs/DescriptionBelow.stub | 6 ++++ 4 files changed, 36 insertions(+), 20 deletions(-) delete mode 100644 stubs/Description.stub create mode 100644 stubs/DescriptionAbove.stub create mode 100644 stubs/DescriptionBelow.stub diff --git a/src/Commands/FilamentResourceTestsCommand.php b/src/Commands/FilamentResourceTestsCommand.php index e08fb554..38bd0b94 100644 --- a/src/Commands/FilamentResourceTestsCommand.php +++ b/src/Commands/FilamentResourceTestsCommand.php @@ -150,16 +150,21 @@ protected function getDescriptionBelowColumns(Resource $resource): Collection ->filter(fn ($column) => $column->getDescriptionBelow()); } - protected function getTableColumnDescription(Resource $resource): array + protected function getTableColumnDescriptionAbove(Resource $resource): array { - return $this->getTableColumns($resource) - ->filter(fn ($column) => $column->getDescriptionAbove() || $column->getDescriptionBelow()) - ->mapWithKeys(fn ($column) => [ - $column->getName() => [ - 'column' => $column->getName(), - 'description' => $column->getDescriptionAbove() ?? $column->getDescriptionBelow(), - 'position' => $column->getDescriptionAbove() ? 'above' : 'below', - ], + return $this->getDescriptionAboveColumns($resource) + ->map(fn ($column) => [ + 'column' => $column->getName(), + 'description' => $column->getDescriptionAbove(), + ])->toArray(); + } + + protected function getTableColumnDescriptionBelow(Resource $resource): array + { + return $this->getDescriptionBelowColumns($resource) + ->map(fn ($column) => [ + 'column' => $column->getName(), + 'description' => $column->getDescriptionBelow(), ])->toArray(); } @@ -227,8 +232,14 @@ protected function getStubs(Resource $resource): array $stubs[] = 'IndividuallySearchColumn'; } - if ($this->getDescriptionAboveColumns($resource)->isNotEmpty() || $this->getDescriptionBelowColumns($resource)->isNotEmpty()) { - $stubs[] = 'Description'; + // Check if there is a description above + if ($this->getDescriptionAboveColumns($resource)->isNotEmpty()) { + $stubs[] = 'DescriptionAbove'; + } + + // Check if there is a description below + if ($this->getDescriptionBelowColumns($resource)->isNotEmpty()) { + $stubs[] = 'DescriptionBelow'; } // Check that trashed columns are not displayed by default @@ -431,7 +442,8 @@ protected function getStubVariables(Resource $resource): array 'MODEL_IMPORT' => $modelImport, 'MODEL_SINGULAR_NAME' => str($resourceModel)->afterLast('\\'), 'MODEL_PLURAL_NAME' => str($resourceModel)->afterLast('\\')->plural(), - 'RESOURCE_TABLE_COLUMNS_DESCRIPTIONS' => $this->transformToPestDataset($this->getTableColumnDescription($resource), ['column', 'description', 'position']), + 'RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_ABOVE' => $this->transformToPestDataset($this->getTableColumnDescriptionAbove($resource), ['column', 'description']), + 'RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_BELOW' => $this->transformToPestDataset($this->getTableColumnDescriptionBelow($resource), ['column', 'description']), ], $converted); } diff --git a/stubs/Description.stub b/stubs/Description.stub deleted file mode 100644 index 848123bb..00000000 --- a/stubs/Description.stub +++ /dev/null @@ -1,8 +0,0 @@ -it('has the correct descriptions above and below', function (string $column, string $description, string $position) { - - $record = $MODEL_SINGULAR_NAME$::factory()->create(); - - livewire(List$MODEL_PLURAL_NAME$::class) - ->assertTableColumnHasDescription($column, $description, $record, $position) - ->assertTableColumnDoesNotHaveDescription($column, $description, $record, ($position === 'above') ? 'below' : 'above'); -})->with($RESOURCE_TABLE_COLUMNS_DESCRIPTIONS$); diff --git a/stubs/DescriptionAbove.stub b/stubs/DescriptionAbove.stub new file mode 100644 index 00000000..3a3a5094 --- /dev/null +++ b/stubs/DescriptionAbove.stub @@ -0,0 +1,6 @@ +it('has the correct descriptions above', function (string $column, string $description) { + $record = $MODEL_SINGULAR_NAME$::factory()->create(); + + livewire(List$MODEL_PLURAL_NAME$::class) + ->assertTableColumnHasDescription($column, $description, $record, 'above'); +})->with($RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_ABOVE$); diff --git a/stubs/DescriptionBelow.stub b/stubs/DescriptionBelow.stub new file mode 100644 index 00000000..46fc5080 --- /dev/null +++ b/stubs/DescriptionBelow.stub @@ -0,0 +1,6 @@ +it('has the correct descriptions below', function (string $column, string $description) { + $record = $MODEL_SINGULAR_NAME$::factory()->create(); + + livewire(List$MODEL_PLURAL_NAME$::class) + ->assertTableColumnHasDescription($column, $description, $record, 'below'); +})->with($RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_BELOW$); From 4babbf5a32c3f5b749f8356dad1cb304635c9c56 Mon Sep 17 00:00:00 2001 From: "M. D" Date: Tue, 2 Apr 2024 21:01:21 +0200 Subject: [PATCH 4/8] Use new curly braces syntax --- stubs/DescriptionAbove.stub | 6 +++--- stubs/DescriptionBelow.stub | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/stubs/DescriptionAbove.stub b/stubs/DescriptionAbove.stub index 3a3a5094..f35a7f41 100644 --- a/stubs/DescriptionAbove.stub +++ b/stubs/DescriptionAbove.stub @@ -1,6 +1,6 @@ it('has the correct descriptions above', function (string $column, string $description) { - $record = $MODEL_SINGULAR_NAME$::factory()->create(); + $record = {{ MODEL_SINGULAR_NAME }}::factory()->create(); - livewire(List$MODEL_PLURAL_NAME$::class) + livewire(List{{ MODEL_PLURAL_NAME }}::class) ->assertTableColumnHasDescription($column, $description, $record, 'above'); -})->with($RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_ABOVE$); +})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_ABOVE }}); diff --git a/stubs/DescriptionBelow.stub b/stubs/DescriptionBelow.stub index 46fc5080..f1580992 100644 --- a/stubs/DescriptionBelow.stub +++ b/stubs/DescriptionBelow.stub @@ -1,6 +1,6 @@ it('has the correct descriptions below', function (string $column, string $description) { - $record = $MODEL_SINGULAR_NAME$::factory()->create(); + $record = {{ MODEL_SINGULAR_NAME }}::factory()->create(); - livewire(List$MODEL_PLURAL_NAME$::class) + livewire(List{{ MODEL_PLURAL_NAME }}::class) ->assertTableColumnHasDescription($column, $description, $record, 'below'); -})->with($RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_BELOW$); +})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_BELOW }}); From c008084c3d994eba90e425c35add96d8e4947c7d Mon Sep 17 00:00:00 2001 From: "M. D" Date: Tue, 2 Apr 2024 21:22:59 +0200 Subject: [PATCH 5/8] Add group --- stubs/DescriptionAbove.stub | 3 ++- stubs/DescriptionBelow.stub | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/stubs/DescriptionAbove.stub b/stubs/DescriptionAbove.stub index f35a7f41..df763775 100644 --- a/stubs/DescriptionAbove.stub +++ b/stubs/DescriptionAbove.stub @@ -3,4 +3,5 @@ it('has the correct descriptions above', function (string $column, string $descr livewire(List{{ MODEL_PLURAL_NAME }}::class) ->assertTableColumnHasDescription($column, $description, $record, 'above'); -})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_ABOVE }}); +})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_ABOVE }}) + ->group('table', 'rendering'); diff --git a/stubs/DescriptionBelow.stub b/stubs/DescriptionBelow.stub index f1580992..8910b5f6 100644 --- a/stubs/DescriptionBelow.stub +++ b/stubs/DescriptionBelow.stub @@ -3,4 +3,5 @@ it('has the correct descriptions below', function (string $column, string $descr livewire(List{{ MODEL_PLURAL_NAME }}::class) ->assertTableColumnHasDescription($column, $description, $record, 'below'); -})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_BELOW }}); +})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_BELOW }}) + ->group('table', 'rendering'); From cb3564f4ceea84b2a0e4b4f442a0855e48a3dbb0 Mon Sep 17 00:00:00 2001 From: "M. D" Date: Wed, 3 Apr 2024 13:44:06 +0200 Subject: [PATCH 6/8] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3a8c7b45..97d1dff2 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ won't be generated. - [x] [It can render column](https://filamentphp.com/docs/3.x/tables/testing#columns) - [x] [It can search column](https://filamentphp.com/docs/3.x/tables/testing#searching) - [x] [It has column](https://filamentphp.com/docs/3.x/tables/testing#existence) + - [x] [It has the correct descriptions](https://filamentphp.com/docs/3.x/tables/testing#descriptions) - [x] It can delete records - [x] It can soft delete records - [x] It can bulk delete records From f205f2883386380b2087b83b8bdb95bc334a3548 Mon Sep 17 00:00:00 2001 From: CodeWithDennis <23448484+CodeWithDennis@users.noreply.github.com> Date: Wed, 3 Apr 2024 13:59:57 +0200 Subject: [PATCH 7/8] Update DescriptionBelow.stub --- stubs/DescriptionBelow.stub | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stubs/DescriptionBelow.stub b/stubs/DescriptionBelow.stub index 8910b5f6..b463f77d 100644 --- a/stubs/DescriptionBelow.stub +++ b/stubs/DescriptionBelow.stub @@ -3,5 +3,4 @@ it('has the correct descriptions below', function (string $column, string $descr livewire(List{{ MODEL_PLURAL_NAME }}::class) ->assertTableColumnHasDescription($column, $description, $record, 'below'); -})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_BELOW }}) - ->group('table', 'rendering'); +})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_BELOW }})->group('table', 'rendering'); From d878b75513e855412c8e57534a1ece7a998bfc2f Mon Sep 17 00:00:00 2001 From: CodeWithDennis <23448484+CodeWithDennis@users.noreply.github.com> Date: Wed, 3 Apr 2024 14:00:09 +0200 Subject: [PATCH 8/8] Update DescriptionAbove.stub --- stubs/DescriptionAbove.stub | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stubs/DescriptionAbove.stub b/stubs/DescriptionAbove.stub index df763775..b3da9ef6 100644 --- a/stubs/DescriptionAbove.stub +++ b/stubs/DescriptionAbove.stub @@ -3,5 +3,4 @@ it('has the correct descriptions above', function (string $column, string $descr livewire(List{{ MODEL_PLURAL_NAME }}::class) ->assertTableColumnHasDescription($column, $description, $record, 'above'); -})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_ABOVE }}) - ->group('table', 'rendering'); +})->with({{ RESOURCE_TABLE_COLUMNS_DESCRIPTIONS_ABOVE }})->group('table', 'rendering');