Skip to content

Commit f3f9c5b

Browse files
committed
Add polymorphic one-to-many relationship tests.
1 parent ee03f1d commit f3f9c5b

File tree

8 files changed

+148
-6
lines changed

8 files changed

+148
-6
lines changed

tests/Fixtures/Comment.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Comment extends Model
1111
protected $fillable = [
1212
'description',
1313
'subject',
14+
"commentable_id",
15+
"commentable_type",
1416
];
1517

1618
public function commentable() : MorphTo

tests/Fixtures/Post.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\MorphMany;
6+
7+
class Post extends Model
8+
{
9+
use Cachable;
10+
11+
protected $fillable = [
12+
"title",
13+
"body",
14+
];
15+
16+
public function comments() : MorphMany
17+
{
18+
return $this->morphMany(Comment::class, "commentable");
19+
}
20+
}

tests/Fixtures/UncachedPost.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2+
3+
use Illuminate\Database\Eloquent\Model;
4+
use Illuminate\Database\Eloquent\Relations\MorphMany;
5+
6+
class UncachedPost extends Model
7+
{
8+
protected $fillable = [
9+
"title",
10+
"body",
11+
];
12+
protected $table = "posts";
13+
14+
public function comments() : MorphMany
15+
{
16+
return $this->morphMany(UncachedComment::class, "commentable");
17+
}
18+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost;
5+
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
6+
7+
class PolymorphicOneToManyTest extends IntegrationTestCase
8+
{
9+
public function testEagerloadedRelationship()
10+
{
11+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:comments:genealabslaravelmodelcachingtestsfixturescomment-comments.commentable_id_inraw_1-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post");
12+
$tags = [
13+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment",
14+
];
15+
16+
$result = (new Post)
17+
->with("comments")
18+
->whereHas("comments")
19+
->first()
20+
->comments;
21+
$cachedResults = $this->cache()
22+
->tags($tags)
23+
->get($key)['value']
24+
->first();
25+
$liveResults = (new UncachedPost)
26+
->with("comments")
27+
->whereHas("comments")
28+
->first()
29+
->comments;
30+
31+
$this->assertEquals($liveResults->first()->description, $result->first()->description);
32+
$this->assertEquals($liveResults->first()->description, $cachedResults->first()->description);
33+
$this->assertNotEmpty($result);
34+
$this->assertNotEmpty($cachedResults);
35+
$this->assertNotEmpty($liveResults);
36+
}
37+
38+
public function testLazyloadedRelationship()
39+
{
40+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:comments:genealabslaravelmodelcachingtestsfixturescomment-comments.commentable_id_=_1-comments.commentable_id_notnull-comments.commentable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post");
41+
$tags = [
42+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturescomment",
43+
];
44+
45+
$result = (new Post)
46+
->first()
47+
->comments;
48+
49+
$cachedResults = $this->cache()
50+
->tags($tags)
51+
->get($key)['value'];
52+
$liveResults = (new UncachedPost)
53+
->first()
54+
->comments;
55+
56+
$this->assertEquals($liveResults->pluck("commentable_id")->values()->toArray(), $result->pluck("commentable_id")->values()->toArray());
57+
$this->assertEquals($liveResults->pluck("commentable_id")->values()->toArray(), $cachedResults->pluck("commentable_id")->values()->toArray());
58+
$this->assertNotEmpty($result);
59+
$this->assertNotEmpty($cachedResults);
60+
$this->assertNotEmpty($liveResults);
61+
}
62+
}

tests/Integration/CachedBuilder/PolymorphicOneToOneTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Integration\CachedBuilder;
22

3-
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4-
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Supplier;
5-
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor;
6-
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedSupplier;
73
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedUser;
84
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\User;
95
use GeneaLabs\LaravelModelCaching\Tests\IntegrationTestCase;
@@ -39,7 +35,6 @@ public function testEagerloadedRelationship()
3935
$this->assertNotEmpty($liveResults);
4036
}
4137

42-
/** @group test */
4338
public function testLazyloadedHasOneThrough()
4439
{
4540
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:images:genealabslaravelmodelcachingtestsfixturesimage-images.imagable_id_=_2-images.imagable_id_notnull-images.imagable_type_=_GeneaLabs\LaravelModelCaching\Tests\Fixtures\User-limit_1");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post;
5+
6+
$factory->define(Post::class, function (Faker $faker) {
7+
return [
8+
'title' => $faker->word,
9+
'body' => $faker->paragraph,
10+
];
11+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreatePostsTable extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('posts', function (Blueprint $table) {
11+
$table->bigIncrements('id');
12+
$table->timestamps();
13+
14+
$table->string("title");
15+
$table->string("body");
16+
});
17+
}
18+
}

tests/database/seeds/DatabaseSeeder.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
44
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Comment;
56
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\History;
67
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Image;
78
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Observers\AuthorObserver;
9+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Post;
810
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Printer;
911
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
1012
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
1113
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
12-
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedImage;
14+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost;
1315
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedUser;
1416
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\User;
1517
use Illuminate\Database\Seeder;
@@ -29,6 +31,20 @@ public function run()
2931
"imagable_type" => UncachedUser::class,
3032
"path" => $image->path,
3133
]);
34+
$post = factory(Post::class)->create();
35+
factory(Comment::class, 5)
36+
->create([
37+
"commentable_id" => $post->id,
38+
"commentable_type" => Post::class,
39+
])
40+
->each(function ($comment) {
41+
(new Comment)->create([
42+
"commentable_id" => $comment->commentable_id,
43+
"commentable_type" => UncachedPost::class,
44+
"description" => $comment->description,
45+
"subject" => $comment->subject,
46+
]);
47+
});
3248
$publishers = factory(Publisher::class, 10)->create();
3349
(new Author)->observe(AuthorObserver::class);
3450
factory(Author::class, 10)->create()

0 commit comments

Comments
 (0)