Skip to content

Commit

Permalink
Merge pull request #95 from dissto/add-description-test
Browse files Browse the repository at this point in the history
Add test for column descriptions
  • Loading branch information
CodeWithDennis authored Apr 3, 2024
2 parents 765949a + d878b75 commit 75448d9
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions src/Commands/FilamentResourceTestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions stubs/DescriptionAbove.stub
Original file line number Diff line number Diff line change
@@ -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');
6 changes: 6 additions & 0 deletions stubs/DescriptionBelow.stub
Original file line number Diff line number Diff line change
@@ -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');

0 comments on commit 75448d9

Please sign in to comment.