|
2 | 2 |
|
3 | 3 | namespace CodeWithDennis\FilamentTests\Commands; |
4 | 4 |
|
5 | | -use App\Filament\Resources\Users\UserResource; |
| 5 | +use CodeWithDennis\FilamentTests\Concerns\Commands\InteractsWithFilesystem; |
| 6 | +use CodeWithDennis\FilamentTests\Concerns\Commands\InteractsWithUserInput; |
| 7 | +use CodeWithDennis\FilamentTests\TestRenderers\BaseTest; |
6 | 8 | use CodeWithDennis\FilamentTests\TestRenderers\BeforeEach; |
7 | | -use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Create\CanRenderCreatePageTest; |
8 | | -use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Edit\CanRenderEditPageTest; |
9 | 9 | use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\CanRenderIndexPageTest; |
10 | | -use Filament\Facades\Filament; |
11 | 10 | use Illuminate\Console\Command; |
12 | | -use Illuminate\Filesystem\Filesystem; |
13 | | -use Illuminate\Support\Collection; |
14 | | -use Illuminate\Support\Facades\Process; |
15 | | - |
16 | | -use function Laravel\Prompts\multiselect; |
17 | 11 |
|
18 | 12 | class FilamentTestsCommand extends Command |
19 | 13 | { |
20 | | - protected $signature = 'make:filament-test |
21 | | - {--skip-pint : Skip running Pint on generated files}'; |
22 | | - |
23 | | - protected $description = 'Create a new test for a Filament component'; |
| 14 | + use InteractsWithFilesystem; |
| 15 | + use InteractsWithUserInput; |
24 | 16 |
|
25 | | - // protected ?Collection $resources = null; |
| 17 | + protected $signature = 'make:filament-test |
| 18 | + {--skip-pint : Skip running Pint on generated files} |
| 19 | + {--force : Overwrite existing test files without confirmation}'; |
26 | 20 |
|
27 | | - public function __construct( |
28 | | - protected ?Collection $resources = null, |
29 | | - protected ?Collection $panels = null, |
30 | | - protected array $generatedFiles = [], |
31 | | - protected ?Filesystem $files = null, |
32 | | - ) { |
33 | | - $this->resources ??= collect(); |
34 | | - $this->panels ??= collect(); |
35 | | - $this->files ??= new Filesystem; |
36 | | - parent::__construct(); |
37 | | - } |
| 21 | + protected $description = 'Create tests for your Filament resources'; |
38 | 22 |
|
39 | 23 | public function handle(): void |
40 | 24 | { |
41 | | - // $this->panels = $this->askUserToSelectPanels(); |
42 | | - // $this->resources = $this->askUserToSelectWhichResourcesFromTheSelectedPanel(); |
43 | | - |
44 | | - $this->panels = collect(['admin']); |
45 | | - $this->resources = collect([ |
46 | | - 'admin' => [ |
47 | | - UserResource::class, |
48 | | - ], |
49 | | - ]); |
50 | | - |
51 | | - foreach ($this->resources as $resourceClasses) { |
52 | | - foreach ($resourceClasses as $resourceClass) { |
53 | | - $rendered = $this->renderTestsForResource($resourceClass); |
54 | | - |
55 | | - $filePath = $this->getTestFilePath($resourceClass); |
56 | | - $this->files->ensureDirectoryExists(dirname($filePath)); |
57 | | - |
58 | | - file_put_contents($filePath, $rendered); |
| 25 | + $this->panels = $this->askUserToSelectPanels(); |
| 26 | + $this->resources = $this->askUserToSelectResourcesFromTheSelectedPanels(); |
59 | 27 |
|
60 | | - $this->generatedFiles[] = $filePath; |
| 28 | + $this->generateTests(); |
61 | 29 |
|
62 | | - $this->info("Created test for {$resourceClass} → {$filePath}"); |
63 | | - } |
64 | | - } |
| 30 | + $this->showGenerationSummary(); |
65 | 31 |
|
66 | | - $this->runPintOnGeneratedFiles(); |
| 32 | + $this->runPintOnGeneratedTests(); |
67 | 33 | } |
68 | 34 |
|
69 | 35 | /** |
70 | | - * Render all tests for a single resource. |
| 36 | + * @return class-string<BaseTest>[] |
71 | 37 | */ |
72 | | - protected function renderTestsForResource(string $resourceClass): string |
| 38 | + protected function getRenderers(): array |
73 | 39 | { |
74 | | - return implode("\n\n", [ |
75 | | - '<?php', |
76 | | - BeforeEach::build($resourceClass)->render(), |
77 | | - CanRenderIndexPageTest::build($resourceClass)->render(), |
78 | | - // CanRenderCreatePageTest::build($resourceClass)->render(), |
79 | | - // CanRenderEditPageTest::build($resourceClass)->render(), |
80 | | - ]); |
81 | | - } |
82 | | - |
83 | | - protected function runPintOnGeneratedFiles(): void |
84 | | - { |
85 | | - if ($this->generatedFiles === []) { |
86 | | - return; |
87 | | - } |
88 | | - |
89 | | - if ($this->option('skip-pint')) { |
90 | | - return; |
91 | | - } |
92 | | - |
93 | | - $files = implode(' ', $this->generatedFiles); |
94 | | - |
95 | | - Process::run("vendor/bin/pint {$files}"); |
96 | | - } |
97 | | - |
98 | | - protected function getTestFilePath(string $resourceClass): string |
99 | | - { |
100 | | - $relativeClass = str($resourceClass) |
101 | | - ->replaceFirst('App\\', '') |
102 | | - ->replace('\\', '/'); |
103 | | - |
104 | | - return base_path("tests/Feature/{$relativeClass}Test.php"); |
105 | | - } |
106 | | - |
107 | | - protected function askUserToSelectPanels(): Collection |
108 | | - { |
109 | | - $allPanels = collect(Filament::getPanels()); |
110 | | - |
111 | | - $selectedPanelIds = multiselect( |
112 | | - label: 'Which Filament panel do you want to use?', |
113 | | - options: $allPanels->mapWithKeys(fn ($panel) => [ |
114 | | - $panel->getId() => $panel->getId(), |
115 | | - ])->toArray(), |
116 | | - ); |
117 | | - |
118 | | - return collect($selectedPanelIds); |
119 | | - } |
120 | | - |
121 | | - public function askUserToSelectWhichResourcesFromTheSelectedPanel(): Collection |
122 | | - { |
123 | | - $resourcesByPanel = $this->panels->mapWithKeys(fn ($panelId) => [ |
124 | | - $panelId => collect(Filament::getPanel($panelId)?->getResources() ?? []) |
125 | | - ->mapWithKeys(fn ($resource) => [$resource => class_basename($resource)]) |
126 | | - ->toArray(), |
127 | | - ]); |
128 | | - |
129 | | - $selectedResources = collect(); |
130 | | - |
131 | | - foreach ($this->panels as $panelId) { |
132 | | - $resources = $resourcesByPanel[$panelId] ?? []; |
133 | | - |
134 | | - if (empty($resources)) { |
135 | | - continue; |
136 | | - } |
137 | | - |
138 | | - $selected = multiselect( |
139 | | - label: "Select resources for panel: {$panelId}", |
140 | | - options: $resources, |
141 | | - required: true, |
142 | | - ); |
143 | | - |
144 | | - if ($selected !== []) { |
145 | | - $selectedResources[$panelId] = $selected; |
146 | | - } |
147 | | - } |
148 | | - |
149 | | - return $selectedResources; |
| 40 | + return [ |
| 41 | + BeforeEach::class, |
| 42 | + CanRenderIndexPageTest::class, |
| 43 | + // CanRenderCreatePageTest::class, |
| 44 | + // CanRenderEditPageTest::class, |
| 45 | + ]; |
150 | 46 | } |
151 | 47 | } |
0 commit comments