From 57cd24c43ac8b09e702688c9e46eaffe4a768959 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Mon, 16 Oct 2017 10:42:15 -0700 Subject: [PATCH 1/4] Update test for whereHas testing --- tests/Unit/CachedBuilderTest.php | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 66d275a..ee26768 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -504,26 +504,17 @@ public function testNestedRelationshipWhereClauseParsing() public function testExistsRelationshipWhereClauseParsing() { - - $authors = collect([(new Author)->whereHas('books')->first()]); - - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_and_authors.id_=_books.author_id-first'; + $authors = (new Author)->whereHas('books') + ->get(); + $key = 'genealabslaravelmodelcachingtestsfixturesauthor_and_authors.id_=_books.author_id'; $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; - $cachedResults = collect([cache()->tags($tags)->get($key)]); - - $liveResults = collect([(new UncachedAuthor) - ->whereHas('books')->first()]); - - $this->assertTrue($authors->diffAssoc($cachedResults)->isEmpty()); - $this->assertTrue($liveResults->diffAssoc($cachedResults)->isEmpty()); - - } + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor)->whereHas('books') + ->get(); - public function testColumnsRelationshipWhereClauseParsing() - { - // ??? - $this->markTestIncomplete(); + $this->assertEmpty($authors->diffAssoc($cachedResults)); + $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } public function testRawWhereClauseParsing() From ed66393af1b09b93a0a9ed7e85ace671bc781b3c Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Mon, 16 Oct 2017 10:53:03 -0700 Subject: [PATCH 2/4] Update column where test --- tests/Unit/CachedBuilderTest.php | 40 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 0feba08..6f2a665 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -504,34 +504,38 @@ public function testNestedRelationshipWhereClauseParsing() public function testExistsRelationshipWhereClauseParsing() { + $authors = (new Author)->whereHas('books') + ->get(); - $authors = collect([(new Author)->whereHas('books')->first()]); - - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_and_authors.id_=_books.author_id-first'; + $key = 'genealabslaravelmodelcachingtestsfixturesauthor_and_authors.id_=_books.author_id'; $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; - $cachedResults = collect([cache()->tags($tags)->get($key)]); - - $liveResults = collect([(new UncachedAuthor) - ->whereHas('books')->first()]); - - $this->assertTrue($authors->diffAssoc($cachedResults)->isEmpty()); - $this->assertTrue($liveResults->diffAssoc($cachedResults)->isEmpty()); + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor)->whereHas('books') + ->get(); + $this->assertEmpty($authors->diffAssoc($cachedResults)); + $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } public function testColumnsRelationshipWhereClauseParsing() { - $author = (new Author)->orderBy('name')->first(); - - $authors = collect([(new Author)->where('name', '=', $author->name)->first()]); - - $key = 'genealabslaravelmodelcachingtestsfixturesauthor-name_' . $author->name . '-first'; + $author = (new Author) + ->orderBy('name') + ->first(); + $authors = (new Author) + ->where('name', '=', $author->name) + ->get(); + $key = 'genealabslaravelmodelcachingtestsfixturesauthor-name_' . + $author->name; $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; - $cachedResults = collect([cache()->tags($tags)->get($key)]); - - $liveResults = collect([(new UncachedAuthor)->where('name', '=', $author->name)->first()]); + $cachedResults = cache() + ->tags($tags) + ->get($key); + $liveResults = (new UncachedAuthor) + ->where('name', '=', $author->name) + ->get(); $this->assertEmpty($authors->diffAssoc($cachedResults)); $this->assertEmpty($liveResults->diffAssoc($cachedResults)); From ea7e88ad729ceefe7d84cc7699e229272721957d Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Mon, 16 Oct 2017 11:06:24 -0700 Subject: [PATCH 3/4] Add parsing for where `doesnthave()`, fixes #23 --- src/CachedBuilder.php | 4 ++-- tests/Unit/CachedBuilderTest.php | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index 6e6de17..f632ef9 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -81,8 +81,8 @@ protected function getWhereClauses(array $wheres = []) : string } return $wheres->reduce(function ($carry, $where) { - if (in_array($where['type'], ['Exists', 'Nested'])) { - return $this->getWhereClauses($where['query']->wheres); + if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) { + return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres); } if ($where['type'] === 'Column') { diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 6f2a665..df37dae 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -507,7 +507,7 @@ public function testExistsRelationshipWhereClauseParsing() $authors = (new Author)->whereHas('books') ->get(); - $key = 'genealabslaravelmodelcachingtestsfixturesauthor_and_authors.id_=_books.author_id'; + $key = 'genealabslaravelmodelcachingtestsfixturesauthor_exists_and_authors.id_=_books.author_id'; $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; $cachedResults = cache()->tags($tags)->get($key); @@ -518,6 +518,26 @@ public function testExistsRelationshipWhereClauseParsing() $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } + public function testDoesntHaveWhereClaseParsing() + { + $authors = (new Author) + ->doesntHave('books') + ->get(); + + $key = 'genealabslaravelmodelcachingtestsfixturesauthor_notexists_and_authors.id_=_books.author_id'; + $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; + + $cachedResults = cache() + ->tags($tags) + ->get($key); + $liveResults = (new UncachedAuthor) + ->doesntHave('books') + ->get(); + + $this->assertEmpty($authors->diffAssoc($cachedResults)); + $this->assertEmpty($liveResults->diffAssoc($cachedResults)); + } + public function testColumnsRelationshipWhereClauseParsing() { $author = (new Author) From a7e05646aa26d4ea160c87502f0d7fe5ca12f711 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Mon, 16 Oct 2017 11:07:48 -0700 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 608d21f..e32f0c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [0.2.7] - 2017-10-16 +### Added +- remaining unit tests that were incomplete, thanks everyone who participated! +- added parsing of where `doesnthave()` condition. + ## [0.2.6] - 2017-10-12 ### Added - orderBy clause to cache key. Thanks @RobMKR for the PR!