Skip to content
This repository was archived by the owner on Feb 14, 2026. It is now read-only.

Commit da03287

Browse files
committed
Implement resource management traits and refactor BaseTest class
1 parent b8b6354 commit da03287

File tree

8 files changed

+110
-55
lines changed

8 files changed

+110
-55
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace CodeWithDennis\FilamentTests\Concerns;
4+
5+
interface HasFilamentResources
6+
{
7+
public function getResourceClass(): ?string;
8+
9+
public function getResource();
10+
}

src/Concerns/InteractsWithResources.php

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
namespace CodeWithDennis\FilamentTests\Concerns;
44

5-
use Filament\Actions\Action;
6-
use Filament\Resources\Pages\ListRecords;
7-
use Filament\Schemas\Schema;
8-
use Filament\Tables\Columns\Column;
9-
use Filament\Tables\Table;
5+
use CodeWithDennis\FilamentTests\Concerns\Resources\InteractsWithForms;
6+
use CodeWithDennis\FilamentTests\Concerns\Resources\InteractsWithModels;
7+
use CodeWithDennis\FilamentTests\Concerns\Resources\InteractsWithTables;
108
use ReflectionClass;
119

1210
trait InteractsWithResources
1311
{
12+
use InteractsWithForms;
13+
use InteractsWithModels;
14+
use InteractsWithTables;
15+
1416
protected function getPrivateProperty(object $object, string $property): mixed
1517
{
1618
$reflection = new ReflectionClass($object);
@@ -19,53 +21,4 @@ protected function getPrivateProperty(object $object, string $property): mixed
1921

2022
return $property->getValue($object);
2123
}
22-
23-
public function getResourceClass(): ?string
24-
{
25-
return $this->resourceClass;
26-
}
27-
28-
public function getResource()
29-
{
30-
return new ($this->getResourceClass());
31-
}
32-
33-
public function getResourceModel(): ?string
34-
{
35-
return $this->getResource()->getModel();
36-
}
37-
38-
public function getResourceForm()
39-
{
40-
return $this->getResource()->form(new Schema);
41-
}
42-
43-
public function getResourceTable()
44-
{
45-
return $this->getResource()->table(new Table(
46-
app('livewire')->new(ListRecords::class)
47-
));
48-
}
49-
50-
public function getResourceTableColumns(): array
51-
{
52-
return $this->getResourceTable()->getColumns();
53-
}
54-
55-
public function getResourceTableActions(): array
56-
{
57-
return $this->getResourceTable()->getActions();
58-
}
59-
60-
public function getResourceTableVisibleActions(): array
61-
{
62-
return array_filter($this->getResourceTableActions(), function (Action $action) {
63-
return ! $this->getPrivateProperty($action, 'isHidden');
64-
});
65-
}
66-
67-
public function getResourceSortableTableColumns(): array
68-
{
69-
return array_filter($this->getResourceTableColumns(), fn (Column $column) => $column->isSortable());
70-
}
7124
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace CodeWithDennis\FilamentTests\Concerns\Resources;
4+
5+
use Filament\Schemas\Schema;
6+
7+
trait InteractsWithForms
8+
{
9+
public function getResourceForm()
10+
{
11+
return $this->getResource()->form(new Schema);
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace CodeWithDennis\FilamentTests\Concerns\Resources;
4+
5+
trait InteractsWithModels
6+
{
7+
public function getResourceModel(): ?string
8+
{
9+
return $this->getResource()->getModel();
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace CodeWithDennis\FilamentTests\Concerns\Resources;
4+
5+
use Filament\Actions\Action;
6+
7+
trait InteractsWithTableActions
8+
{
9+
public function getResourceTableActions(): array
10+
{
11+
return $this->getResourceTable()->getActions();
12+
}
13+
14+
public function getResourceTableVisibleActions(): array
15+
{
16+
return array_filter($this->getResourceTableActions(), function (Action $action) {
17+
return ! $this->getPrivateProperty($action, 'isHidden');
18+
});
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace CodeWithDennis\FilamentTests\Concerns\Resources;
4+
5+
use Filament\Tables\Columns\Column;
6+
7+
trait InteractsWithTableColumns
8+
{
9+
public function getResourceTableColumns(): array
10+
{
11+
return $this->getResourceTable()->getColumns();
12+
}
13+
14+
public function getResourceSortableTableColumns(): array
15+
{
16+
return array_filter($this->getResourceTableColumns(), fn (Column $column) => $column->isSortable());
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace CodeWithDennis\FilamentTests\Concerns\Resources;
4+
5+
use Filament\Resources\Pages\ListRecords;
6+
use Filament\Tables\Table;
7+
8+
trait InteractsWithTables
9+
{
10+
use InteractsWithTableActions;
11+
use InteractsWithTableColumns;
12+
13+
public function getResourceTable()
14+
{
15+
return $this->getResource()->table(new Table(
16+
app('livewire')->new(ListRecords::class)
17+
));
18+
}
19+
}

src/TestRenderers/BaseTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace CodeWithDennis\FilamentTests\TestRenderers;
44

55
use CodeWithDennis\FilamentTests\Concerns\ExposesPublicMethodsToViews;
6+
use CodeWithDennis\FilamentTests\Concerns\HasFilamentResources;
67
use CodeWithDennis\FilamentTests\Concerns\InteractsWithResources;
78

8-
abstract class BaseTest
9+
abstract class BaseTest implements HasFilamentResources
910
{
1011
use ExposesPublicMethodsToViews;
1112
use InteractsWithResources;
@@ -21,6 +22,16 @@ public static function build(string $resourceClass): static
2122
return new static($resourceClass);
2223
}
2324

25+
public function getResourceClass(): ?string
26+
{
27+
return $this->resourceClass;
28+
}
29+
30+
public function getResource()
31+
{
32+
return new ($this->getResourceClass());
33+
}
34+
2435
public function view(string $view): static
2536
{
2637
$this->view = $view;

0 commit comments

Comments
 (0)