From cb1e9a70bfe9cbe759593c39054db87ac150cf31 Mon Sep 17 00:00:00 2001 From: dmason30 Date: Fri, 10 Apr 2020 15:12:55 +0100 Subject: [PATCH] Add whereHasMorph test --- .../CachedBuilder/WhereHasMorphTest.php | 78 +++++++++++++++++++ tests/database/seeds/DatabaseSeeder.php | 2 +- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/CachedBuilder/WhereHasMorphTest.php diff --git a/tests/Integration/CachedBuilder/WhereHasMorphTest.php b/tests/Integration/CachedBuilder/WhereHasMorphTest.php new file mode 100644 index 0000000..7b2827a --- /dev/null +++ b/tests/Integration/CachedBuilder/WhereHasMorphTest.php @@ -0,0 +1,78 @@ +whereHasMorph('commentable', Post::class) + ->get(); + $uncachedComments = (new UncachedComment()) + ->whereHasMorph('commentable', Post::class) + ->get(); + + $cacheResults = $this->cache()->tags([ + "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment", + ]) + ->get(sha1( + "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:comments:genealabslaravelmodelcachingtestsfixturescomment-nested-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post-exists-and_comments.commentable_id_=_posts.id" + ))['value']; + + $this->assertCount(5, $comments); + $this->assertEquals($comments->pluck("id"), $uncachedComments->pluck("id")); + $this->assertEquals($uncachedComments->pluck("id"), $cacheResults->pluck("id")); + } + + public function testWithMultipleMorphModels() + { + $comments = (new Comment) + ->whereHasMorph('commentable', [Post::class, UncachedPost::class]) + ->get(); + $uncachedComments = (new UncachedComment()) + ->whereHasMorph('commentable', [Post::class, UncachedPost::class]) + ->get(); + + $cacheResults = $this->cache()->tags([ + "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment", + ]) + ->get(sha1( + "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:comments:genealabslaravelmodelcachingtestsfixturescomment-nested-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post-exists-and_comments.commentable_id_=_posts.id-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost-exists-and_comments.commentable_id_=_posts.id" + ))['value']; + + $this->assertCount(10, $comments); + $this->assertEquals($comments->pluck("id"), $uncachedComments->pluck("id")); + $this->assertEquals($uncachedComments->pluck("id"), $cacheResults->pluck("id")); + } + + public function testWithMultipleMorphModelsWithClosure() + { + $comments = (new Comment) + ->whereHasMorph('commentable', [Post::class, UncachedPost::class], function (Builder $query) { + return $query->where('subject', 'like', '%uncached post'); + }) + ->get(); + $uncachedComments = (new UncachedComment()) + ->whereHasMorph('commentable', [Post::class, UncachedPost::class], function (Builder $query) { + return $query->where('subject', 'like', '%uncached post'); + }) + ->get(); + + $cacheResults = $this->cache()->tags([ + "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment", + ]) + ->get(sha1( + "genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:comments:genealabslaravelmodelcachingtestsfixturescomment-nested-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post-exists-and_comments.commentable_id_=_posts.id-subject_like_%uncached post-nested-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost-exists-and_comments.commentable_id_=_posts.id-subject_like_%uncached post" + ))['value']; + + $this->assertCount(5, $comments); + $this->assertEquals($comments->pluck("id"), $uncachedComments->pluck("id")); + $this->assertEquals($uncachedComments->pluck("id"), $cacheResults->pluck("id")); + } +} diff --git a/tests/database/seeds/DatabaseSeeder.php b/tests/database/seeds/DatabaseSeeder.php index 5ca06f2..be2c50c 100644 --- a/tests/database/seeds/DatabaseSeeder.php +++ b/tests/database/seeds/DatabaseSeeder.php @@ -47,7 +47,7 @@ public function run() "commentable_id" => $comment->commentable_id, "commentable_type" => UncachedPost::class, "description" => $comment->description, - "subject" => $comment->subject, + "subject" => $comment->subject . ' - uncached post', ]); }); $publishers = factory(Publisher::class, 10)->create();