Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/Commands/FilamentTestsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class FilamentTestsCommand extends Command

protected Collection $resources;

protected bool $tableLoadingGloballyDeferred = false;

use InteractsWithFilesystem;
use InteractsWithUserInput;

Expand All @@ -47,7 +45,6 @@ public function handle(): void
{
$this->panels = $this->askUserToSelectPanels();
$this->resources = $this->askUserToSelectResourcesFromTheSelectedPanels();
$this->tableLoadingGloballyDeferred = $this->askUserIfTableLoadingIsGloballyDeferred();

$this->generateTests();
$this->showGenerationSummary();
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/Commands/InteractsWithFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function renderTestsForResource(string $resource): array
$output = $renderers
->map(fn (string $renderer) =>
/** @var BaseTest $renderer */
$renderer::build($resource)->tableLoadingGloballyDeferred($this->tableLoadingGloballyDeferred)->render())
$renderer::build($resource)->render())
->prepend('<?php')
->implode("\n\n");

Expand Down
10 changes: 0 additions & 10 deletions src/Concerns/Commands/InteractsWithUserInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Filament\Panel;
use Illuminate\Support\Collection;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\multiselect;

trait InteractsWithUserInput
Expand Down Expand Up @@ -65,13 +64,4 @@ protected function askUserToSelectResourcesFromTheSelectedPanels(): Collection

return $selectedResources;
}

protected function askUserIfTableLoadingIsGloballyDeferred(): bool
{
return confirm(
label: 'Do you globally defer table loading in your Filament app?',
default: false,
hint: 'If you set `deferLoading` individually on your resource tables, you can select "No" here.',
);
}
}
26 changes: 26 additions & 0 deletions src/Concerns/InteractsWithGlobalConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace CodeWithDennis\FilamentTests\Concerns;

use CodeWithDennis\FilamentTests\Exceptions\GlobalConfiguredUsingCouldNotBeDeterminedException;
use Filament\Forms\Components\Field;
use Filament\Infolists\Components\Entry;
use Filament\Resources\Pages\ListRecords;
use Filament\Support\Concerns\EvaluatesClosures;
use Filament\Tables\Table;

trait InteractsWithGlobalConfiguration
{
use EvaluatesClosures;

public function getGloballyConfiguredUsing(string $class): object
{
return match ($class) {
Table::class => Table::make(app('livewire')->new(ListRecords::class)),
is_subclass_of($class, Entry::class, true),
is_subclass_of($class, Field::class, true) => $class::make('dummy-name'), // some components require a name in their constructor
method_exists($class, 'make') => $class::make(),
default => throw new GlobalConfiguredUsingCouldNotBeDeterminedException($class, 'make() method')
};
}
}
7 changes: 6 additions & 1 deletion src/Concerns/Resources/InteractsWithTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodeWithDennis\FilamentTests\Concerns\Resources;

use CodeWithDennis\FilamentTests\Exceptions\GlobalConfiguredUsingCouldNotBeDeterminedException;
use Filament\Actions\Action;
use Filament\Resources\Pages\ListRecords;
use Filament\Tables\Columns\Column;
Expand Down Expand Up @@ -126,7 +127,11 @@ public function getResourceTableVisibleActions(): array

public function isResourceTableLoadingDeferred(): bool
{
return $this->isTableLoadingGlobalyDeferred() ?: $this->getResourceTable()->isLoadingDeferred();
try {
return $this->getGloballyConfiguredUsing(\Filament\Tables\Table::class)->isLoadingDeferred();
} catch (GlobalConfiguredUsingCouldNotBeDeterminedException) {
return $this->getResourceTable()->isLoadingDeferred();
}
}

public function isResourceTablePaginationEnabled(): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace CodeWithDennis\FilamentTests\Exceptions;

use Exception;

class GlobalConfiguredUsingCouldNotBeDeterminedException extends Exception
{
public function __construct(string $class, string $method)
{
parent::__construct("Could not determine the globally configured using for {$class}::{$method}().");
}
}
16 changes: 2 additions & 14 deletions src/TestRenderers/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CodeWithDennis\FilamentTests\Concerns\ExposesPublicMethodsToViews;
use CodeWithDennis\FilamentTests\Concerns\HasFilamentResources;
use CodeWithDennis\FilamentTests\Concerns\InteractsWithGlobalConfiguration;
use CodeWithDennis\FilamentTests\Concerns\InteractsWithResources;
use CodeWithDennis\FilamentTests\Concerns\Renderers\CanRenderViews;
use Filament\Resources\Resource;
Expand All @@ -12,31 +13,18 @@ abstract class BaseTest implements HasFilamentResources
{
use CanRenderViews;
use ExposesPublicMethodsToViews;
use InteractsWithGlobalConfiguration;
use InteractsWithResources;

public bool $tableLoadingGloballyDeferred = false;

public function __construct(
public ?string $resourceClass = null,
) {}

public function tableLoadingGloballyDeferred(bool $tableLoadingGloballyDeferred): static
{
$this->tableLoadingGloballyDeferred = $tableLoadingGloballyDeferred;

return $this;
}

public static function build(string $resourceClass): static
{
return new static($resourceClass);
}

public function isTableLoadingGlobalyDeferred(): bool
{
return $this->tableLoadingGloballyDeferred;
}

public function getResourceClass(): ?string
{
return $this->resourceClass;
Expand Down