Skip to content
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);
}
}