Skip to content

Add whereHasMorph test #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions tests/Integration/CachedBuilder/WhereHasMorphTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;

use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Comment;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedComment;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost;
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
use Illuminate\Database\Eloquent\Builder;

class WhereHasMorphTest extends IntegrationTestCase
{
public function testWithSingleMorphModel()
{
$comments = (new Comment)
->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"));
}
}
2 changes: 1 addition & 1 deletion tests/database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down