|
1 | 1 | <?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration; |
2 | 2 |
|
3 | 3 | use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author; |
| 4 | +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\PrefixedAuthor; |
4 | 5 | use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; |
5 | 6 | use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile; |
6 | 7 | use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher; |
@@ -53,6 +54,41 @@ public function testScopeDisablesCaching() |
53 | 54 | $this->assertNotEquals($authors, $cachedResults); |
54 | 55 | } |
55 | 56 |
|
| 57 | + public function testScopeDisablesCachingWhenCalledOnModel() |
| 58 | + { |
| 59 | + $key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'); |
| 60 | + $tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor']; |
| 61 | + $authors = (new PrefixedAuthor) |
| 62 | + ->disableCache() |
| 63 | + ->where("name", "Bruno") |
| 64 | + ->get(); |
| 65 | + |
| 66 | + $cachedResults = $this->cache() |
| 67 | + ->tags($tags) |
| 68 | + ->get($key)['value']; |
| 69 | + |
| 70 | + $this->assertNull($cachedResults); |
| 71 | + $this->assertNotEquals($authors, $cachedResults); |
| 72 | + } |
| 73 | + |
| 74 | + public function testScopeDisableCacheDoesntCrashWhenCachingIsDisabledInConfig() |
| 75 | + { |
| 76 | + config(['laravel-model-caching.disabled' => true]); |
| 77 | + $key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'); |
| 78 | + $tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor']; |
| 79 | + $authors = (new PrefixedAuthor) |
| 80 | + ->where("name", "Bruno") |
| 81 | + ->disableCache() |
| 82 | + ->get(); |
| 83 | + |
| 84 | + $cachedResults = $this->cache() |
| 85 | + ->tags($tags) |
| 86 | + ->get($key)['value']; |
| 87 | + |
| 88 | + $this->assertNull($cachedResults); |
| 89 | + $this->assertNotEquals($authors, $cachedResults); |
| 90 | + } |
| 91 | + |
56 | 92 | public function testAllMethodCachingCanBeDisabledViaConfig() |
57 | 93 | { |
58 | 94 | config(['laravel-model-caching.disabled' => true]); |
@@ -98,6 +134,26 @@ public function testWhereHasIsBeingCached() |
98 | 134 | $this->assertEquals(1, $cachedResults->first()->author->id); |
99 | 135 | } |
100 | 136 |
|
| 137 | + public function testWhereHasWithClosureIsBeingCached() |
| 138 | + { |
| 139 | + $books1 = (new Book) |
| 140 | + ->with('author') |
| 141 | + ->whereHas('author', function ($query) { |
| 142 | + $query->whereId(1); |
| 143 | + }) |
| 144 | + ->get() |
| 145 | + ->keyBy('id'); |
| 146 | + $books2 = (new Book) |
| 147 | + ->with('author') |
| 148 | + ->whereHas('author', function ($query) { |
| 149 | + $query->whereId(2); |
| 150 | + }) |
| 151 | + ->get() |
| 152 | + ->keyBy('id'); |
| 153 | + |
| 154 | + $this->assertNotEmpty($books1->diffKeys($books2)); |
| 155 | + } |
| 156 | + |
101 | 157 | public function testModelCacheDoesntInvalidateDuringCooldownPeriod() |
102 | 158 | { |
103 | 159 | $authors = (new Author) |
|
0 commit comments