From eda4c5ec3ce3661d994e2b01f6a1396e8bac52dc Mon Sep 17 00:00:00 2001 From: Joe <104938042+lrljoe@users.noreply.github.com> Date: Thu, 26 Dec 2024 21:19:29 +0000 Subject: [PATCH] Tidy ComponentUtilities, Migrate ColumnSelectQueryString (#2135) * Tidy ComponentUtilities, Migrate ColumnSelectQueryString * Fix styling --------- Co-authored-by: lrljoe --- src/Traits/ComponentUtilities.php | 18 --- .../Configuration/ComponentConfiguration.php | 111 ---------------- .../Configuration/QueryConfiguration.php | 124 ++++++++++++++++++ .../QueryStringConfiguration.php | 32 ++++- .../Core/QueryStrings/HasQueryString.php | 59 --------- .../HasQueryStringForColumnSelect.php | 66 ++++++++++ src/Traits/HasAllTraits.php | 3 +- src/Traits/Helpers/ComponentHelpers.php | 95 -------------- src/Traits/Helpers/QueryHelpers.php | 99 ++++++++++++++ src/Traits/Helpers/QueryStringHelpers.php | 25 ++++ src/Traits/WithColumnSelect.php | 8 +- src/Traits/WithQuery.php | 31 +++++ src/Traits/WithQueryString.php | 10 +- .../QueryStringForColumnSelectTest.php | 67 ++++++++++ .../QueryStringForFiltersTest.php | 106 +++++---------- .../QueryStrings/QueryStringForSearchTest.php | 109 +++++---------- .../QueryStrings/QueryStringForSortTest.php | 108 +++++---------- .../Core/QueryStrings/QueryStringTestBase.php | 28 ++++ 18 files changed, 576 insertions(+), 523 deletions(-) create mode 100644 src/Traits/Configuration/QueryConfiguration.php delete mode 100644 src/Traits/Core/QueryStrings/HasQueryString.php create mode 100644 src/Traits/Core/QueryStrings/HasQueryStringForColumnSelect.php create mode 100644 src/Traits/Helpers/QueryHelpers.php create mode 100644 src/Traits/WithQuery.php create mode 100644 tests/Unit/Traits/Core/QueryStrings/QueryStringForColumnSelectTest.php create mode 100644 tests/Unit/Traits/Core/QueryStrings/QueryStringTestBase.php diff --git a/src/Traits/ComponentUtilities.php b/src/Traits/ComponentUtilities.php index 89607fe66..d2121205f 100644 --- a/src/Traits/ComponentUtilities.php +++ b/src/Traits/ComponentUtilities.php @@ -15,14 +15,8 @@ trait ComponentUtilities public array $table = []; - protected Builder $builder; - protected $model; - protected ?string $primaryKey; - - protected array $relationships = []; - #[Locked] public string $tableName = 'table'; @@ -31,20 +25,8 @@ trait ComponentUtilities protected bool $offlineIndicatorStatus = true; - protected bool $eagerLoadAllRelationsStatus = false; - protected string $emptyMessage = 'No items found, try to broaden your search'; - protected array $additionalSelects = []; - - protected array $extraWiths = []; - - protected array $extraWithCounts = []; - - protected array $extraWithSums = []; - - protected array $extraWithAvgs = []; - protected bool $useComputedProperties = true; protected bool $hasRunConfigure = false; diff --git a/src/Traits/Configuration/ComponentConfiguration.php b/src/Traits/Configuration/ComponentConfiguration.php index f233718c8..fee44c87d 100644 --- a/src/Traits/Configuration/ComponentConfiguration.php +++ b/src/Traits/Configuration/ComponentConfiguration.php @@ -4,13 +4,6 @@ trait ComponentConfiguration { - public function setPrimaryKey(?string $key): self - { - $this->primaryKey = $key; - - return $this; - } - /** * Set the empty message */ @@ -42,54 +35,6 @@ public function setOfflineIndicatorDisabled(): self return $this; } - public function setEagerLoadAllRelationsStatus(bool $status): self - { - $this->eagerLoadAllRelationsStatus = $status; - - return $this; - } - - public function setEagerLoadAllRelationsEnabled(): self - { - $this->setEagerLoadAllRelationsStatus(true); - - return $this; - } - - public function setEagerLoadAllRelationsDisabled(): self - { - $this->setEagerLoadAllRelationsStatus(false); - - return $this; - } - - /** - * Allows adding a single set of additional selects to the query - */ - public function setAdditionalSelects(string|array $selects): self - { - if (! is_array($selects)) { - $selects = [$selects]; - } - - $this->additionalSelects = $selects; - - return $this; - } - - /** - * Allows appending more additional selects - */ - public function addAdditionalSelects(string|array $selects): self - { - if (! is_array($selects)) { - $selects = [$selects]; - } - $this->additionalSelects = [...$this->additionalSelects, ...$selects]; - - return $this; - } - public function setDataTableFingerprint(string $dataTableFingerprint): self { $this->dataTableFingerprint = $dataTableFingerprint; @@ -97,62 +42,6 @@ public function setDataTableFingerprint(string $dataTableFingerprint): self return $this; } - public function setExtraWiths(array $extraWiths): self - { - $this->extraWiths = $extraWiths; - - return $this; - } - - public function addExtraWith(string $extraWith): self - { - $this->extraWiths[] = $extraWith; - - return $this; - } - - public function addExtraWiths(array $extraWiths): self - { - $this->extraWiths = [...$this->extraWiths, ...$extraWiths]; - - return $this; - } - - public function setExtraWithCounts(array $extraWithCounts): self - { - $this->extraWithCounts = $extraWithCounts; - - return $this; - } - - public function addExtraWithCount(string $extraWithCount): self - { - $this->extraWithCounts[] = $extraWithCount; - - return $this; - } - - public function addExtraWithCounts(array $extraWithCounts): self - { - $this->extraWithCounts = [...$this->extraWithCounts, ...$extraWithCounts]; - - return $this; - } - - public function addExtraWithSum(string $relationship, string $column): self - { - $this->extraWithSums[] = ['table' => $relationship, 'field' => $column]; - - return $this; - } - - public function addExtraWithAvg(string $relationship, string $column): self - { - $this->extraWithAvgs[] = ['table' => $relationship, 'field' => $column]; - - return $this; - } - public function useComputedPropertiesEnabled(): self { $this->useComputedProperties = true; diff --git a/src/Traits/Configuration/QueryConfiguration.php b/src/Traits/Configuration/QueryConfiguration.php new file mode 100644 index 000000000..8c2d5f3e6 --- /dev/null +++ b/src/Traits/Configuration/QueryConfiguration.php @@ -0,0 +1,124 @@ +builder = $builder; + } + + public function setPrimaryKey(?string $key): self + { + $this->primaryKey = $key; + + return $this; + } + + /** + * Allows adding a single set of additional selects to the query + */ + public function setAdditionalSelects(string|array $selects): self + { + if (! is_array($selects)) { + $selects = [$selects]; + } + + $this->additionalSelects = $selects; + + return $this; + } + + /** + * Allows appending more additional selects + */ + public function addAdditionalSelects(string|array $selects): self + { + if (! is_array($selects)) { + $selects = [$selects]; + } + $this->additionalSelects = [...$this->additionalSelects, ...$selects]; + + return $this; + } + + public function setExtraWiths(array $extraWiths): self + { + $this->extraWiths = $extraWiths; + + return $this; + } + + public function addExtraWith(string $extraWith): self + { + $this->extraWiths[] = $extraWith; + + return $this; + } + + public function addExtraWiths(array $extraWiths): self + { + $this->extraWiths = [...$this->extraWiths, ...$extraWiths]; + + return $this; + } + + public function setExtraWithCounts(array $extraWithCounts): self + { + $this->extraWithCounts = $extraWithCounts; + + return $this; + } + + public function addExtraWithCount(string $extraWithCount): self + { + $this->extraWithCounts[] = $extraWithCount; + + return $this; + } + + public function addExtraWithCounts(array $extraWithCounts): self + { + $this->extraWithCounts = [...$this->extraWithCounts, ...$extraWithCounts]; + + return $this; + } + + public function addExtraWithSum(string $relationship, string $column): self + { + $this->extraWithSums[] = ['table' => $relationship, 'field' => $column]; + + return $this; + } + + public function addExtraWithAvg(string $relationship, string $column): self + { + $this->extraWithAvgs[] = ['table' => $relationship, 'field' => $column]; + + return $this; + } + + public function setEagerLoadAllRelationsStatus(bool $status): self + { + $this->eagerLoadAllRelationsStatus = $status; + + return $this; + } + + public function setEagerLoadAllRelationsEnabled(): self + { + $this->setEagerLoadAllRelationsStatus(true); + + return $this; + } + + public function setEagerLoadAllRelationsDisabled(): self + { + $this->setEagerLoadAllRelationsStatus(false); + + return $this; + } +} diff --git a/src/Traits/Configuration/QueryStringConfiguration.php b/src/Traits/Configuration/QueryStringConfiguration.php index fb0dedfad..e62997bc5 100644 --- a/src/Traits/Configuration/QueryStringConfiguration.php +++ b/src/Traits/Configuration/QueryStringConfiguration.php @@ -4,13 +4,6 @@ trait QueryStringConfiguration { - public function setQueryStringAlias(string $queryStringAlias): self - { - $this->queryStringAlias = $queryStringAlias; - - return $this; - } - public function setupQueryStringStatus(): void { if (! $this->hasQueryStringStatus()) { @@ -41,4 +34,29 @@ public function setQueryStringDisabled(): self return $this; } + + public function setQueryStringAlias(string $queryStringAlias): self + { + $this->queryStringAlias = $queryStringAlias; + + return $this; + } + + protected function setQueryStringConfig(string $type, array $config): self + { + $this->queryStringConfig[$type] = array_merge($this->getQueryStringConfig($type), $config); + + return $this; + } + + protected function setQueryStringConfigStatus(string $type, bool $status): self + { + return $this->setQueryStringConfig($type, ['status' => $status]); + + } + + protected function setQueryStringConfigAlias(string $type, string $alias): self + { + return $this->setQueryStringConfig($type, ['alias' => $alias]); + } } diff --git a/src/Traits/Core/QueryStrings/HasQueryString.php b/src/Traits/Core/QueryStrings/HasQueryString.php deleted file mode 100644 index 7c1f567b6..000000000 --- a/src/Traits/Core/QueryStrings/HasQueryString.php +++ /dev/null @@ -1,59 +0,0 @@ - [], - 'filter' => [], - 'search' => [], - 'sorts' => [], - ]; - - protected function getQueryStringConfig(string $type): array - { - return array_merge(['status' => null, 'alias' => null], ($this->queryStringConfig[$type] ?? [])); - } - - protected function hasQueryStringConfigStatus(string $type): bool - { - return isset($this->getQueryStringConfig($type)['status']); - } - - protected function getQueryStringConfigStatus(string $type): bool - { - return $this->getQueryStringConfig($type)['status'] ?? $this->getQueryStringStatus(); - } - - protected function hasQueryStringConfigAlias(string $type): bool - { - return isset($this->getQueryStringConfig($type)['alias']); - } - - protected function getQueryStringConfigAlias(string $type): string - { - return $this->getQueryStringConfig($type)['alias'] ?? $this->getQueryStringAlias().'-'.$type; - } - - protected function setQueryStringConfig(string $type, array $config): self - { - $this->queryStringConfig[$type] = array_merge($this->getQueryStringConfig($type), $config); - - return $this; - } - - protected function setQueryStringConfigStatus(string $type, bool $status): self - { - return $this->setQueryStringConfig($type, ['status' => $status]); - - } - - protected function setQueryStringConfigAlias(string $type, string $alias): self - { - return $this->setQueryStringConfig($type, ['alias' => $alias]); - } -} diff --git a/src/Traits/Core/QueryStrings/HasQueryStringForColumnSelect.php b/src/Traits/Core/QueryStrings/HasQueryStringForColumnSelect.php new file mode 100644 index 000000000..51e9140a2 --- /dev/null +++ b/src/Traits/Core/QueryStrings/HasQueryStringForColumnSelect.php @@ -0,0 +1,66 @@ +queryStringIsEnabled() || $this->queryStringForColumnSelectEnabled()) && $this->columnSelectIsEnabled()) ? ['selectedColumns' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAliasForColumnSelect()]] : []; + + } + + protected function setupQueryStringStatusForColumnSelect(): void + { + if (! $this->hasQueryStringStatusForColumnSelect()) { + $this->setQueryStringForColumnSelectEnabled(); + } + } + + public function hasQueryStringStatusForColumnSelect(): bool + { + return $this->hasQueryStringConfigStatus('columns'); + } + + public function getQueryStringStatusForColumnSelect(): bool + { + return $this->getQueryStringConfigStatus('columns'); + } + + public function queryStringForColumnSelectEnabled(): bool + { + $this->setupQueryStringStatusForColumnSelect(); + + return $this->getQueryStringStatusForColumnSelect() && $this->columnSelectIsEnabled(); + } + + public function setQueryStringStatusForColumnSelect(bool $status): self + { + return $this->setQueryStringConfigStatus('columns', $status); + } + + public function setQueryStringForColumnSelectEnabled(): self + { + return $this->setQueryStringStatusForColumnSelect(true); + } + + public function setQueryStringForColumnSelectDisabled(): self + { + return $this->setQueryStringStatusForColumnSelect(false); + } + + public function hasQueryStringAliasForColumnSelect(): bool + { + return $this->hasQueryStringConfigAlias('columns'); + } + + public function getQueryStringAliasForColumnSelect(): string + { + return $this->getQueryStringConfigAlias('columns'); + } + + public function setQueryStringAliasForColumnSelect(string $alias): self + { + return $this->setQueryStringConfigAlias('columns', $alias); + } +} diff --git a/src/Traits/HasAllTraits.php b/src/Traits/HasAllTraits.php index ebbe7a815..e2e675588 100644 --- a/src/Traits/HasAllTraits.php +++ b/src/Traits/HasAllTraits.php @@ -12,7 +12,8 @@ trait HasAllTraits use HasLocalisations; use WithLoadingPlaceholder; use HasTheme; - use ComponentUtilities, + use WithQuery, + ComponentUtilities, WithActions, WithData, WithQueryString, diff --git a/src/Traits/Helpers/ComponentHelpers.php b/src/Traits/Helpers/ComponentHelpers.php index ff4c5c212..06cf2ee16 100644 --- a/src/Traits/Helpers/ComponentHelpers.php +++ b/src/Traits/Helpers/ComponentHelpers.php @@ -12,38 +12,6 @@ public function getDataTableFingerprint(): string return $this->dataTableFingerprint ?? ($this->dataTableFingerprint = $this->generateDataTableFingerprint()); } - public function setBuilder(Builder $builder): void - { - $this->builder = $builder; - } - - public function getBuilder(): Builder - { - return $this->builder; - } - - public function hasPrimaryKey(): bool - { - return isset($this->primaryKey) && $this->primaryKey !== null; - } - - /** - * @return mixed - */ - #[Computed] - public function getPrimaryKey() - { - return $this->primaryKey; - } - - /** - * @return array - */ - public function getRelationships(): array - { - return $this->relationships; - } - public function hasModel(): bool { return $this->model !== null; @@ -106,69 +74,6 @@ public function isTableNamed(string $name): bool return $this->tableName === $name; } - public function getEagerLoadAllRelationsStatus(): bool - { - return $this->eagerLoadAllRelationsStatus; - } - - public function eagerLoadAllRelationsIsEnabled(): bool - { - return $this->getEagerLoadAllRelationsStatus() === true; - } - - public function eagerLoadAllRelationsIsDisabled(): bool - { - return $this->getEagerLoadAllRelationsStatus() === false; - } - - /** - * @return array - */ - public function getAdditionalSelects(): array - { - return $this->additionalSelects; - } - - public function hasExtraWiths(): bool - { - return ! empty($this->extraWiths); - } - - public function getExtraWiths(): array - { - return $this->extraWiths; - } - - public function hasExtraWithCounts(): bool - { - return ! empty($this->extraWithCounts); - } - - public function getExtraWithCounts(): array - { - return $this->extraWithCounts; - } - - public function hasExtraWithSums(): bool - { - return ! empty($this->extraWithSums); - } - - public function getExtraWithSums(): array - { - return $this->extraWithSums; - } - - public function hasExtraWithAvgs(): bool - { - return ! empty($this->extraWithAvgs); - } - - public function getExtraWithAvgs(): array - { - return $this->extraWithAvgs; - } - public function getComputedPropertiesStatus(): bool { return $this->useComputedProperties ?? false; diff --git a/src/Traits/Helpers/QueryHelpers.php b/src/Traits/Helpers/QueryHelpers.php new file mode 100644 index 000000000..1a7b2d46c --- /dev/null +++ b/src/Traits/Helpers/QueryHelpers.php @@ -0,0 +1,99 @@ +builder; + } + + public function hasPrimaryKey(): bool + { + return isset($this->primaryKey) && $this->primaryKey !== null; + } + + /** + * @return mixed + */ + #[Computed] + public function getPrimaryKey() + { + return $this->primaryKey; + } + + /** + * @return array + */ + public function getRelationships(): array + { + return $this->relationships; + } + + /** + * @return array + */ + public function getAdditionalSelects(): array + { + return $this->additionalSelects; + } + + public function hasExtraWiths(): bool + { + return ! empty($this->extraWiths); + } + + public function getExtraWiths(): array + { + return $this->extraWiths; + } + + public function hasExtraWithCounts(): bool + { + return ! empty($this->extraWithCounts); + } + + public function getExtraWithCounts(): array + { + return $this->extraWithCounts; + } + + public function hasExtraWithSums(): bool + { + return ! empty($this->extraWithSums); + } + + public function getExtraWithSums(): array + { + return $this->extraWithSums; + } + + public function hasExtraWithAvgs(): bool + { + return ! empty($this->extraWithAvgs); + } + + public function getExtraWithAvgs(): array + { + return $this->extraWithAvgs; + } + + public function getEagerLoadAllRelationsStatus(): bool + { + return $this->eagerLoadAllRelationsStatus; + } + + public function eagerLoadAllRelationsIsEnabled(): bool + { + return $this->getEagerLoadAllRelationsStatus() === true; + } + + public function eagerLoadAllRelationsIsDisabled(): bool + { + return $this->getEagerLoadAllRelationsStatus() === false; + } +} diff --git a/src/Traits/Helpers/QueryStringHelpers.php b/src/Traits/Helpers/QueryStringHelpers.php index e1acd73a9..cb1940e2a 100644 --- a/src/Traits/Helpers/QueryStringHelpers.php +++ b/src/Traits/Helpers/QueryStringHelpers.php @@ -35,4 +35,29 @@ public function getQueryStringAlias(): string { return $this->queryStringAlias ?? $this->getTableName(); } + + protected function getQueryStringConfig(string $type): array + { + return array_merge(['status' => null, 'alias' => null], ($this->queryStringConfig[$type] ?? [])); + } + + protected function hasQueryStringConfigStatus(string $type): bool + { + return isset($this->getQueryStringConfig($type)['status']); + } + + protected function getQueryStringConfigStatus(string $type): bool + { + return $this->getQueryStringConfig($type)['status'] ?? $this->getQueryStringStatus(); + } + + protected function hasQueryStringConfigAlias(string $type): bool + { + return isset($this->getQueryStringConfig($type)['alias']); + } + + protected function getQueryStringConfigAlias(string $type): string + { + return $this->getQueryStringConfig($type)['alias'] ?? $this->getQueryStringAlias().'-'.$type; + } } diff --git a/src/Traits/WithColumnSelect.php b/src/Traits/WithColumnSelect.php index d398967cc..e992c72a8 100644 --- a/src/Traits/WithColumnSelect.php +++ b/src/Traits/WithColumnSelect.php @@ -5,12 +5,14 @@ use Livewire\Attributes\Locked; use Rappasoft\LaravelLivewireTables\Events\ColumnsSelected; use Rappasoft\LaravelLivewireTables\Traits\Configuration\ColumnSelectConfiguration; +use Rappasoft\LaravelLivewireTables\Traits\Core\QueryStrings\HasQueryStringForColumnSelect; use Rappasoft\LaravelLivewireTables\Traits\Helpers\ColumnSelectHelpers; trait WithColumnSelect { use ColumnSelectConfiguration, - ColumnSelectHelpers; + ColumnSelectHelpers, + HasQueryStringForColumnSelect; #[Locked] public array $columnSelectColumns = ['setupRun' => false, 'selected' => [], 'deselected' => [], 'defaultdeselected' => []]; @@ -35,7 +37,7 @@ trait WithColumnSelect protected bool $columnSelectHiddenOnTablet = false; - protected function queryStringWithColumnSelect(): array + /*protected function queryStringWithColumnSelect(): array { if ($this->queryStringIsEnabled() && $this->columnSelectIsEnabled()) { return [ @@ -44,7 +46,7 @@ protected function queryStringWithColumnSelect(): array } return []; - } + }*/ public function bootedWithColumnSelect(): void { diff --git a/src/Traits/WithQuery.php b/src/Traits/WithQuery.php new file mode 100644 index 000000000..836006ae2 --- /dev/null +++ b/src/Traits/WithQuery.php @@ -0,0 +1,31 @@ + ['status' => false, 'alias' => null], + 'filter' => ['status' => false, 'alias' => null], + 'search' => ['status' => true, 'alias' => null], + 'sorts' => ['status' => true, 'alias' => null], + ]; #[Locked] public ?bool $queryStringStatus; diff --git a/tests/Unit/Traits/Core/QueryStrings/QueryStringForColumnSelectTest.php b/tests/Unit/Traits/Core/QueryStrings/QueryStringForColumnSelectTest.php new file mode 100644 index 000000000..1ab117386 --- /dev/null +++ b/tests/Unit/Traits/Core/QueryStrings/QueryStringForColumnSelectTest.php @@ -0,0 +1,67 @@ +configure(); + parent::$mock->boot(); + + $this->assertSame(false, parent::$mock->getQueryStringStatusForColumnSelect()); + + parent::$mock->setQueryStringForColumnSelectEnabled(); + $this->assertSame(true, parent::$mock->getQueryStringStatusForColumnSelect()); + + } + + public function test_can_disable_column_select_query_string_status(): void + { + parent::$mock->configure(); + parent::$mock->boot(); + $this->assertSame(false, parent::$mock->getQueryStringStatusForColumnSelect()); + parent::$mock->setQueryStringForColumnSelectDisabled(); + $this->assertSame(false, parent::$mock->getQueryStringStatusForColumnSelect()); + } + + public function test_can_enable_column_select_query_string_status(): void + { + parent::$mock->configure(); + parent::$mock->boot(); + + $this->assertSame(false, parent::$mock->getQueryStringStatusForColumnSelect()); + parent::$mock->setQueryStringForColumnSelectEnabled(); + $this->assertSame(true, parent::$mock->getQueryStringStatusForColumnSelect()); + parent::$mock->setQueryStringForColumnSelectDisabled(); + $this->assertSame(false, parent::$mock->getQueryStringStatusForColumnSelect()); + + } + + public function test_can_get_default_column_select_query_string_alias(): void + { + parent::$mock->configure(); + parent::$mock->boot(); + + $this->assertSame('table-columns', parent::$mock->getQueryStringAliasForColumnSelect()); + + } + + public function test_can_change_default_column_select_query_string_alias(): void + { + parent::$mock->configure(); + parent::$mock->boot(); + + $this->assertSame('table-columns', parent::$mock->getQueryStringAliasForColumnSelect()); + $this->assertFalse(parent::$mock->hasQueryStringAliasForColumnSelect()); + parent::$mock->setQueryStringAliasForColumnSelect('selected-columns'); + $this->assertSame('selected-columns', parent::$mock->getQueryStringAliasForColumnSelect()); + $this->assertTrue(parent::$mock->hasQueryStringAliasForColumnSelect()); + } +} diff --git a/tests/Unit/Traits/Core/QueryStrings/QueryStringForFiltersTest.php b/tests/Unit/Traits/Core/QueryStrings/QueryStringForFiltersTest.php index 849da880f..63ae05085 100644 --- a/tests/Unit/Traits/Core/QueryStrings/QueryStringForFiltersTest.php +++ b/tests/Unit/Traits/Core/QueryStrings/QueryStringForFiltersTest.php @@ -6,107 +6,63 @@ use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\Attributes\Group; use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable; -use Rappasoft\LaravelLivewireTables\Tests\TestCase; #[Group('Filters')] -final class QueryStringForFiltersTest extends TestCase +#[Group('QueryString')] +final class QueryStringForFiltersTest extends QueryStringTestBase { - public function test_can_get_default_filter_query_string_status(): void + protected function setUp(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - } - }; + parent::setUp(); + } - $mock->configure(); - $mock->boot(); + public function test_can_get_default_filter_query_string_status(): void + { + parent::$mock->configure(); + parent::$mock->boot(); - $this->assertSame(true, $mock->getQueryStringStatusForFilter()); + $this->assertSame(true, parent::$mock->getQueryStringStatusForFilter()); } public function test_can_disable_filter_query_string_status(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - $this->setQueryStringForFilterDisabled(); - } - }; - - $mock->configure(); - $mock->boot(); - - $this->assertSame(false, $mock->getQueryStringStatusForFilter()); + parent::$mock->configure(); + parent::$mock->boot(); + $this->assertSame(true, parent::$mock->getQueryStringStatusForFilter()); + parent::$mock->setQueryStringForFilterDisabled(); + $this->assertSame(false, parent::$mock->getQueryStringStatusForFilter()); } public function test_can_enable_filter_query_string_status(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - $this->setQueryStringForFilterDisabled(); - } - }; + parent::$mock->configure(); + parent::$mock->boot(); - $mock->configure(); - $mock->boot(); - - $this->assertSame(false, $mock->getQueryStringStatusForFilter()); - $mock->setQueryStringForFilterEnabled(); - $this->assertSame(true, $mock->getQueryStringStatusForFilter()); + $this->assertSame(true, parent::$mock->getQueryStringStatusForFilter()); + parent::$mock->setQueryStringForFilterDisabled(); + $this->assertSame(false, parent::$mock->getQueryStringStatusForFilter()); + parent::$mock->setQueryStringForFilterEnabled(); + $this->assertSame(true, parent::$mock->getQueryStringStatusForFilter()); } public function test_can_get_default_filter_query_string_alias(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - } - }; + parent::$mock->configure(); + parent::$mock->boot(); - $mock->configure(); - $mock->boot(); - - $this->assertSame('table-filters', $mock->getQueryStringAliasForFilter()); + $this->assertSame('table-filters', parent::$mock->getQueryStringAliasForFilter()); } public function test_can_change_default_filter_query_string_alias(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - } - }; - - $mock->configure(); - $mock->boot(); + parent::$mock->configure(); + parent::$mock->boot(); - $this->assertSame('table-filters', $mock->getQueryStringAliasForFilter()); - $mock->setQueryStringAliasForFilter('pet-filters'); - $this->assertSame('pet-filters', $mock->getQueryStringAliasForFilter()); - $this->assertTrue($mock->hasQueryStringAliasForFilter()); + $this->assertSame('table-filters', parent::$mock->getQueryStringAliasForFilter()); + parent::$mock->setQueryStringAliasForFilter('pet-filters'); + $this->assertSame('pet-filters', parent::$mock->getQueryStringAliasForFilter()); + $this->assertTrue(parent::$mock->hasQueryStringAliasForFilter()); } } diff --git a/tests/Unit/Traits/Core/QueryStrings/QueryStringForSearchTest.php b/tests/Unit/Traits/Core/QueryStrings/QueryStringForSearchTest.php index c972feb96..f85deaf97 100644 --- a/tests/Unit/Traits/Core/QueryStrings/QueryStringForSearchTest.php +++ b/tests/Unit/Traits/Core/QueryStrings/QueryStringForSearchTest.php @@ -4,107 +4,62 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Depends; +use PHPUnit\Framework\Attributes\Group; use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable; -use Rappasoft\LaravelLivewireTables\Tests\TestCase; -final class QueryStringForSearchTest extends TestCase +#[Group('QueryString')] +final class QueryStringForSearchTest extends QueryStringTestBase { - public function test_can_get_default_search_query_string_status(): void + protected function setUp(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - } - }; + parent::setUp(); + } - $mock->configure(); - $mock->boot(); + public function test_can_get_default_search_query_string_status(): void + { + parent::$mock->configure(); + parent::$mock->boot(); - $this->assertSame(true, $mock->getQueryStringStatusForSearch()); + $this->assertSame(true, parent::$mock->getQueryStringStatusForSearch()); } public function test_can_disable_search_query_string_status(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - $this->setQueryStringForSearchDisabled(); - } - }; + parent::$mock->configure(); + parent::$mock->boot(); + parent::$mock->setQueryStringForSearchDisabled(); - $mock->configure(); - $mock->boot(); - - $this->assertSame(false, $mock->getQueryStringStatusForSearch()); + $this->assertSame(false, parent::$mock->getQueryStringStatusForSearch()); } public function test_can_enable_search_query_string_status(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - $this->setQueryStringForSearchDisabled(); - } - }; - - $mock->configure(); - $mock->boot(); - - $this->assertSame(false, $mock->getQueryStringStatusForSearch()); - $mock->setQueryStringForSearchEnabled(); - $this->assertSame(true, $mock->getQueryStringStatusForSearch()); + parent::$mock->configure(); + parent::$mock->boot(); + parent::$mock->setQueryStringForSearchDisabled(); + $this->assertSame(false, parent::$mock->getQueryStringStatusForSearch()); + parent::$mock->setQueryStringForSearchEnabled(); + $this->assertSame(true, parent::$mock->getQueryStringStatusForSearch()); } public function test_can_get_default_search_query_string_alias(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - } - }; - - $mock->configure(); - $mock->boot(); + parent::$mock->configure(); + parent::$mock->boot(); - $this->assertSame('table-search', $mock->getQueryStringAliasForSearch()); + $this->assertSame('table-search', parent::$mock->getQueryStringAliasForSearch()); } public function test_can_change_default_search_query_string_alias(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - } - }; - - $mock->configure(); - $mock->boot(); - - $this->assertFalse($mock->hasQueryStringAliasForSearch()); - $this->assertSame('table-search', $mock->getQueryStringAliasForSearch()); - $mock->setQueryStringAliasForSearch('pet-search'); - $this->assertSame('pet-search', $mock->getQueryStringAliasForSearch()); - $this->assertTrue($mock->hasQueryStringAliasForSearch()); + parent::$mock->configure(); + parent::$mock->boot(); + + $this->assertFalse(parent::$mock->hasQueryStringAliasForSearch()); + $this->assertSame('table-search', parent::$mock->getQueryStringAliasForSearch()); + parent::$mock->setQueryStringAliasForSearch('pet-search'); + $this->assertSame('pet-search', parent::$mock->getQueryStringAliasForSearch()); + $this->assertTrue(parent::$mock->hasQueryStringAliasForSearch()); } } diff --git a/tests/Unit/Traits/Core/QueryStrings/QueryStringForSortTest.php b/tests/Unit/Traits/Core/QueryStrings/QueryStringForSortTest.php index 10215a710..4f71eb5ee 100644 --- a/tests/Unit/Traits/Core/QueryStrings/QueryStringForSortTest.php +++ b/tests/Unit/Traits/Core/QueryStrings/QueryStringForSortTest.php @@ -4,107 +4,65 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Depends; +use PHPUnit\Framework\Attributes\Group; use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable; -use Rappasoft\LaravelLivewireTables\Tests\TestCase; -final class QueryStringForSortTest extends TestCase +#[Group('QueryString')] +final class QueryStringForSortTest extends QueryStringTestBase { - public function test_can_get_default_sort_query_string_status(): void + protected function setUp(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - } - }; + parent::setUp(); + } - $mock->configure(); - $mock->boot(); + public function test_can_get_default_sort_query_string_status(): void + { + parent::$mock->configure(); + parent::$mock->boot(); - $this->assertSame(true, $mock->getQueryStringStatusForSort()); + $this->assertSame(true, parent::$mock->getQueryStringStatusForSort()); } public function test_can_disable_sort_query_string_status(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - $this->setQueryStringForSortDisabled(); - } - }; - - $mock->configure(); - $mock->boot(); + parent::$mock->configure(); + parent::$mock->boot(); + parent::$mock->setQueryStringForSortDisabled(); - $this->assertSame(false, $mock->getQueryStringStatusForSort()); + $this->assertSame(false, parent::$mock->getQueryStringStatusForSort()); } public function test_can_enable_sort_query_string_status(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; + parent::$mock->configure(); + parent::$mock->boot(); + parent::$mock->setQueryStringForSortDisabled(); - public function configure(): void - { - $this->setDataTableFingerprint('test'); - $this->setQueryStringForSortDisabled(); - } - }; - - $mock->configure(); - $mock->boot(); - - $this->assertSame(false, $mock->getQueryStringStatusForSort()); - $mock->setQueryStringForSortEnabled(); - $this->assertSame(true, $mock->getQueryStringStatusForSort()); + $this->assertSame(false, parent::$mock->getQueryStringStatusForSort()); + parent::$mock->setQueryStringForSortEnabled(); + $this->assertSame(true, parent::$mock->getQueryStringStatusForSort()); } public function test_can_get_default_sort_query_string_alias(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - } - }; - - $mock->configure(); - $mock->boot(); + parent::$mock->configure(); + parent::$mock->boot(); + parent::$mock->setQueryStringForSortDisabled(); - $this->assertSame('table-sorts', $mock->getQueryStringAliasForSort()); + $this->assertSame('table-sorts', parent::$mock->getQueryStringAliasForSort()); } public function test_can_change_default_sort_query_string_alias(): void { - $mock = new class extends PetsTable - { - public ?array $testAttributesArray; - - public function configure(): void - { - $this->setDataTableFingerprint('test'); - } - }; - - $mock->configure(); - $mock->boot(); - - $this->assertSame('table-sorts', $mock->getQueryStringAliasForSort()); - $mock->setQueryStringAliasForSort('pet-sorts'); - $this->assertSame('pet-sorts', $mock->getQueryStringAliasForSort()); - $this->assertTrue($mock->hasQueryStringAliasForSort()); + parent::$mock->configure(); + parent::$mock->boot(); + parent::$mock->setQueryStringForSortDisabled(); + + $this->assertSame('table-sorts', parent::$mock->getQueryStringAliasForSort()); + parent::$mock->setQueryStringAliasForSort('pet-sorts'); + $this->assertSame('pet-sorts', parent::$mock->getQueryStringAliasForSort()); + $this->assertTrue(parent::$mock->hasQueryStringAliasForSort()); } } diff --git a/tests/Unit/Traits/Core/QueryStrings/QueryStringTestBase.php b/tests/Unit/Traits/Core/QueryStrings/QueryStringTestBase.php new file mode 100644 index 000000000..769ae168f --- /dev/null +++ b/tests/Unit/Traits/Core/QueryStrings/QueryStringTestBase.php @@ -0,0 +1,28 @@ +setDataTableFingerprint('test'); + } + }; + } +}