Skip to content

Commit e9210ff

Browse files
committed
Merge branch 'master' into develop
2 parents 7fbaf2e + 9b8fdb1 commit e9210ff

File tree

5 files changed

+83
-19
lines changed

5 files changed

+83
-19
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
}
1010
],
1111
"require": {
12+
"fico7489/laravel-pivot": "*",
1213
"illuminate/cache": "5.5 - 5.6",
1314
"illuminate/config": "5.5 - 5.6",
1415
"illuminate/console": "5.5 - 5.6",
1516
"illuminate/database": "5.5 - 5.6",
1617
"illuminate/support": "5.5 - 5.6",
17-
"php": ">=7.1.3",
18-
"fico7489/laravel-pivot": "^2.2"
18+
"php": ">=7.1.3"
1919
},
2020
"require-dev": {
2121
"codedungeon/phpunit-result-printer": "*",

src/Traits/ModelCaching.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public function newEloquentBuilder($query)
5454

5555
public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
5656
{
57-
$query = $query->disableModelCaching();
57+
if ($this->isCachable()) {
58+
$query = $query->disableModelCaching();
59+
}
5860

5961
return $query;
6062
}

tests/Feature/PaginationTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@ class PaginationTest extends FeatureTestCase
77
{
88
public function testPaginationProvidesDifferentLinksOnDifferentPages()
99
{
10+
$page1ActiveLink = starts_with(app()->version(), "5.5")
11+
? '<li class="active"><span>1</span></li>'
12+
: '<li class="page-item active"><span class="page-link">1</span></li>';
13+
$page2ActiveLink = starts_with(app()->version(), "5.5")
14+
? '<li class="active"><span>2</span></li>'
15+
: '<li class="page-item active"><span class="page-link">2</span></li>';
16+
1017
$book = (new Book)
1118
->take(11)
1219
->get()
1320
->last();
1421
$page1 = $this->visit("pagination-test");
1522

16-
$this->assertTrue(str_contains($page1->response->getContent(), '<li class="page-item active"><span class="page-link">1</span></li>'));
17-
23+
$page1->see($page1ActiveLink);
1824
$page2 = $page1->click("2");
19-
20-
$this->assertTrue(str_contains($page2->response->getContent(), '<li class="page-item active"><span class="page-link">2</span></li>'));
25+
$page2->see($page2ActiveLink);
2126
$page2->see($book->title);
2227
}
2328
}

tests/Integration/CachedBuilderPaginationTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ public function testPaginationIsCached()
4444

4545
public function testPaginationReturnsCorrectLinks()
4646
{
47+
$page1ActiveLink = starts_with(app()->version(), "5.5")
48+
? '<li class="active"><span>1</span></li>'
49+
: '<li class="page-item active"><span class="page-link">1</span></li>';
50+
$page2ActiveLink = starts_with(app()->version(), "5.5")
51+
? '<li class="active"><span>2</span></li>'
52+
: '<li class="page-item active"><span class="page-link">2</span></li>';
53+
$page24ActiveLink = starts_with(app()->version(), "5.5")
54+
? '<li class="active"><span>24</span></li>'
55+
: '<li class="page-item active"><span class="page-link">24</span></li>';
56+
4757
$booksPage1 = (new Book)
4858
->paginate(2);
4959
$booksPage2 = (new Book)
@@ -54,17 +64,8 @@ public function testPaginationReturnsCorrectLinks()
5464
$this->assertCount(2, $booksPage1);
5565
$this->assertCount(2, $booksPage2);
5666
$this->assertCount(2, $booksPage24);
57-
$this->assertContains(
58-
'<li class="page-item active"><span class="page-link">1</span></li>',
59-
(string) $booksPage1->links()
60-
);
61-
$this->assertContains(
62-
'<li class="page-item active"><span class="page-link">2</span></li>',
63-
(string) $booksPage2->links()
64-
);
65-
$this->assertContains(
66-
'<li class="page-item active"><span class="page-link">24</span></li>',
67-
(string) $booksPage24->links()
68-
);
67+
$this->assertContains($page1ActiveLink, (string) $booksPage1->links());
68+
$this->assertContains($page2ActiveLink, (string) $booksPage2->links());
69+
$this->assertContains($page24ActiveLink, (string) $booksPage24->links());
6970
}
7071
}

tests/Integration/CachedModelTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration;
22

33
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\PrefixedAuthor;
45
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
56
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
67
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
@@ -53,6 +54,41 @@ public function testScopeDisablesCaching()
5354
$this->assertNotEquals($authors, $cachedResults);
5455
}
5556

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+
5692
public function testAllMethodCachingCanBeDisabledViaConfig()
5793
{
5894
config(['laravel-model-caching.disabled' => true]);
@@ -98,6 +134,26 @@ public function testWhereHasIsBeingCached()
98134
$this->assertEquals(1, $cachedResults->first()->author->id);
99135
}
100136

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+
101157
public function testModelCacheDoesntInvalidateDuringCooldownPeriod()
102158
{
103159
$authors = (new Author)

0 commit comments

Comments
 (0)