From 4f3f330306dfed623a44dc29f01b50736cb43bc8 Mon Sep 17 00:00:00 2001 From: Kirk Bushell Date: Sat, 26 Aug 2023 23:58:01 +1000 Subject: [PATCH] Got model restoration working on sumcache. --- src/Behaviours/Cacheable.php | 18 ++++++++++++++++++ src/Behaviours/CountCache/CountCache.php | 11 ++--------- src/Behaviours/CountCache/HasCounts.php | 3 --- src/Behaviours/SumCache/HasSums.php | 4 ---- src/Behaviours/SumCache/SumCache.php | 19 ++++++------------- tests/Acceptance/SumCacheTest.php | 7 +++++++ 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/Behaviours/Cacheable.php b/src/Behaviours/Cacheable.php index 0ac5e75..ddc4565 100644 --- a/src/Behaviours/Cacheable.php +++ b/src/Behaviours/Cacheable.php @@ -1,6 +1,7 @@ relationsMethod() as $key => $value) { + $function($this->config($key, $value)); + } + } + /** * Updates a table's record based on the query information provided in the $config variable. * diff --git a/src/Behaviours/CountCache/CountCache.php b/src/Behaviours/CountCache/CountCache.php index 0101787..b916e78 100644 --- a/src/Behaviours/CountCache/CountCache.php +++ b/src/Behaviours/CountCache/CountCache.php @@ -14,16 +14,9 @@ class CountCache private function __construct(private Countable $model) {} - /** - * Applies the provided function to the count cache setup/configuration. - * - * @param \Closure $function - */ - public function apply(\Closure $function) + private function relationsMethod(): array { - foreach ($this->model->countedBy() as $key => $value) { - $function($this->config($key, $value)); - } + return $this->model->countedBy(); } /** diff --git a/src/Behaviours/CountCache/HasCounts.php b/src/Behaviours/CountCache/HasCounts.php index f407443..c25a45a 100644 --- a/src/Behaviours/CountCache/HasCounts.php +++ b/src/Behaviours/CountCache/HasCounts.php @@ -3,9 +3,6 @@ trait HasCounts { - /** - * Boot the countable behaviour and setup the appropriate event bindings. - */ public static function bootHasCounts() { static::observe(Observer::class); diff --git a/src/Behaviours/SumCache/HasSums.php b/src/Behaviours/SumCache/HasSums.php index 327cf56..8545c99 100644 --- a/src/Behaviours/SumCache/HasSums.php +++ b/src/Behaviours/SumCache/HasSums.php @@ -1,12 +1,8 @@ model->summedBy() as $key => $cache) { - $function($this->config($key, $cache)); - } + return $this->model->summedBy(); } /** * Rebuild the count caches from the database */ - public function rebuild() + public function rebuild(): void { $this->apply(function($config) { $this->rebuildCacheRecord($config, $this->model, 'SUM', $config['columnToSum']); }); } - public function increase() + public function increase(): void { $this->apply(function(CacheConfig $config) { $this->updateCacheValue($config->relatedModel($this->model), $config, $this->model->{$config->sourceField}); }); } - public function decrease() + public function decrease(): void { $this->apply(function(CacheConfig $config) { $this->updateCacheValue($config->relatedModel($this->model), $config, -$this->model->{$config->sourceField}); @@ -52,7 +45,7 @@ public function decrease() /** * Update the cache for all operations. */ - public function update() + public function update(): void { $this->apply(function($config) { $foreignKey = $config->foreignKeyName($this->model); diff --git a/tests/Acceptance/SumCacheTest.php b/tests/Acceptance/SumCacheTest.php index 4d58a91..fcbde70 100644 --- a/tests/Acceptance/SumCacheTest.php +++ b/tests/Acceptance/SumCacheTest.php @@ -47,6 +47,13 @@ function test_whenAnAggregatedModelValueSwitchesContext() $this->assertEquals(45, $order->fresh()->totalAmount); } + function test_aggregateValuesAreUpdatedWhenModelsAreRestored() + { + $this->data['item']->delete(); + + $this->assertEquals(0, $this->data['order']->fresh()->totalAmount); + } + private function setupOrderAndItem() { $order = new Order;