generated from spatie/package-skeleton-laravel
-
Couldn't load subscription status.
- Fork 13
Improve current command #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d7d6cdb
Add HasGenerationSummary trait with showGenerationSummary method
dissto cfb55f4
Add InteractsWithFilesystem trait for test file generation and manage…
dissto 24c84bd
Add RendersFilamentTests trait for rendering resource tests
dissto e058863
Add InteractsWithUserInput trait for user panel and resource selection
dissto 79944bb
Refactor FilamentTestsCommand to enhance user interaction and test ge…
dissto acda6bb
Enhance HasGenerationSummary trait to display generated test files in…
dissto 12632d5
Update HasGenerationSummary trait to use warning for no generated tes…
dissto 51bcd0c
Refactor HasGenerationSummary to improve test file display and count
dissto 24e57e5
Refactor InteractsWithFilesystem to improve file handling and test ge…
dissto 7f81655
Refactor RendersFilamentTests to return structured output with test c…
dissto 0759c47
Refactor HasGenerationSummary to streamline generated files output st…
dissto 89a161f
Add --force option to FilamentTestsCommand for overwriting existing t…
dissto 4f9e6a5
Refactor HasGenerationSummary to use panel names instead of IDs in ge…
dissto 3b528e9
Use the getter method instead
dissto 3e53874
Fix test count calculation in HasGenerationSummary and InteractsWithF…
dissto 60f2463
Add generation summary display to InteractsWithFilesystem trait
dissto 8e7639f
Add getRenderers method to FilamentTestsCommand for test renderer man…
dissto 3ec33f0
Add renderTestsForResource method to InteractsWithFilesystem trait fo…
dissto c2a83ff
Refactor type hints and improve method signatures in various classes
dissto 2bc5cf1
Pint
dissto a4d36df
Add return type hint to getResourceTable method in InteractsWithTable…
dissto 9fac937
Fix using deprecated `->getActions()` call
dissto 6541818
Annotation no longer needed with the correct return type from the int…
dissto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| namespace CodeWithDennis\FilamentTests\Concerns\Commands; | ||
|
|
||
| use function Laravel\Prompts\table; | ||
| use function Laravel\Prompts\warning; | ||
|
|
||
| trait HasGenerationSummary | ||
| { | ||
| protected function showGenerationSummary(): void | ||
| { | ||
| if (blank($this->getGeneratedFiles())) { | ||
| warning('No test files were generated.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $rows = collect($this->getGeneratedFiles()) | ||
| ->flatMap(fn ($resources, $panelName) => collect($resources) | ||
| ->map(fn ($data, $resource): array => [ | ||
| $resource, | ||
| $panelName, | ||
| $data['num_tests'] ?? 0, | ||
| ]) | ||
| ) | ||
| ->values() | ||
| ->all(); | ||
|
|
||
| table( | ||
| ['Resource', 'Panel', '# Tests'], | ||
| $rows | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| namespace CodeWithDennis\FilamentTests\Concerns\Commands; | ||
|
|
||
| use Illuminate\Support\Facades\File; | ||
| use Illuminate\Support\Facades\Process; | ||
|
|
||
| use function Laravel\Prompts\confirm; | ||
| use function Laravel\Prompts\info; | ||
|
|
||
| trait InteractsWithFilesystem | ||
| { | ||
| use RendersFilamentTests; | ||
|
|
||
| protected array $generatedFiles = []; | ||
|
|
||
| protected function getGeneratedFiles(): array | ||
| { | ||
| return $this->generatedFiles; | ||
| } | ||
|
|
||
| protected function runPintOnGeneratedTests(): void | ||
| { | ||
| if (empty($this->getGeneratedFiles()) || $this->option('skip-pint')) { | ||
| return; | ||
| } | ||
|
|
||
| $files = collect($this->getGeneratedFiles()) | ||
| ->map(fn ($resources) => collect($resources)->pluck('path')) | ||
| ->flatten() | ||
| ->implode(' '); | ||
|
|
||
| Process::run("vendor/bin/pint {$files}"); | ||
| } | ||
|
|
||
| protected function getTestFilePath(string $resourceClass): string | ||
| { | ||
| $relativeClass = str($resourceClass) | ||
| ->replaceFirst('App\\', '') | ||
| ->replace('\\', '/'); | ||
|
|
||
| return base_path("tests/Feature/{$relativeClass}Test.php"); | ||
| } | ||
|
|
||
| protected function generateTestsForSelectedResource(string $resource, ?string $panel = null): void | ||
| { | ||
| $filePath = $this->getTestFilePath($resource); | ||
| $force = (bool) $this->option('force'); | ||
|
|
||
| if (File::exists($filePath) && ! $force && ! confirm("The tests for {$resource} already exists. Do you want to overwrite it?", false)) { | ||
| info("Skipped generating test for {$resource}."); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $renderResult = $this->renderTestsForResource($resource); | ||
|
|
||
| File::ensureDirectoryExists(dirname((string) $filePath)); | ||
| File::put($filePath, $renderResult['content']); | ||
|
|
||
| $panelKey = $panel ?? 'default'; | ||
|
|
||
| $this->generatedFiles[$panelKey][$resource] = [ | ||
| 'path' => $filePath, | ||
| 'num_tests' => (int) $renderResult['num_tests'] - 1, // -1 for the BeforeEach | ||
| ]; | ||
| } | ||
|
|
||
| protected function generateTests(): void | ||
| { | ||
| collect($this->getSelectedResources()) | ||
| ->each(function ($resources, $panelId): void { | ||
| collect($resources) | ||
| ->flatten() | ||
| ->each(fn (string $resourceClass) => $this->generateTestsForSelectedResource($resourceClass, $panelId)); | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| namespace CodeWithDennis\FilamentTests\Concerns\Commands; | ||
|
|
||
| use Filament\Facades\Filament; | ||
| use Filament\Panel; | ||
| use Illuminate\Support\Collection; | ||
|
|
||
| use function Laravel\Prompts\multiselect; | ||
|
|
||
| trait InteractsWithUserInput | ||
| { | ||
| protected Collection $panels; | ||
|
|
||
| protected Collection $resources; | ||
|
|
||
| protected function getSelectedPanels(): Collection | ||
| { | ||
| return $this->panels ??= collect(); | ||
| } | ||
|
|
||
| protected function getSelectedResources(): Collection | ||
| { | ||
| return $this->resources ??= collect(); | ||
| } | ||
|
|
||
| protected function askUserToSelectPanels(): Collection | ||
| { | ||
| $allPanels = collect(Filament::getPanels()); | ||
|
|
||
| if ($allPanels->count() === 1) { | ||
| return collect([$allPanels->first()->getId()]); | ||
| } | ||
|
|
||
| $options = $allPanels->mapWithKeys(fn (Panel $panel) => [$panel->getId() => $panel->getId()])->toArray(); | ||
|
|
||
| return collect(multiselect( | ||
| label: 'Which Filament panel/s do you want to generate tests for?', | ||
| options: $options, | ||
| required: true, | ||
| hint: 'You can select multiple panels', | ||
| )); | ||
| } | ||
|
|
||
| protected function askUserToSelectResourcesFromTheSelectedPanels(): Collection | ||
| { | ||
| $selectedResources = collect(); | ||
|
|
||
| foreach ($this->getSelectedPanels() as $panelId) { | ||
| $resources = collect(Filament::getPanel($panelId)?->getResources() ?? []) | ||
| ->mapWithKeys(fn ($resource) => [$resource => class_basename($resource)]) | ||
| ->toArray(); | ||
|
|
||
| if (empty($resources)) { | ||
| continue; | ||
| } | ||
|
|
||
| $selected = multiselect( | ||
| label: "Select resources for panel: {$panelId}", | ||
| options: $resources, | ||
| required: true, | ||
| ); | ||
|
|
||
| if ($selected !== []) { | ||
| $selectedResources[$panelId] = $selected; | ||
| } | ||
| } | ||
|
|
||
| return $selectedResources; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| namespace CodeWithDennis\FilamentTests\Concerns\Commands; | ||
|
|
||
| use CodeWithDennis\FilamentTests\TestRenderers\BaseTest; | ||
| use CodeWithDennis\FilamentTests\TestRenderers\BeforeEach; | ||
| use CodeWithDennis\FilamentTests\TestRenderers\Resources\Pages\Index\CanRenderIndexPageTest; | ||
|
|
||
| trait RendersFilamentTests | ||
| { | ||
| protected function renderTestsForResource(string $resource): array | ||
| { | ||
| $renderers = collect($this->getRenderers()); | ||
|
|
||
| $output = $renderers | ||
| ->map(fn (string $renderer) => | ||
| /** @var BaseTest $renderer */ | ||
| $renderer::build($resource)->render()) | ||
| ->prepend('<?php') | ||
| ->implode("\n\n"); | ||
|
|
||
| return [ | ||
| 'content' => $output, | ||
| 'num_tests' => $renderers->count(), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @return class-string<BaseTest>[] | ||
| */ | ||
| protected function getRenderers(): array | ||
| { | ||
| return [ | ||
| BeforeEach::class, | ||
| CanRenderIndexPageTest::class, | ||
| // CanRenderCreatePageTest::class, | ||
| // CanRenderEditPageTest::class, | ||
| ]; | ||
| } | ||
dissto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.