Skip to content

Commit 8606561

Browse files
authored
Merge pull request #4 from rennokki/increase-code-coverage
Increase the code coverage
2 parents 39f003d + 02ec858 commit 8606561

File tree

9 files changed

+162
-22
lines changed

9 files changed

+162
-22
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ env:
88
matrix:
99
- COMPOSER_FLAGS=""
1010

11+
cache:
12+
directories:
13+
- $HOME/.composer/cache
14+
1115
before_script:
1216
- travis_retry composer self-update
1317
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,6 @@ $alice = Kid::whereName('Alice')->cacheFor(60)->cacheTags(['kids'])->first();
8787
$bob = Kid::whereName('Bob')->cacheFor(60)->cacheTags(['kids'])->first();
8888
```
8989

90-
In case you want to invalidate all the cache, don't specify an argument for the `flushQueryCache()` method:
91-
92-
```php
93-
Problem::flushQueryCache(); // bye-bye problems!
94-
```
95-
9690
## Relationship Caching
9791
Relationships are just another queries. They can be intercepted and modified before the database is hit with the query. The following example needs the `Order` model (or the model associated with the `orders` relationship) to include the `QueryCacheable` trait.
9892

@@ -109,7 +103,7 @@ $orders = $user->orders;
109103
The package automatically generate the keys needed to store the data in the cache store. However, prefixing them might be useful if the cache store is used by other applications and/or models and you want to manage the keys better to avoid collisions.
110104

111105
```php
112-
$bob = Kid::whereName('Bob')->cacheFor(60)->prefix('kids_')->first();
106+
$bob = Kid::whereName('Bob')->cacheFor(60)->cachePrefix('kids_')->first();
113107
```
114108

115109
If no prefix is specified, the string `leqc` is going to be used.

database/factories/KidFactory.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/*
3+
|--------------------------------------------------------------------------
4+
| Model Factories
5+
|--------------------------------------------------------------------------
6+
|
7+
| This directory should contain each of the model factory definitions for
8+
| your application. Factories provide a convenient way to generate new
9+
| model instances for testing / seeding your application's database.
10+
|
11+
*/
12+
13+
use Illuminate\Support\Str;
14+
15+
$factory->define(\Rennokki\QueryCache\Test\Models\Kid::class, function () {
16+
return [
17+
'name' => 'Kid'.Str::random(5),
18+
];
19+
});

src/Query/Builder.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ protected function getFromQueryCache(string $method = 'get', $columns = ['*'])
7878
}
7979

8080
$key = $this->getCacheKey('get');
81-
$seconds = $this->cacheTime;
8281
$cache = $this->getCache();
8382
$callback = $this->getQueryCacheCallback($method, $columns);
8483

@@ -101,10 +100,6 @@ protected function getQueryCacheCallback(string $method = 'get', $columns = ['*'
101100
return function () use ($method, $columns) {
102101
$this->avoidCache = true;
103102

104-
if ($method === 'get') {
105-
return $this->get($columns);
106-
}
107-
108103
return $this->{$method}($columns);
109104
};
110105
}
@@ -170,7 +165,7 @@ public function generatePlainCacheKey(string $method = 'get', $id = null, $appen
170165
* @param array $tags
171166
* @return bool
172167
*/
173-
public function flushQueryCache(array $tags = []): bool
168+
public function flushQueryCache(array $tags = ['leqc']): bool
174169
{
175170
$cache = $this->getCacheDriver();
176171

@@ -206,25 +201,24 @@ protected function getCache()
206201
/**
207202
* Indicate that the query results should be cached.
208203
*
209-
* @param \DateTime|int $seconds
204+
* @param \DateTime|int $time
210205
* @return \Rennokki\QueryCache\Query\Builder
211206
*/
212-
public function cacheFor($seconds)
207+
public function cacheFor($time)
213208
{
214-
$this->cacheTime = $seconds;
209+
$this->cacheTime = $time;
215210

216211
return $this;
217212
}
218213

219214
/**
220215
* Indicate that the query results should be cached forever.
221216
*
222-
* @param string|null $key
223217
* @return \Illuminate\Database\Query\Builder|static
224218
*/
225-
public function cacheForever($key = null)
219+
public function cacheForever()
226220
{
227-
return $this->cacheFor(-1, $key);
221+
return $this->cacheFor(-1);
228222
}
229223

230224
/**
@@ -234,7 +228,7 @@ public function cacheForever($key = null)
234228
*/
235229
public function dontCache()
236230
{
237-
$this->cacheTime = $this->cacheTags = null;
231+
$this->avoidCache = true;
238232

239233
return $this;
240234
}
@@ -255,7 +249,7 @@ public function doNotCache()
255249
* @param string $prefix
256250
* @return \Rennokki\QueryCache\Query\Builder
257251
*/
258-
public function prefix(string $prefix)
252+
public function cachePrefix(string $prefix)
259253
{
260254
$this->cachePrefix = $prefix;
261255

src/Traits/QueryCacheable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function newBaseQueryBuilder()
2828
}
2929

3030
if ($this->cachePrefix) {
31-
$builder->prefix($this->cachePrefix);
31+
$builder->cachePrefix($this->cachePrefix);
3232
}
3333

3434
if ($this->cacheDriver) {

tests/MethodsTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Rennokki\QueryCache\Test;
4+
5+
use Cache;
6+
use Rennokki\QueryCache\Test\Models\Kid;
7+
use Rennokki\QueryCache\Test\Models\Post;
8+
9+
class MethodsTest extends TestCase
10+
{
11+
public function test_do_not_cache()
12+
{
13+
$post = factory(Post::class)->create();
14+
15+
$storedPost = Post::cacheFor(now()->addHours(1))->doNotCache()->first();
16+
$cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
17+
$this->assertNull($cache);
18+
19+
$storedPost = Post::cacheFor(now()->addHours(1))->dontCache()->first();
20+
$cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
21+
$this->assertNull($cache);
22+
}
23+
24+
public function test_cache_prefix()
25+
{
26+
$post = factory(Post::class)->create();
27+
$storedPost = Post::cacheFor(now()->addHours(1))->cachePrefix('test')->first();
28+
$cache = Cache::get('test:sqlitegetselect * from "posts" limit 1a:0:{}');
29+
30+
$this->assertNotNull($cache);
31+
}
32+
33+
public function test_cache_tags()
34+
{
35+
$post = factory(Post::class)->create();
36+
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
37+
38+
$cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
39+
$this->assertNull($cache);
40+
41+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
42+
$this->assertNotNull($cache);
43+
}
44+
45+
public function test_cache_flush_with_the_right_tag()
46+
{
47+
$post = factory(Post::class)->create();
48+
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
49+
50+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
51+
$this->assertNotNull($cache);
52+
53+
Post::flushQueryCache(['test']);
54+
55+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
56+
$this->assertNull($cache);
57+
}
58+
59+
public function test_cache_flush_without_the_right_tag()
60+
{
61+
$post = factory(Post::class)->create();
62+
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
63+
64+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
65+
$this->assertNotNull($cache);
66+
67+
Post::flushQueryCache(['test2']);
68+
69+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
70+
$this->assertNotNull($cache);
71+
}
72+
73+
public function test_hashed_key()
74+
{
75+
$kid = factory(Kid::class)->create();
76+
$storedKid = Kid::cacheFor(now()->addHours(1))->first();
77+
$cache = Cache::get('leqc:156667fa9bcb7fb8abb01018568648406f251ef65736e89e6fd27d08bc48b5bb');
78+
79+
$this->assertNotNull($cache);
80+
}
81+
}

tests/Models/Kid.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rennokki\QueryCache\Test\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Rennokki\QueryCache\Traits\QueryCacheable;
7+
8+
class Kid extends Model
9+
{
10+
use QueryCacheable;
11+
12+
protected $fillable = [
13+
'name',
14+
];
15+
}

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function getEnvironmentSetUp($app)
3838
]);
3939
$app['config']->set('auth.providers.users.model', User::class);
4040
$app['config']->set('auth.providers.posts.model', Post::class);
41+
$app['config']->set('auth.providers.kids.model', Kid::class);
4142
$app['config']->set('app.key', 'wslxrEFGWY6GfGhvN9L3wH3KSRJQQpBD');
4243
}
4344

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class Kids extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('kids', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->string('name');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('kids');
31+
}
32+
}

0 commit comments

Comments
 (0)