Skip to content

[bug] Fix current binding count when previous binding is false #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 26, 2022
5 changes: 4 additions & 1 deletion src/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ protected function getValuesFromWhere(array $where) : string

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

if (($this->query->bindings["where"][$this->currentBinding] ?? $bindingFallback) !== $bindingFallback) {
$values = $this->query->bindings["where"][$this->currentBinding];
$this->currentBinding++;

Expand Down
61 changes: 60 additions & 1 deletion tests/Integration/CachedBuilder/BooleanTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;

use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook;
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;

class BooleanTest extends IntegrationTestCase
{
public function testBooleanWhereCreatesCorrectCacheKey()
public function testBooleanWhereTrueCreatesCorrectCacheKey()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-is_famous_=_1-authors.deleted_at_null");
$tags = [
Expand All @@ -29,4 +31,61 @@ public function testBooleanWhereCreatesCorrectCacheKey()
$this->assertNotEmpty($cachedResults);
$this->assertNotEmpty($liveResults);
}

public function testBooleanWhereFalseCreatesCorrectCacheKey()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-is_famous_=_-authors.deleted_at_null");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
];

$authors = (new Author)
->where("is_famous", false)
->get();
$cachedResults = $this->cache()
->tags($tags)
->get($key)['value'];
$liveResults = (new UncachedAuthor)
->where("is_famous", false)
->get();

$this->assertEquals($liveResults->pluck("id"), $authors->pluck("id"));
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
$this->assertNotEmpty($authors);
$this->assertNotEmpty($cachedResults);
$this->assertNotEmpty($liveResults);
}

public function testBooleanWhereHasRelationWithFalseConditionAndAdditionalParentRawCondition()
{
$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");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
];

$expectedAuthor = factory(Author::class)->create(['is_famous' => false]);
factory(Book::class)->create(['author_id' => $expectedAuthor->getKey(), 'title' => 'Mixed Clause']);

$books = (new Book)
->whereHas('author', function ($query) {
return $query->where('is_famous', false);
})
->whereRaw("title = ?", ['Mixed Clause']) // Test ensures this binding is included in the key
->get();
$cachedResults = $this->cache()
->tags($tags)
->get($key)['value'];
$liveResults = (new UncachedBook)
->whereHas('author', function ($query) {
return $query->where('is_famous', false);
})
->whereRaw("title = ?", ['Mixed Clause'])
->get();

$this->assertEquals($liveResults->pluck("id"), $books->pluck("id"));
$this->assertEquals($liveResults->pluck("id"), $cachedResults->pluck("id"));
$this->assertNotEmpty($books);
$this->assertNotEmpty($cachedResults);
$this->assertNotEmpty($liveResults);
}
}