From 890f2e765d36c973160cfc8881ffcc0ec325610e Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Mon, 7 Apr 2025 10:42:48 +0100 Subject: [PATCH] Revert "Clean up" This reverts commit c768180a70ce2287f1367f2a48093ea2b7e3f92d. --- .../support/src/Concerns/HasCellState.php | 19 +++++++++++++------ packages/tables/src/Testing/TestsColumns.php | 16 ++++++++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/support/src/Concerns/HasCellState.php b/packages/support/src/Concerns/HasCellState.php index e5dda3a7ef..e4b0f0e100 100644 --- a/packages/support/src/Concerns/HasCellState.php +++ b/packages/support/src/Concerns/HasCellState.php @@ -74,7 +74,7 @@ trait HasCellState return $this->evaluate($this->defaultState); } - public function getState(bool $isCached = true): mixed + public function getState(): mixed { $record = $this->getRecord(); @@ -84,7 +84,7 @@ trait HasCellState $recordKey = (string) $record->getKey(); - if ($isCached && array_key_exists($recordKey, $this->cachedState)) { + if (array_key_exists($recordKey, $this->cachedState)) { return $this->cachedState[$recordKey]; } @@ -103,10 +103,6 @@ trait HasCellState $state = $this->getDefaultState(); } - if (! $isCached) { - return $state; - } - return $this->cachedState[$recordKey] = $state; } @@ -146,6 +142,17 @@ trait HasCellState return $state->all(); } + public function clearCache(): static + { + if (! app()->isRunningUnitTests()) { + throw new Exception('Clearing cache is only available in unit tests.'); + } + + $this->cachedState = []; + + return $this; + } + public function separator(string | Closure | null $separator = ','): static { $this->separator = $separator; diff --git a/packages/tables/src/Testing/TestsColumns.php b/packages/tables/src/Testing/TestsColumns.php index d825f03e60..2cdf657164 100644 --- a/packages/tables/src/Testing/TestsColumns.php +++ b/packages/tables/src/Testing/TestsColumns.php @@ -180,7 +180,9 @@ class TestsColumns $livewireClass = $this->instance()::class; - $state = $column->getState(isCached: false); + $column->clearCache(); + + $state = $column->getState(); if (is_array($state)) { $state = json_encode($state); @@ -216,7 +218,9 @@ class TestsColumns $livewireClass = $this->instance()::class; - $state = $column->getState(isCached: false); + $column->clearCache(); + + $state = $column->getState(); if (is_array($state)) { $state = json_encode($state); @@ -252,9 +256,11 @@ class TestsColumns $livewireClass = $this->instance()::class; + $column->clearCache(); + Assert::assertEquals( $value, - $column->formatState($column->getState(isCached: false)), + $column->formatState($column->getState()), message: "Failed asserting that a table column with name [{$name}] has a formatted state of [{$value}] for record [{$record->getKey()}] on the [{$livewireClass}] component.", ); @@ -279,8 +285,10 @@ class TestsColumns $livewireClass = $this->instance()::class; + $column->clearCache(); + Assert::assertFalse( - $column->formatState($column->getState(isCached: false)) == $value, + $column->formatState($column->getState()) == $value, message: "Failed asserting that a table column with name [{$name}] does not have a formatted state of [{$value}] for record [{$record->getKey()}] on the [{$livewireClass}] component.", );