Skip to content

Commit cb1e9a7

Browse files
committed
Add whereHasMorph test
1 parent 6be9445 commit cb1e9a7

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Comment;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedComment;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost;
7+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
8+
use Illuminate\Database\Eloquent\Builder;
9+
10+
class WhereHasMorphTest extends IntegrationTestCase
11+
{
12+
public function testWithSingleMorphModel()
13+
{
14+
$comments = (new Comment)
15+
->whereHasMorph('commentable', Post::class)
16+
->get();
17+
$uncachedComments = (new UncachedComment())
18+
->whereHasMorph('commentable', Post::class)
19+
->get();
20+
21+
$cacheResults = $this->cache()->tags([
22+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment",
23+
])
24+
->get(sha1(
25+
"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"
26+
))['value'];
27+
28+
$this->assertCount(5, $comments);
29+
$this->assertEquals($comments->pluck("id"), $uncachedComments->pluck("id"));
30+
$this->assertEquals($uncachedComments->pluck("id"), $cacheResults->pluck("id"));
31+
}
32+
33+
public function testWithMultipleMorphModels()
34+
{
35+
$comments = (new Comment)
36+
->whereHasMorph('commentable', [Post::class, UncachedPost::class])
37+
->get();
38+
$uncachedComments = (new UncachedComment())
39+
->whereHasMorph('commentable', [Post::class, UncachedPost::class])
40+
->get();
41+
42+
$cacheResults = $this->cache()->tags([
43+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment",
44+
])
45+
->get(sha1(
46+
"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"
47+
))['value'];
48+
49+
$this->assertCount(10, $comments);
50+
$this->assertEquals($comments->pluck("id"), $uncachedComments->pluck("id"));
51+
$this->assertEquals($uncachedComments->pluck("id"), $cacheResults->pluck("id"));
52+
}
53+
54+
public function testWithMultipleMorphModelsWithClosure()
55+
{
56+
$comments = (new Comment)
57+
->whereHasMorph('commentable', [Post::class, UncachedPost::class], function (Builder $query) {
58+
return $query->where('subject', 'like', '%uncached post');
59+
})
60+
->get();
61+
$uncachedComments = (new UncachedComment())
62+
->whereHasMorph('commentable', [Post::class, UncachedPost::class], function (Builder $query) {
63+
return $query->where('subject', 'like', '%uncached post');
64+
})
65+
->get();
66+
67+
$cacheResults = $this->cache()->tags([
68+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment",
69+
])
70+
->get(sha1(
71+
"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"
72+
))['value'];
73+
74+
$this->assertCount(5, $comments);
75+
$this->assertEquals($comments->pluck("id"), $uncachedComments->pluck("id"));
76+
$this->assertEquals($uncachedComments->pluck("id"), $cacheResults->pluck("id"));
77+
}
78+
}

tests/database/seeds/DatabaseSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function run()
4747
"commentable_id" => $comment->commentable_id,
4848
"commentable_type" => UncachedPost::class,
4949
"description" => $comment->description,
50-
"subject" => $comment->subject,
50+
"subject" => $comment->subject . ' - uncached post',
5151
]);
5252
});
5353
$publishers = factory(Publisher::class, 10)->create();

0 commit comments

Comments
 (0)