Skip to content

Commit 863c84d

Browse files
committed
Append cache tests
1 parent bda4eaa commit 863c84d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

tests/MethodsTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Rennokki\QueryCache\Test\Models\Book;
77
use Rennokki\QueryCache\Test\Models\Kid;
88
use Rennokki\QueryCache\Test\Models\Post;
9+
use Rennokki\QueryCache\Test\Models\User;
910

1011
class MethodsTest extends TestCase
1112
{
@@ -123,4 +124,50 @@ public function test_hashed_key()
123124

124125
$this->assertNotNull($cache);
125126
}
127+
128+
public function test_append_cache_tags()
129+
{
130+
$post = factory(Post::class)->create();
131+
$storedPost = Post::cacheFor(now()->addHours(1))->appendCacheTags(['test'])->first();
132+
133+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
134+
135+
// The caches that do not support tagging should
136+
// cache the query either way.
137+
$this->driverSupportsTags()
138+
? $this->assertNull($cache)
139+
: $this->assertNotNull($cache);
140+
141+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
142+
$this->assertNotNull($cache);
143+
}
144+
145+
public function test_multiple_append_cache_tags()
146+
{
147+
$post = factory(Post::class)->create();
148+
$storedPostQuery = Post::cacheFor(now()->addHours(1))->appendCacheTags(['test'])->appendCacheTags(['test2']);
149+
150+
$this->assertEquals($storedPostQuery->getQuery()->getCacheTags(), ['test', 'test2']);
151+
}
152+
153+
public function test_append_cache_tags_with_sub_query()
154+
{
155+
$user = factory(User::class)->create();
156+
factory(Post::class)->createMany([
157+
['user_id' => $user->id, 'name' => 'Post 1 on topic 1'],
158+
['user_id' => $user->id, 'name' => 'Post 2 on topic 1'],
159+
['user_id' => $user->id, 'name' => 'Post 3 on topic 2'],
160+
]);
161+
162+
$userAndPosts = User::cacheFor(now()->addHours(1))
163+
->withCount([
164+
'posts' => function ($query) {
165+
$query->appendCacheTags(['posts'])
166+
->where('name', 'like', '%topic 1%');
167+
},
168+
])
169+
->appendCacheTags(['user']);
170+
171+
$this->assertEquals($userAndPosts->getQuery()->getCacheTags(), ['posts', 'user']);
172+
}
126173
}

tests/Models/User.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Rennokki\QueryCache\Test\Models;
44

55
use Illuminate\Foundation\Auth\User as Authenticatable;
6+
use Rennokki\QueryCache\Traits\QueryCacheable;
67

78
class User extends Authenticatable
89
{
10+
use QueryCacheable;
11+
912
protected $fillable = [
1013
'name', 'email', 'password',
1114
];
@@ -20,4 +23,9 @@ protected function getCacheBaseTags(): array
2023
//
2124
];
2225
}
26+
27+
public function posts()
28+
{
29+
return $this->hasMany(Post::class);
30+
}
2331
}

tests/database/migrations/2018_07_14_183253_posts.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function up()
1515
{
1616
Schema::create('posts', function (Blueprint $table) {
1717
$table->increments('id');
18+
$table->unsignedBigInteger('user_id')->nullable();
1819
$table->string('name');
1920
$table->timestamps();
2021
});

0 commit comments

Comments
 (0)