From 049b5cfa9ab3c7f433c71a1986245664d09f1ccd Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sun, 19 Nov 2017 09:24:22 -0800 Subject: [PATCH 1/2] Add test of query scopes in cache key --- tests/Fixtures/Author.php | 6 ++++++ tests/Fixtures/UncachedAuthor.php | 6 ++++++ tests/Unit/CachedBuilderTest.php | 26 ++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/Fixtures/Author.php b/tests/Fixtures/Author.php index 3edfbaa..e4aecc1 100644 --- a/tests/Fixtures/Author.php +++ b/tests/Fixtures/Author.php @@ -1,6 +1,7 @@ hasOne(Profile::class); } + + public function scopeStartsWithA(Builder $query) : Builder + { + return $query->where('name', 'LIKE', 'A%'); + } } diff --git a/tests/Fixtures/UncachedAuthor.php b/tests/Fixtures/UncachedAuthor.php index 0dca539..e5a5d03 100644 --- a/tests/Fixtures/UncachedAuthor.php +++ b/tests/Fixtures/UncachedAuthor.php @@ -1,5 +1,6 @@ hasOne(UncachedProfile::class, 'author_id', 'id'); } + + public function scopeStartsWithA(Builder $query) : Builder + { + return $query->where('name', 'LIKE', 'A%'); + } } diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index db1a00f..1d30b50 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -537,7 +537,7 @@ public function testExistsRelationshipWhereClauseParsing() $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } - public function testDoesntHaveWhereClaseParsing() + public function testDoesntHaveWhereClauseParsing() { $authors = (new Author) ->doesntHave('books') @@ -583,7 +583,8 @@ public function testColumnsRelationshipWhereClauseParsing() public function testRawWhereClauseParsing() { $authors = collect([(new Author) - ->whereRaw('name <> \'\'')->first()]); + ->whereRaw('name <> \'\'') + ->first()]); $key = 'genealabslaravelmodelcachingtestsfixturesauthor_and_name-first'; $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; @@ -596,4 +597,25 @@ public function testRawWhereClauseParsing() $this->assertTrue($authors->diffAssoc($cachedResults)->isEmpty()); $this->assertTrue($liveResults->diffAssoc($cachedResults)->isEmpty()); } + + public function testScopeClauseParsing() + { + $author = factory(Author::class, 1) + ->create(['name' => 'Anton']) + ->first(); + $authors = (new Author) + ->startsWithA() + ->get(); + $key = 'genealabslaravelmodelcachingtestsfixturesauthor-name_A%'; + $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; + + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor) + ->startsWithA() + ->get(); + + $this->assertTrue($authors->contains($author)); + $this->assertTrue($cachedResults->contains($author)); + $this->assertTrue($liveResults->contains($author)); + } } From 5b084ce8e74ddaf4fb9a70980f4a802e4625d165 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sun, 19 Nov 2017 09:31:20 -0800 Subject: [PATCH 2/2] Add test to check that nested relationship queries are cached --- tests/Unit/CachedBuilderTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 1d30b50..dbbe3bc 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -618,4 +618,25 @@ public function testScopeClauseParsing() $this->assertTrue($cachedResults->contains($author)); $this->assertTrue($liveResults->contains($author)); } + + public function testRelationshipQueriesAreCached() + { + $books = (new Author) + ->first() + ->books() + ->get(); + $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1-books.author_id_notnull'; + $tags = [ + 'genealabslaravelmodelcachingtestsfixturesbook' + ]; + + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor) + ->first() + ->books() + ->get(); + + $this->assertTrue($cachedResults->diffAssoc($books)->isEmpty()); + $this->assertTrue($liveResults->diffAssoc($books)->isEmpty()); + } }