diff --git a/CHANGELOG.md b/CHANGELOG.md index d9a483a..e35c299 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ 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.1] - 2017-09-29 +### Added +- additional unit tests for checking caching of lazy-loaded relationships. + +### Fixed +- generation of cache key for queries with where clauses. + ## [0.2.0] - 2017-09-24 ### Changed - approach to caching things. Completely rewrote the CachedBuilder class. diff --git a/src/CachedBuilder.php b/src/CachedBuilder.php index 9f53bf0..7166e50 100644 --- a/src/CachedBuilder.php +++ b/src/CachedBuilder.php @@ -76,7 +76,7 @@ protected function getWhereClauses() : string $value = $where['value'] ?? implode('_', ($where['values'] ?? [])); if (! $value) { - return ''; + return $carry . ''; } return "{$carry}-{$where['column']}_{$value}"; diff --git a/tests/Fixtures/Http/Resources/Author.php b/tests/Fixtures/Http/Resources/Author.php new file mode 100644 index 0000000..c598f77 --- /dev/null +++ b/tests/Fixtures/Http/Resources/Author.php @@ -0,0 +1,14 @@ + $this->name, + 'books' => $this->books, + ]; + } +} diff --git a/tests/Unit/CachedBuilderTest.php b/tests/Unit/CachedBuilderTest.php index 8f8552f..d8e104b 100644 --- a/tests/Unit/CachedBuilderTest.php +++ b/tests/Unit/CachedBuilderTest.php @@ -10,6 +10,7 @@ use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile; use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher; use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore; +use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Http\Resources\Author as AuthorResource; use GeneaLabs\LaravelModelCaching\Tests\TestCase; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -53,7 +54,7 @@ public function testCacheIsEmptyBeforeLoadingModels() 'genealabslaravelmodelcachingtestsfixturesauthor', 'genealabslaravelmodelcachingtestsfixturesbook' ]) - ->get('genealabslaravelmodelcachingtestsfixturesauthor_1_2_3_4_5_6_7_8_9_10-genealabslaravelmodelcachingtestsfixturesbooks'); + ->get('genealabslaravelmodelcachingtestsfixturesauthor-books'); $this->assertNull($results); } @@ -438,4 +439,34 @@ public function testNestedRelationshipEagerloading() $this->assertEmpty($authors->diffAssoc($cachedResults)); $this->assertEmpty($liveResults->diffAssoc($cachedResults)); } + + public function testLazyLoadedRelationshipResolvesThroughCachedBuilder() + { + $books = (new Author)->first()->books; + $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1'; + $tags = [ + 'genealabslaravelmodelcachingtestsfixturesbook', + ]; + + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor)->first()->books; + + $this->assertEmpty($books->diffAssoc($cachedResults)); + $this->assertEmpty($liveResults->diffAssoc($cachedResults)); + } + + public function testLazyLoadingOnResourceIsCached() + { + $books = (new AuthorResource((new Author)->first()))->books; + $key = 'genealabslaravelmodelcachingtestsfixturesbook-books.author_id_1'; + $tags = [ + 'genealabslaravelmodelcachingtestsfixturesbook', + ]; + + $cachedResults = cache()->tags($tags)->get($key); + $liveResults = (new UncachedAuthor)->first()->books; + + $this->assertEmpty($books->diffAssoc($cachedResults)); + $this->assertEmpty($liveResults->diffAssoc($cachedResults)); + } }