Skip to content

Commit f88c29e

Browse files
author
Corey McCormick
committed
Correctly set up overriding of traits
1 parent 62ce0db commit f88c29e

File tree

2 files changed

+86
-11
lines changed

2 files changed

+86
-11
lines changed

src/Cacheable.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,41 @@
55
trait Cacheable
66
{
77
/**
8-
* Configures the cache store to be used.
8+
* Returns the Cache store to use.
99
*
10-
* @var
10+
* @return null|string
1111
*/
12-
protected $cacheStore;
12+
public function getCacheStore()
13+
{
14+
if (property_exists($this, 'cacheStore')) return $this->cacheStore;
15+
16+
return null;
17+
}
1318

1419
/**
15-
* Determines the length to cache a result.
20+
* Returns if the cache should be busted
21+
* on inserts/updates/delete.
1622
*
17-
* @var int
23+
* @return bool
1824
*/
19-
protected $cacheLength = 30;
25+
public function getCacheBusting()
26+
{
27+
if (property_exists($this, 'cacheBusting')) return $this->cacheBusting;
28+
29+
return false;
30+
}
2031

2132
/**
22-
* Determines if the cache should be busted
23-
* on inserts/updates/deletes.
33+
* Gets the length of the cache. Defaults to 30 minutes.
2434
*
25-
* @var bool
35+
* @return int
2636
*/
27-
protected $cacheBusting = false;
37+
public function getCacheLength()
38+
{
39+
if (property_exists($this, 'cacheLength')) return $this->cacheLength;
40+
41+
return 30;
42+
}
2843

2944
/**
3045
* Overrides the default QueryBuilder to inject the Cache methods.
@@ -47,7 +62,7 @@ protected function newBaseQueryBuilder()
4762
*/
4863
protected function queryCache()
4964
{
50-
return new QueryCache($this->cacheStore, $this->cacheLength);
65+
return new QueryCache($this->cacheStore, $this->getCacheLength());
5166
}
5267

5368
/**

tests/CacheableTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,66 @@ public function tearDown()
1616
Mockery::close();
1717
}
1818

19+
/** @test */
20+
public function the_correct_cache_store_is_returned_when_a_cache_store_is_set()
21+
{
22+
$model = new class extends EloquentModel {
23+
use Cacheable;
24+
25+
protected $cacheStore= 'fooStore';
26+
};
27+
28+
$this->assertEquals('fooStore', $model->getCacheStore());
29+
}
30+
31+
/** @test */
32+
public function null_is_returned_when_a_cache_store_is_not_set()
33+
{
34+
$model = new Model;
35+
36+
$this->assertNull($model->getCacheStore());
37+
}
38+
39+
/** @test */
40+
public function cache_busting_returns_true_when_cache_busting_is_set()
41+
{
42+
$model = new class extends EloquentModel {
43+
use Cacheable;
44+
45+
protected $cacheBusting = true;
46+
};
47+
48+
$this->assertTrue($model->getCacheBusting());
49+
}
50+
51+
/** @test */
52+
public function cache_busting_returns_false_when_no_cache_busting_is_set()
53+
{
54+
$model = new Model;
55+
56+
$this->assertFalse($model->getCacheBusting());
57+
}
58+
59+
/** @test */
60+
public function thirty_minutes_are_returned_when_no_cache_length_is_set()
61+
{
62+
$model = new Model();
63+
64+
$this->assertEquals(30, $model->getCacheLength());
65+
}
66+
67+
/** @test */
68+
public function the_correct_length_is_returned_when_a_length_is_set()
69+
{
70+
$model = new class extends EloquentModel {
71+
use Cacheable;
72+
73+
protected $cacheLength = 45;
74+
};
75+
76+
$this->assertEquals(45, $model->getCacheLength());
77+
}
78+
1979
/** @test */
2080
public function insert_and_update_bust_the_cache_when_busting_is_enabled()
2181
{

0 commit comments

Comments
 (0)