Skip to content

Commit 4480f53

Browse files
committed
Fix whereNotIn caching
1 parent f90b699 commit 4480f53

File tree

6 files changed

+145
-55
lines changed

6 files changed

+145
-55
lines changed

src/CacheKey.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function make(
3939
$key .= $this->getOffsetClause();
4040
$key .= $this->getLimitClause();
4141
$key .= $keyDifferentiator;
42-
dump($key);
42+
4343
return $key;
4444
}
4545

@@ -151,6 +151,7 @@ protected function getWhereClauses(array $wheres = []) : string
151151
$value .= $this->getColumnClauses($where);
152152
$value .= $this->getRawClauses($where);
153153
$value .= $this->getInClauses($where);
154+
$value .= $this->getNotInClauses($where);
154155
$value .= $this->getOtherClauses($where, $carry);
155156

156157
return $value;
@@ -181,7 +182,7 @@ protected function getColumnClauses(array $where) : string
181182

182183
protected function getInClauses(array $where) : string
183184
{
184-
if ($where["type"] !== "In") {
185+
if (! in_array($where["type"], ["In"])) {
185186
return "";
186187
}
187188

@@ -191,6 +192,18 @@ protected function getInClauses(array $where) : string
191192
return "-{$where["column"]}_in{$values}";
192193
}
193194

195+
protected function getNotInClauses(array $where) : string
196+
{
197+
if (! in_array($where["type"], ["NotIn"])) {
198+
return "";
199+
}
200+
201+
$this->currentBinding++;
202+
$values = $this->recursiveImplode($where["values"], "_");
203+
204+
return "-{$where["column"]}_not_in{$values}";
205+
}
206+
194207
protected function recursiveImplode(array $items, string $glue = ",") : string
195208
{
196209
$result = "";
@@ -234,7 +247,7 @@ protected function getRawClauses(array $where) : string
234247

235248
protected function getOtherClauses(array $where) : string
236249
{
237-
if (in_array($where["type"], ["Exists", "Nested", "NotExists", "Column", "raw", "In"])) {
250+
if (in_array($where["type"], ["Exists", "Nested", "NotExists", "Column", "raw", "In", "NotIn"])) {
238251
return "";
239252
}
240253

tests/CreatesApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function setUp()
4040
$publishers = factory(Publisher::class, 10)->create();
4141
factory(Author::class, 10)->create()
4242
->each(function ($author) use ($publishers) {
43-
factory(Book::class, random_int(2, 10))->make()
43+
factory(Book::class, random_int(5, 25))->make()
4444
->each(function ($book) use ($author, $publishers) {
4545
$book->author()->associate($author);
4646
$book->publisher()->associate($publishers[rand(0, 9)]);

tests/Integration/CachedBuilder/LazyLoadTest.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,34 @@
1515
use Illuminate\Foundation\Testing\RefreshDatabase;
1616
use Illuminate\Support\Collection;
1717

18-
class LazyLoadTest extends IntegrationTestCase
18+
abstract class LazyLoadTest extends IntegrationTestCase
1919
{
2020
use RefreshDatabase;
2121

22-
/** @group test */
23-
public function testLazyLoadingRelationshipQuery()
24-
{
25-
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-id_<_5');
26-
$tags = [
27-
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
28-
];
29-
$book = (new Book)
30-
->first();
31-
32-
dump("start");
33-
$stores = $book->stores;
34-
dump("end");
35-
dd($stores);
36-
$cachedResults = $this
37-
->cache()
38-
->tags($tags)
39-
->get($key)['value'];
40-
$liveResults = (new UncachedBook)
41-
->when(true, function ($query) {
42-
$query->where("id", "<", 5);
43-
})
44-
->get();
45-
46-
$this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
47-
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
48-
}
22+
// public function testLazyLoadingRelationshipQuery()
23+
// {
24+
// $key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-id_<_5');
25+
// $tags = [
26+
// 'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
27+
// ];
28+
// $book = (new Book)
29+
// ->first();
30+
//
31+
// dump("start");
32+
// $stores = $book->stores;
33+
// dump("end");
34+
// dd($stores);
35+
// $cachedResults = $this
36+
// ->cache()
37+
// ->tags($tags)
38+
// ->get($key)['value'];
39+
// $liveResults = (new UncachedBook)
40+
// ->when(true, function ($query) {
41+
// $query->where("id", "<", 5);
42+
// })
43+
// ->get();
44+
//
45+
// $this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
46+
// $this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
47+
// }
4948
}

tests/Integration/CachedBuilderPaginationTest.php renamed to tests/Integration/CachedBuilder/PaginateTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
1919
* @SuppressWarnings(PHPMD.TooManyMethods)
2020
*/
21-
class CachedBuilderPaginationTest extends IntegrationTestCase
21+
class PaginateTest extends IntegrationTestCase
2222
{
2323
use RefreshDatabase;
2424

@@ -124,4 +124,35 @@ public function testPaginationWithCustomOptionsReturnsCorrectLinks()
124124
$this->assertContains($page2ActiveLink, (string) $booksPage2->links());
125125
$this->assertContains($page24ActiveLink, (string) $booksPage24->links());
126126
}
127+
128+
public function testCustomPageNamePagination()
129+
{
130+
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_custom-page_1');
131+
$tags = [
132+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesauthor',
133+
];
134+
135+
$authors = (new Author)
136+
->paginate(3, ["*"], "custom-page");
137+
$cachedResults = $this
138+
->cache()
139+
->tags($tags)
140+
->get($key)['value'];
141+
$liveResults = (new UncachedAuthor)
142+
->paginate(3, ["*"], "custom-page");
143+
144+
$this->assertEquals($cachedResults, $authors);
145+
$this->assertEquals($liveResults->pluck("email"), $authors->pluck("email"));
146+
$this->assertEquals($liveResults->pluck("name"), $authors->pluck("name"));
147+
}
148+
149+
public function testCustomPageNamePaginationFetchesCorrectPages()
150+
{
151+
$authors1 = (new Author)
152+
->paginate(3, ["*"], "custom-page", 1);
153+
$authors2 = (new Author)
154+
->paginate(3, ["*"], "custom-page", 2);
155+
156+
$this->assertNotEquals($authors1->pluck("id"), $authors2->pluck("id"));
157+
}
127158
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 WhereNotInTest extends IntegrationTestCase
19+
{
20+
use RefreshDatabase;
21+
22+
public function testWhereNotInQuery()
23+
{
24+
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-author_id_not_in_1_2_3_4');
25+
$tags = [
26+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
27+
];
28+
$authors = (new UncachedAuthor)
29+
->where("id", "<", 5)
30+
->get(["id"]);
31+
32+
$books = (new Book)
33+
->whereNotIn("author_id", $authors)
34+
->get();
35+
$cachedResults = $this
36+
->cache()
37+
->tags($tags)
38+
->get($key)['value'];
39+
$liveResults = (new UncachedBook)
40+
->whereNotIn("author_id", $authors)
41+
->get();
42+
43+
$this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
44+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
45+
}
46+
47+
public function testWhereNotInResults()
48+
{
49+
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-id_not_in_1_2');
50+
$tags = [
51+
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
52+
];
53+
54+
$results = (new Book)
55+
->whereNotIn('id', [1, 2])
56+
->get();
57+
$cachedResults = $this->cache()
58+
->tags($tags)
59+
->get($key)['value'];
60+
$liveResults = (new UncachedBook)
61+
->whereNotIn('id', [1, 2])
62+
->get();
63+
64+
$this->assertEquals($liveResults->pluck("id"), $results->pluck("id"));
65+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
66+
}
67+
}

tests/Integration/CachedBuilderTest.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -673,30 +673,10 @@ public function testWhereDatesResults()
673673
$this->assertTrue($liveResults->diffKeys($books)->isEmpty());
674674
}
675675

676-
public function testWhereNotInResults()
677-
{
678-
$books = (new Book)
679-
->whereNotIn('id', [1, 2])
680-
->get();
681-
$key = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-id_notin_1_2');
682-
$tags = [
683-
'genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook',
684-
];
685-
686-
$cachedResults = $this->cache()
687-
->tags($tags)
688-
->get($key)['value'];
689-
$liveResults = (new UncachedAuthor)
690-
->whereNotIn('id', [1, 2])
691-
->get();
692-
693-
$this->assertTrue($cachedResults->diffKeys($books)->isEmpty());
694-
$this->assertTrue($liveResults->diffKeys($books)->isEmpty());
695-
}
696-
676+
/** @group test */
697677
public function testHashCollision()
698678
{
699-
$key1 = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-id_notin_1_2');
679+
$key1 = sha1('genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook-id_not_in_1_2');
700680
$tags1 = ['genealabs:laravel-model-caching:testing::memory::genealabslaravelmodelcachingtestsfixturesbook'];
701681
$books = (new Book)
702682
->whereNotIn('id', [1, 2])

0 commit comments

Comments
 (0)