Skip to content

Commit 3123118

Browse files
committed
Update dependencies, fix find using array
Fixes #125
1 parent d45a417 commit 3123118

File tree

6 files changed

+157
-47
lines changed

6 files changed

+157
-47
lines changed

src/CachedBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public function find($id, $columns = ["*"])
5252
return parent::find($id, $columns);
5353
}
5454

55-
$cacheKey = $this->makeCacheKey($columns, null, "-find_{$id}");
55+
$idKey = collect($id)->implode('-');
56+
$cacheKey = $this->makeCacheKey($columns, null, "-find_{$idKey}");
5657

5758
return $this->cachedValue(func_get_args(), $cacheKey);
5859
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
10+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
11+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher;
12+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore;
13+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource;
14+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
15+
use Illuminate\Foundation\Testing\RefreshDatabase;
16+
use Illuminate\Support\Collection;
17+
18+
class FindOrFailTest extends IntegrationTestCase
19+
{
20+
use RefreshDatabase;
21+
22+
public function testFindOrFailCachesModels()
23+
{
24+
$author = (new Author)
25+
->findOrFail(1);
26+
27+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-find_1');
28+
$tags = [
29+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
30+
];
31+
32+
$cachedResults = $this->cache()
33+
->tags($tags)
34+
->get($key)['value'];
35+
$liveResults = (new UncachedAuthor)
36+
->findOrFail(1);
37+
38+
$this->assertEquals($cachedResults->toArray(), $author->toArray());
39+
$this->assertEquals($liveResults->toArray(), $author->toArray());
40+
}
41+
42+
public function testFindOrFailWithArrayReturnsResults()
43+
{
44+
$author = (new Author)->findOrFail([1, 2]);
45+
$uncachedAuthor = (new UncachedAuthor)->findOrFail([1, 2]);
46+
47+
$this->assertEquals($uncachedAuthor->count(), $author->count());
48+
$this->assertEquals($uncachedAuthor->pluck("id"), $author->pluck("id"));
49+
}
50+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
8+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
10+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile;
11+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher;
12+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore;
13+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource;
14+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
15+
use Illuminate\Foundation\Testing\RefreshDatabase;
16+
use Illuminate\Support\Collection;
17+
18+
class FindTest extends IntegrationTestCase
19+
{
20+
use RefreshDatabase;
21+
22+
public function testFindModelResultsCreatesCache()
23+
{
24+
$author = collect()->push((new Author)->find(1));
25+
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor_1');
26+
$tags = [
27+
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
28+
];
29+
30+
$cachedResults = collect()->push($this->cache()->tags($tags)
31+
->get($key));
32+
$liveResults = collect()->push((new UncachedAuthor)->find(1));
33+
34+
$this->assertEmpty($author->diffKeys($cachedResults));
35+
$this->assertEmpty($liveResults->diffKeys($cachedResults));
36+
}
37+
38+
public function testSubsequentFindsReturnDifferentModels()
39+
{
40+
$author1 = (new Author)->find(1);
41+
$author2 = (new Author)->find(2);
42+
43+
$this->assertNotEquals($author1, $author2);
44+
$this->assertEquals($author1->id, 1);
45+
$this->assertEquals($author2->id, 2);
46+
}
47+
48+
public function testFindWithArrayReturnsResults()
49+
{
50+
$author = (new Author)->find([1, 2]);
51+
$uncachedAuthor = (new UncachedAuthor)->find([1, 2]);
52+
53+
$this->assertEquals($uncachedAuthor->count(), $author->count());
54+
$this->assertEquals($uncachedAuthor->pluck("id"), $author->pluck("id"));
55+
}
56+
}

tests/Integration/CachedBuilder/WithTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,26 @@ public function testWithQuery()
4040
$this->assertEquals($uncachedAuthor->books()->count(), $author->books()->count());
4141
$this->assertEquals($uncachedAuthor->id, $author->id);
4242
}
43+
44+
public function testMultiLevelWithQuery()
45+
{
46+
$author = (new Author)
47+
->where("id", 1)
48+
->with([
49+
'books.publisher' => function ($query) {
50+
$query->where("id", "<", 100);
51+
}
52+
])
53+
->first();
54+
$uncachedAuthor = (new UncachedAuthor)->with([
55+
'books.publisher' => function ($query) {
56+
$query->where("id", "<", 100);
57+
},
58+
])
59+
->where("id", 1)
60+
->first();
61+
62+
$this->assertEquals($uncachedAuthor->books()->count(), $author->books()->count());
63+
$this->assertEquals($uncachedAuthor->id, $author->id);
64+
}
4365
}

tests/Integration/CachedBuilderPaginationTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,31 @@ public function testPaginationWithOptionsReturnsCorrectLinks()
9797
$this->assertContains($page2ActiveLink, (string) $booksPage2->links());
9898
$this->assertContains($page24ActiveLink, (string) $booksPage24->links());
9999
}
100+
101+
public function testPaginationWithCustomOptionsReturnsCorrectLinks()
102+
{
103+
$page1ActiveLink = starts_with(app()->version(), "5.5")
104+
? '<li class="active"><span>1</span></li>'
105+
: '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
106+
$page2ActiveLink = starts_with(app()->version(), "5.5")
107+
? '<li class="active"><span>2</span></li>'
108+
: '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
109+
$page24ActiveLink = starts_with(app()->version(), "5.5")
110+
? '<li class="active"><span>24</span></li>'
111+
: '<li class="page-item active" aria-current="page"><span class="page-link">24</span></li>';
112+
113+
$booksPage1 = (new Book)
114+
->paginate('2');
115+
$booksPage2 = (new Book)
116+
->paginate('2', ['*'], 'pages', 2);
117+
$booksPage24 = (new Book)
118+
->paginate('2', ['*'], 'pages', 24);
119+
120+
$this->assertCount(2, $booksPage1);
121+
$this->assertCount(2, $booksPage2);
122+
$this->assertCount(2, $booksPage24);
123+
$this->assertContains($page1ActiveLink, (string) $booksPage1->links());
124+
$this->assertContains($page2ActiveLink, (string) $booksPage2->links());
125+
$this->assertContains($page24ActiveLink, (string) $booksPage24->links());
126+
}
100127
}

tests/Integration/CachedBuilderTest.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -265,22 +265,6 @@ public function testCountWithStringCreatesCache()
265265
$this->assertEquals($liveResults, $cachedResults);
266266
}
267267

268-
public function testFindModelResultsCreatesCache()
269-
{
270-
$author = collect()->push((new Author)->find(1));
271-
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor_1');
272-
$tags = [
273-
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
274-
];
275-
276-
$cachedResults = collect()->push($this->cache()->tags($tags)
277-
->get($key));
278-
$liveResults = collect()->push((new UncachedAuthor)->find(1));
279-
280-
$this->assertEmpty($author->diffKeys($cachedResults));
281-
$this->assertEmpty($liveResults->diffKeys($cachedResults));
282-
}
283-
284268
public function testFirstModelResultsCreatesCache()
285269
{
286270
$author = (new Author)
@@ -783,36 +767,6 @@ public function testSubsequentDisabledCacheQueriesDoNotCache()
783767
$this->assertEmpty($cachedAuthors2);
784768
}
785769

786-
public function testSubsequentFindsReturnDifferentModels()
787-
{
788-
$author1 = (new Author)->find(1);
789-
$author2 = (new Author)->find(2);
790-
791-
$this->assertNotEquals($author1, $author2);
792-
$this->assertEquals($author1->id, 1);
793-
$this->assertEquals($author2->id, 2);
794-
}
795-
796-
public function testFindOrFailCachesModels()
797-
{
798-
$author = (new Author)
799-
->findOrFail(1);
800-
801-
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-find_1');
802-
$tags = [
803-
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
804-
];
805-
806-
$cachedResults = $this->cache()
807-
->tags($tags)
808-
->get($key)['value'];
809-
$liveResults = (new UncachedAuthor)
810-
->findOrFail(1);
811-
812-
$this->assertEquals($cachedResults->toArray(), $author->toArray());
813-
$this->assertEquals($liveResults->toArray(), $author->toArray());
814-
}
815-
816770
public function testInsertInvalidatesCache()
817771
{
818772
$authors = (new Author)

0 commit comments

Comments
 (0)