Skip to content

Commit 1e477a1

Browse files
committed
WIP
1 parent 17b905c commit 1e477a1

File tree

8 files changed

+117
-1
lines changed

8 files changed

+117
-1
lines changed

src/CacheKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function make(
3939
$key .= $this->getOffsetClause();
4040
$key .= $this->getLimitClause();
4141
$key .= $keyDifferentiator;
42-
42+
dump("key", $key);
4343
return $key;
4444
}
4545

src/Traits/ModelCaching.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public static function all($columns = ['*'])
2525

2626
public static function bootCachable()
2727
{
28+
static::created(function ($instance) {
29+
$instance->checkCooldownAndFlushAfterPersiting($instance);
30+
});
31+
2832
static::deleted(function ($instance) {
2933
$instance->checkCooldownAndFlushAfterPersiting($instance);
3034
});
@@ -33,6 +37,10 @@ public static function bootCachable()
3337
$instance->checkCooldownAndFlushAfterPersiting($instance);
3438
});
3539

40+
static::restored(function ($instance) {
41+
$instance->checkCooldownAndFlushAfterPersiting($instance);
42+
});
43+
3644
static::pivotAttached(function ($instance) {
3745
$instance->checkCooldownAndFlushAfterPersiting($instance);
3846
});

tests/Fixtures/Book.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
44
use Illuminate\Database\Eloquent\Model;
55
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
use Illuminate\Database\Eloquent\Relations\MorphMany;
67
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
78

89
class Book extends Model
@@ -27,6 +28,11 @@ public function author() : BelongsTo
2728
return $this->belongsTo(Author::class);
2829
}
2930

31+
public function comments() : MorphMany
32+
{
33+
return $this->morphMany(Comment::class, "commentable");
34+
}
35+
3036
public function publisher() : BelongsTo
3137
{
3238
return $this->belongsTo(Publisher::class);

tests/Fixtures/Comment.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\MorphTo;
6+
7+
class Comment extends Model
8+
{
9+
use Cachable;
10+
11+
protected $fillable = [
12+
'description',
13+
'subject',
14+
];
15+
16+
public function commentable() : MorphTo
17+
{
18+
return $this->morphTo();
19+
}
20+
}

tests/Fixtures/UncachedComment.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\MorphTo;
6+
7+
class UncachedComment extends Model
8+
{
9+
protected $fillable = [
10+
'description',
11+
'subject',
12+
];
13+
protected $table = "comments";
14+
15+
public function commentable() : MorphTo
16+
{
17+
return $this->morphTo();
18+
}
19+
}

tests/Integration/CachedBuilder/WithCountTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
44
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Comment;
56
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
67
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
78
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
89
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
910
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
11+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedComment;
1012
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
1113
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher;
1214
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore;
@@ -39,4 +41,29 @@ public function testWithCountUpdatesAfterRecordIsAdded()
3941
$this->assertNotEquals($author1->books_count, $author2->books_count);
4042
$this->assertEquals($author1->books_count + 1, $author2->books_count);
4143
}
44+
45+
/** @group test */
46+
public function testWithCountOnMorphManyRelationshipUpdatesAfterRecordIsAdded()
47+
{
48+
$book1 = (new Book)
49+
->withCount("comments")
50+
->first();
51+
dump("start");
52+
factory(Comment::class, 1)
53+
->make()
54+
->each(function ($comment) use ($book1) {
55+
$comment->commentable_id = $book1->id;
56+
$comment->commentable_type = $book1->getTable();
57+
$comment->save();
58+
});
59+
dump("end");
60+
61+
$book2 = (new Book)
62+
->withCount("comments")
63+
->where("id", $book1->id)
64+
->first();
65+
66+
$this->assertNotEquals($book1->comments_count, $book2->comments_count);
67+
$this->assertEquals($book1->comments_count + 1, $book2->comments_count);
68+
}
4269
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Comment;
5+
6+
$factory->define(Comment::class, function (Faker $faker) {
7+
return [
8+
'description' => $faker->paragraphs(3, true),
9+
'subject' => $faker->sentence,
10+
];
11+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateComments extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('comments', function (Blueprint $table) {
11+
$table->increments('id');
12+
$table->timestamps();
13+
14+
$table->unsignedInteger("commentable_id");
15+
$table->string("commentable_type");
16+
$table->text("description");
17+
$table->string("subject");
18+
});
19+
}
20+
21+
public function down()
22+
{
23+
//
24+
}
25+
}

0 commit comments

Comments
 (0)