From c768180a70ce2287f1367f2a48093ea2b7e3f92d Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Mon, 7 Apr 2025 10:24:45 +0100 Subject: [PATCH] Clean up --- .../support/src/Concerns/HasCellState.php | 19 ++++++------------- packages/tables/src/Testing/TestsColumns.php | 16 ++++------------ 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/packages/support/src/Concerns/HasCellState.php b/packages/support/src/Concerns/HasCellState.php index e4b0f0e100..e5dda3a7ef 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(): mixed + public function getState(bool $isCached = true): mixed { $record = $this->getRecord(); @@ -84,7 +84,7 @@ trait HasCellState $recordKey = (string) $record->getKey(); - if (array_key_exists($recordKey, $this->cachedState)) { + if ($isCached && array_key_exists($recordKey, $this->cachedState)) { return $this->cachedState[$recordKey]; } @@ -103,6 +103,10 @@ trait HasCellState $state = $this->getDefaultState(); } + if (! $isCached) { + return $state; + } + return $this->cachedState[$recordKey] = $state; } @@ -142,17 +146,6 @@ 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 2cdf657164..d825f03e60 100644 --- a/packages/tables/src/Testing/TestsColumns.php +++ b/packages/tables/src/Testing/TestsColumns.php @@ -180,9 +180,7 @@ class TestsColumns $livewireClass = $this->instance()::class; - $column->clearCache(); - - $state = $column->getState(); + $state = $column->getState(isCached: false); if (is_array($state)) { $state = json_encode($state); @@ -218,9 +216,7 @@ class TestsColumns $livewireClass = $this->instance()::class; - $column->clearCache(); - - $state = $column->getState(); + $state = $column->getState(isCached: false); if (is_array($state)) { $state = json_encode($state); @@ -256,11 +252,9 @@ class TestsColumns $livewireClass = $this->instance()::class; - $column->clearCache(); - Assert::assertEquals( $value, - $column->formatState($column->getState()), + $column->formatState($column->getState(isCached: false)), message: "Failed asserting that a table column with name [{$name}] has a formatted state of [{$value}] for record [{$record->getKey()}] on the [{$livewireClass}] component.", ); @@ -285,10 +279,8 @@ class TestsColumns $livewireClass = $this->instance()::class; - $column->clearCache(); - Assert::assertFalse( - $column->formatState($column->getState()) == $value, + $column->formatState($column->getState(isCached: false)) == $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.", );