Skip to content

Commit

Permalink
Fixing a few bugs, cleaning up tests by adding factories, making it m…
Browse files Browse the repository at this point in the history
…uch easier to expand on additional features and testing.
  • Loading branch information
kirkbushell committed Aug 27, 2023
1 parent 18362db commit 4a4de05
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 69 deletions.
2 changes: 0 additions & 2 deletions src/Behaviours/Cacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

use Closure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

/**
* The cacheable trait is concerned with the related models.
Expand Down
3 changes: 2 additions & 1 deletion src/Behaviours/CountCache/CountCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function update(): void
$this->apply(function(CacheConfig $config) {
$foreignKey = $config->foreignKeyName($this->model);

if (!$this->model->getOriginal($foreignKey) || $this->model->$foreignKey === $this->model->getOriginal($foreignKey)) return;
// We only need to do anything if the foreign key was changed.
if (!$this->model->wasChanged($foreignKey)) return;

// 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));
Expand Down
4 changes: 2 additions & 2 deletions src/Behaviours/SumCache/SumCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ public function rebuild(): void
public function increase(): void
{
$this->apply(function(CacheConfig $config) {
$this->updateCacheValue($config->relatedModel($this->model), $config, $this->model->{$config->sourceField});
$this->updateCacheValue($config->relatedModel($this->model), $config, (int) $this->model->{$config->sourceField});
});
}

public function decrease(): void
{
$this->apply(function(CacheConfig $config) {
$this->updateCacheValue($config->relatedModel($this->model), $config, -$this->model->{$config->sourceField});
$this->updateCacheValue($config->relatedModel($this->model), $config, -(int) $this->model->{$config->sourceField});
});
}

Expand Down
6 changes: 5 additions & 1 deletion src/Behaviours/SumCache/Summable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ interface Summable
*
* Of course, if you want to customise the field saving the total as well, you can do that too:
*
* ['order' => ['amount' => 'total_amount']].
* ['relationshp' => ['aggregate_field' => 'source_field']]
*
* In real-world terms:
*
* ['order' => ['total_amount' => 'amount']]
*
* By default, the sum cache will take the source field, and add "_total" to it on the related model.
*
Expand Down
12 changes: 10 additions & 2 deletions tests/Acceptance/AcceptanceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ private function migrate()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('slug')->nullable();
$table->integer('comment_count')->default(0);
$table->integer('post_count')->default(0);
Expand All @@ -43,6 +43,7 @@ private function migrate()

Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->nullable();
$table->integer('user_id')->nullable();
$table->string('slug')->nullable();
$table->integer('comment_count')->default(0);
Expand All @@ -69,5 +70,12 @@ private function migrate()
$table->integer('amount');
$table->timestamps();
});

Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_count')->default(0);
$table->integer('total_comments')->default(0);
$table->timestamps();
});
}
}
18 changes: 18 additions & 0 deletions tests/Acceptance/ChainedAggregatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tests\Acceptance;

use Tests\Acceptance\Models\Category;
use Tests\Acceptance\Models\Comment;

class ChainedAggregatesTest extends AcceptanceTestCase
{
function test_aggregateDependentsAreUpdated()
{
// Comment will create a user, and a post - the post created will bubble up to category and update it as well.
Comment::factory()->create();

$this->assertSame(1, Category::first()->postCount);
$this->assertSame(1, Category::first()->totalComments);
}
}
65 changes: 19 additions & 46 deletions tests/Acceptance/CountCacheTest.php
Original file line number Diff line number Diff line change
@@ -1,76 +1,49 @@
<?php
namespace Tests\Acceptance;

use Tests\Acceptance\Models\Category;
use Tests\Acceptance\Models\Comment;
use Tests\Acceptance\Models\Post;
use Tests\Acceptance\Models\User;

class CountCacheTest extends AcceptanceTestCase
{
private $data = [];

public function init()
{
$this->data = $this->setupUserAndPost();
}

function test_userHasASinglePostCount()
{
$user = User::first();
$user = User::factory()->create();

$this->assertEquals(1, $user->postCount);
Post::factory()->for($user)->create();

$this->assertEquals(1, $user->fresh()->postCount);
}

function test_whenRelatedModelsAreSwitchedBothCountCachesAreUpdated()
{
$post = new Post;
$post->userId = $this->data['user']->id;
$post->save();

$comment = new Comment;
$comment->userId = $this->data['user']->id;
$comment->postId = $this->data['post']->id;
$comment->save();
$user1 = User::factory()->create();
$user2 = User::factory()->create();
$posts = Post::factory()->count(2)->for($user1)->create();
$comment = Comment::factory()->for($user1)->for($posts->first())->create();

$this->assertEquals(2, User::first()->postCount);
$this->assertEquals(1, User::first()->commentCount);
$this->assertEquals(1, Post::first()->commentCount);
$this->assertEquals(2, $user1->fresh()->postCount);
$this->assertEquals(1, $user1->fresh()->commentCount);
$this->assertEquals(1, $posts->first()->fresh()->commentCount);

$comment = $comment->fresh();
$comment->postId = $post->id;
$comment->userId = $user2->id;
$comment->save();

$this->assertEquals(0, $this->data['post']->fresh()->commentCount);
$this->assertEquals(1, $post->fresh()->commentCount);
$this->assertEquals(0, $user1->fresh()->commentCount);
$this->assertEquals(1, $user2->fresh()->commentCount);
}

public function testItCanHandleNegativeCounts()
public function testItCanHandleModelRestoration()
{
$post = new Post;
$post->userId = $this->data['user']->id;
$post->save();
$post = Post::factory()->create();

$comment = new Comment;
$comment->userId = $this->data['user']->id;
$comment->postId = $this->data['post']->id;
$comment->save();
$comment = Comment::factory()->for($post)->create();
$comment->delete();
$comment->restore();

$this->assertEquals(1, Post::first()->commentCount);
}

private function setupUserAndPost()
{
$user = new User;
$user->firstName = 'Kirk';
$user->lastName = 'Bushell';
$user->save();

$post = new Post;
$post->userId = $user->id;
$post->save();

return compact('user', 'post');
$this->assertEquals(1, $post->fresh()->commentCount);
}
}
19 changes: 19 additions & 0 deletions tests/Acceptance/Models/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests\Acceptance\Models;

use Eloquence\Behaviours\CamelCased;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
use CamelCased;
use HasFactory;

protected static function newFactory(): Factory
{
return CategoryFactory::new();
}
}
15 changes: 15 additions & 0 deletions tests/Acceptance/Models/CategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tests\Acceptance\Models;

use Illuminate\Database\Eloquent\Factories\Factory;

class CategoryFactory extends Factory
{
protected $model = Category::class;

public function definition()
{
return [];
}
}
13 changes: 13 additions & 0 deletions tests/Acceptance/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use Eloquence\Behaviours\CountCache\Countable;
use Eloquence\Behaviours\CountCache\HasCounts;
use Eloquence\Behaviours\CamelCased;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -12,8 +14,14 @@ class Comment extends Model implements Countable
{
use CamelCased;
use HasCounts;
use HasFactory;
use SoftDeletes;

protected $fillable = [
'user_id',
'post_id',
];

public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
Expand All @@ -28,4 +36,9 @@ public function countedBy(): array
{
return ['post', 'user'];
}

protected static function newFactory(): Factory
{
return CommentFactory::new();
}
}
18 changes: 18 additions & 0 deletions tests/Acceptance/Models/CommentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tests\Acceptance\Models;

use Illuminate\Database\Eloquent\Factories\Factory;

class CommentFactory extends Factory
{
protected $model = Comment::class;

public function definition()
{
return [
'post_id' => Post::factory(),
'user_id' => User::factory(),
];
}
}
8 changes: 8 additions & 0 deletions tests/Acceptance/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
use Eloquence\Behaviours\SumCache\HasSums;
use Eloquence\Behaviours\CamelCased;
use Eloquence\Behaviours\SumCache\Summable;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Item extends Model implements Summable
{
use CamelCased;
use HasSums;
use HasFactory;

public function summedBy(): array
{
Expand All @@ -20,4 +23,9 @@ public function order()
{
return $this->belongsTo(Order::class);
}

protected static function newFactory(): Factory
{
return ItemFactory::new();
}
}
15 changes: 15 additions & 0 deletions tests/Acceptance/Models/ItemFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tests\Acceptance\Models;

use Illuminate\Database\Eloquent\Factories\Factory;

class ItemFactory extends Factory
{
protected $model = Item::class;

public function definition()
{
// TODO: Implement definition() method.
}
}
8 changes: 8 additions & 0 deletions tests/Acceptance/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@
namespace Tests\Acceptance\Models;

use Eloquence\Behaviours\CamelCased;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
use CamelCased;
use HasFactory;

public function items()
{
return $this->hasMany(Item::class);
}

protected static function newFactory(): Factory
{
return OrderFactory::new();
}
}
15 changes: 15 additions & 0 deletions tests/Acceptance/Models/OrderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tests\Acceptance\Models;

use Illuminate\Database\Eloquent\Factories\Factory;

class OrderFactory extends Factory
{
protected $model = Order::class;

public function definition()
{
// TODO: Implement definition() method.
}
}
Loading

0 comments on commit 4a4de05

Please sign in to comment.