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! 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 0feba08..df37dae 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -504,34 +504,58 @@ 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_exists_and_authors.id_=_books.author_id'; $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; - $cachedResults = collect([cache()->tags($tags)->get($key)]); + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor)->whereHas('books') + ->get(); - $liveResults = collect([(new UncachedAuthor) - ->whereHas('books')->first()]); + $this->assertEmpty($authors->diffAssoc($cachedResults)); + $this->assertEmpty($liveResults->diffAssoc($cachedResults)); + } - $this->assertTrue($authors->diffAssoc($cachedResults)->isEmpty()); - $this->assertTrue($liveResults->diffAssoc($cachedResults)->isEmpty()); + 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)->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));