diff --git a/README.md b/README.md index 1de5763b..a7b22c4b 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,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 diff --git a/src/Commands/FilamentResourceTestsCommand.php b/src/Commands/FilamentResourceTestsCommand.php index 5c1db66c..171b8344 100644 --- a/src/Commands/FilamentResourceTestsCommand.php +++ b/src/Commands/FilamentResourceTestsCommand.php @@ -138,6 +138,36 @@ 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 getTableColumnDescriptionAbove(Resource $resource): array + { + 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(); + } + protected function hasSoftDeletes(Resource $resource): bool { return method_exists($resource->getModel(), 'bootSoftDeletes'); @@ -202,6 +232,16 @@ protected function getStubs(Resource $resource): array $stubs[] = 'IndividuallySearchColumn'; } + // 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 if ($this->hasSoftDeletes($resource) && $this->getTableColumns($resource)->isNotEmpty()) { $stubs[] = 'Trashed'; @@ -360,6 +400,23 @@ 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 +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_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/DescriptionAbove.stub b/stubs/DescriptionAbove.stub new file mode 100644 index 00000000..b3da9ef6 --- /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 }})->group('table', 'rendering'); diff --git a/stubs/DescriptionBelow.stub b/stubs/DescriptionBelow.stub new file mode 100644 index 00000000..b463f77d --- /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 }})->group('table', 'rendering');