Skip to content

Commit ae3d1ec

Browse files
committed
Fix current binding count when previous binding is false
1 parent b8ffa34 commit ae3d1ec

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/CacheKey.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ protected function getValuesFromWhere(array $where) : string
174174

175175
protected function getValuesFromBindings(array $where, string $values) : string
176176
{
177-
if (($this->query->bindings["where"][$this->currentBinding] ?? false) !== false) {
177+
// Fallback to this when the current binding does not exist in the bindings array
178+
$bindingFallback = __CLASS__ . ':UNKNOWN_BINDING';
179+
180+
if (($this->query->bindings["where"][$this->currentBinding] ?? $bindingFallback) !== $bindingFallback) {
178181
$values = $this->query->bindings["where"][$this->currentBinding];
179182
$this->currentBinding++;
180183

tests/Integration/CachedBuilder/BooleanTest.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
22

33
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
45
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
57
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
68

79
class BooleanTest extends IntegrationTestCase
810
{
9-
public function testBooleanWhereCreatesCorrectCacheKey()
11+
public function testBooleanWhereTrueCreatesCorrectCacheKey()
1012
{
1113
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-is_famous_=_1-authors.deleted_at_null");
1214
$tags = [
@@ -29,4 +31,61 @@ public function testBooleanWhereCreatesCorrectCacheKey()
2931
$this->assertNotEmpty($cachedResults);
3032
$this->assertNotEmpty($liveResults);
3133
}
34+
35+
public function testBooleanWhereFalseCreatesCorrectCacheKey()
36+
{
37+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-is_famous_=_-authors.deleted_at_null");
38+
$tags = [
39+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
40+
];
41+
42+
$authors = (new Author)
43+
->where("is_famous", false)
44+
->get();
45+
$cachedResults = $this->cache()
46+
->tags($tags)
47+
->get($key)['value'];
48+
$liveResults = (new UncachedAuthor)
49+
->where("is_famous", false)
50+
->get();
51+
52+
$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
53+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
54+
$this->assertNotEmpty($authors);
55+
$this->assertNotEmpty($cachedResults);
56+
$this->assertNotEmpty($liveResults);
57+
}
58+
59+
public function testBooleanWhereHasRelationWithFalseConditionAndAdditionalParentRawCondition()
60+
{
61+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-exists-and_books.author_id_=_authors.id-is_famous_=_-authors.deleted_at_null-_and_title_=_Mixed_Clause");
62+
$tags = [
63+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
64+
];
65+
66+
$expectedAuthor = factory(Author::class)->create(['is_famous' => false]);
67+
factory(Book::class)->create(['author_id' => $expectedAuthor->getKey(), 'title' => 'Mixed Clause']);
68+
69+
$books = (new Book)
70+
->whereHas('author', function ($query) {
71+
return $query->where('is_famous', false);
72+
})
73+
->whereRaw("title = ?", ['Mixed Clause']) // Test ensures this binding is included in the key
74+
->get();
75+
$cachedResults = $this->cache()
76+
->tags($tags)
77+
->get($key)['value'];
78+
$liveResults = (new UncachedBook)
79+
->whereHas('author', function ($query) {
80+
return $query->where('is_famous', false);
81+
})
82+
->whereRaw("title = ?", ['Mixed Clause'])
83+
->get();
84+
85+
$this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
86+
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
87+
$this->assertNotEmpty($books);
88+
$this->assertNotEmpty($cachedResults);
89+
$this->assertNotEmpty($liveResults);
90+
}
3291
}

0 commit comments

Comments
 (0)