|
6 | 6 | use Rennokki\QueryCache\Test\Models\Book; |
7 | 7 | use Rennokki\QueryCache\Test\Models\Kid; |
8 | 8 | use Rennokki\QueryCache\Test\Models\Post; |
| 9 | +use Rennokki\QueryCache\Test\Models\User; |
9 | 10 |
|
10 | 11 | class MethodsTest extends TestCase |
11 | 12 | { |
@@ -123,4 +124,50 @@ public function test_hashed_key() |
123 | 124 |
|
124 | 125 | $this->assertNotNull($cache); |
125 | 126 | } |
| 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 | + } |
126 | 173 | } |
0 commit comments