Skip to content

Commit

Permalink
Applied some small fixes, as well as updated readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkbushell committed May 16, 2024
1 parent d4734d7 commit 39e246f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ value for.

## Changelog

#### 11.0.3

* Bug fix for count cache when relation is removed (#118)
* Identified and applied a similar bugfix for the sum cache

#### 11.0.2

* Fixed a bug where relationships were not being returned
Expand Down
12 changes: 6 additions & 6 deletions src/Behaviours/CountCache/CountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function update(): void
$this->apply(function (CacheConfig $config) {
$foreignKey = $config->foreignKeyName($this->model);

// We only need to do anything if the foreign key was changed.
// We only do updates if the foreign key was actually changed
if (!$this->model->wasChanged($foreignKey)) {
return;
}
Expand All @@ -45,10 +45,10 @@ public function update(): void

$this->updateCacheValue($originalRelatedModel, $config, -1);

// if the relation is null, then we don't need to do anything else.
if($this->model->{$foreignKey}) {
$this->updateCacheValue($config->relatedModel($this->model), $config, 1);
}
// If there is no longer a relation, nothing more to do.
if (null === $this->model->{$foreignKey}) return;

$this->updateCacheValue($config->relatedModel($this->model), $config, 1);
});
}

Expand All @@ -75,4 +75,4 @@ public function decrement(): void
$this->updateCacheValue($config->relatedModel($this->model), $config, -1);
});
}
}
}
4 changes: 2 additions & 2 deletions src/Behaviours/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function fromId($id)
{
$salt = md5(uniqid().$id);
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$slug = with(new Hashids($salt, $length = 8, $alphabet))->encode($id);
$slug = with(new Hashids($salt, 8, $alphabet))->encode($id);

return new Slug($slug);
}
Expand Down Expand Up @@ -71,4 +71,4 @@ public function toJson($options = 0): string
{
return $this->__toString();
}
}
}
5 changes: 4 additions & 1 deletion src/Behaviours/SumCache/SumCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public function update(): void
// for the minus operation, we first have to get the model that is no longer associated with this one.
$originalRelatedModel = $config->emptyRelatedModel($this->model)->find($this->model->getOriginal($foreignKey));
$this->updateCacheValue($originalRelatedModel, $config, -$this->model->getOriginal($config->sourceField));

if (null === $this->model->{$foreignKey}) return;

$this->updateCacheValue($config->relatedModel($this->model), $config, $this->model->{$config->sourceField});
} else {
$difference = $this->model->{$config->sourceField} - $this->model->getOriginal($config->sourceField);
Expand All @@ -77,4 +80,4 @@ protected function config($relationName, $sourceField): CacheConfig

return new CacheConfig($relationName, $aggregateField, $sourceField);
}
}
}
4 changes: 2 additions & 2 deletions tests/Acceptance/AcceptanceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private function migrate()

Schema::create('items', function (Blueprint $table) {
$table->increments('id');
$table->integer('order_id');
$table->integer('order_id')->nullable();
$table->integer('amount');
$table->timestamps();
$table->softDeletes();
Expand All @@ -90,4 +90,4 @@ private function migrate()
$table->timestamps();
});
}
}
}
6 changes: 3 additions & 3 deletions tests/Acceptance/CountCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function test_whenRelatedModelsAreSwitchedBothCountCachesAreUpdated()
$this->assertEquals(1, $user2->fresh()->commentCount);
}

public function testItCanHandleModelRestoration()
public function test_itCanHandleModelRestoration()
{
$post = Post::factory()->create();

Expand All @@ -44,7 +44,7 @@ public function testItCanHandleModelRestoration()
$this->assertEquals(1, $post->fresh()->commentCount);
}

public function testItCanHandleNullableRelation()
public function test_cacheIsNotUsedWhenRelatedFieldIsNull()
{
$user1 = User::factory()->create();
$posts = Post::factory()->count(2)->for($user1)->create();
Expand All @@ -57,4 +57,4 @@ public function testItCanHandleNullableRelation()

$this->assertEquals(1, $user1->fresh()->postCount);
}
}
}
15 changes: 14 additions & 1 deletion tests/Acceptance/SumCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,17 @@ function test_aggregateValueOnOriginalRelatedModelIsUpdatedCorrectlyWhenTheForei
$this->assertEquals(0, Order::first()->totalAmount);
$this->assertEquals(20, $newOrder->fresh()->totalAmount);
}
}

public function test_cacheIsNotUsedWhenRelatedFieldIsNull()
{
$order = Order::factory()->create();
$items = Item::factory()->count(5)->for($order)->create(['amount' => 1]);

$this->assertEquals(5, Order::first()->totalAmount);

$items->first()->order_id = null;
$items->first()->save();

$this->assertEquals(4, $order->fresh()->totalAmount);
}
}

0 comments on commit 39e246f

Please sign in to comment.