Skip to content

Commit 1f53fef

Browse files
committed
Add tests for many-to-many polymorphic relationships.
1 parent f3f9c5b commit 1f53fef

11 files changed

+161
-0
lines changed

tests/Fixtures/Post.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
44
use Illuminate\Database\Eloquent\Model;
55
use Illuminate\Database\Eloquent\Relations\MorphMany;
6+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
67

78
class Post extends Model
89
{
@@ -17,4 +18,9 @@ public function comments() : MorphMany
1718
{
1819
return $this->morphMany(Comment::class, "commentable");
1920
}
21+
22+
public function tags() : MorphToMany
23+
{
24+
return $this->morphToMany(Tag::class, "taggable");
25+
}
2026
}

tests/Fixtures/Tag.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\MorphToMany;
6+
7+
class Tag extends Model
8+
{
9+
use Cachable;
10+
11+
protected $fillable = [
12+
"name",
13+
];
14+
15+
public function posts() : MorphToMany
16+
{
17+
return $this->morphedByMany(Post::class, "taggable");
18+
}
19+
}

tests/Fixtures/UncachedPost.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Database\Eloquent\Model;
44
use Illuminate\Database\Eloquent\Relations\MorphMany;
5+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
56

67
class UncachedPost extends Model
78
{
@@ -15,4 +16,9 @@ public function comments() : MorphMany
1516
{
1617
return $this->morphMany(UncachedComment::class, "commentable");
1718
}
19+
20+
public function tags() : MorphToMany
21+
{
22+
return $this->morphToMany(Tag::class, "taggable");
23+
}
1824
}

tests/Fixtures/UncachedTag.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 GeneaLabs\LaravelModelCaching\Traits\Cachable;
4+
use Illuminate\Database\Eloquent\Model;
5+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
6+
7+
class UncachedTag extends Model
8+
{
9+
protected $fillable = [
10+
"name",
11+
];
12+
protected $table = "tags";
13+
14+
public function posts() : MorphToMany
15+
{
16+
return $this->morphedByMany(UncachedPost::class, "taggable");
17+
}
18+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 PolymorphicManyToManyTest extends IntegrationTestCase
8+
{
9+
public function testEagerloadedRelationship()
10+
{
11+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:posts:genealabslaravelmodelcachingtestsfixturespost-testing:{$this->testingSqlitePath}testing.sqlite:tags-first");
12+
$tags = [
13+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturespost",
14+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturestag",
15+
];
16+
17+
$result = (new Post)
18+
->with("tags")
19+
->first()
20+
->tags;
21+
$cachedResults = $this->cache()
22+
->tags($tags)
23+
->get($key)['value'];
24+
$liveResults = (new UncachedPost)
25+
->with("tags")
26+
->first()
27+
->tags;
28+
29+
$this->assertEquals($liveResults->pluck("id")->toArray(), $result->pluck("id")->toArray());
30+
$this->assertEquals($liveResults->pluck("id")->toArray(), $cachedResults->pluck("id")->toArray());
31+
$this->assertNotEmpty($result);
32+
$this->assertNotEmpty($cachedResults);
33+
$this->assertNotEmpty($liveResults);
34+
}
35+
36+
public function testLazyloadedRelationship()
37+
{
38+
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:posts:genealabslaravelmodelcachingtestsfixturespost-testing:{$this->testingSqlitePath}testing.sqlite:tags-first");
39+
$tags = [
40+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturespost",
41+
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturestag",
42+
];
43+
44+
// $result = (new Post)
45+
// ->with("tags")
46+
// ->first()
47+
// ->tags;
48+
// $cachedResults = $this->cache()
49+
// ->tags($tags)
50+
// ->get($key)['value'];
51+
// $liveResults = (new UncachedPost)
52+
// ->with("tags")
53+
// ->first()
54+
// ->tags;
55+
56+
// $this->assertEquals($liveResults->pluck("id")->toArray(), $result->pluck("id")->toArray());
57+
// $this->assertEquals($liveResults->pluck("id")->toArray(), $cachedResults->pluck("id")->toArray());
58+
// $this->assertNotEmpty($result);
59+
// $this->assertNotEmpty($cachedResults);
60+
// $this->assertNotEmpty($liveResults);
61+
$this->markTestSkipped();
62+
}
63+
}

tests/database/baseline.sqlite

64 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Tag;
5+
6+
$factory->define(Tag::class, function (Faker $faker) {
7+
return [
8+
'name' => $faker->word,
9+
];
10+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateTagsTable extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('tags', function (Blueprint $table) {
11+
$table->bigIncrements('id');
12+
$table->timestamps();
13+
14+
$table->string("name");
15+
});
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateTaggablesTable extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::create('taggables', function (Blueprint $table) {
11+
$table->unsignedBigInteger('tag_id');
12+
$table->unsignedBigInteger('taggable_id');
13+
$table->string("taggable_type");
14+
$table->timestamps();
15+
});
16+
}
17+
}

tests/database/seeds/DatabaseSeeder.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
1212
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
1313
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
14+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Tag;
1415
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPost;
1516
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedUser;
1617
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\User;
@@ -31,7 +32,11 @@ public function run()
3132
"imagable_type" => UncachedUser::class,
3233
"path" => $image->path,
3334
]);
35+
factory(Tag::class, 5)->create();
3436
$post = factory(Post::class)->create();
37+
$uncachedPost = (new UncachedPost)->first();
38+
$post->tags()->attach(1);
39+
$uncachedPost->tags()->attach(1);
3540
factory(Comment::class, 5)
3641
->create([
3742
"commentable_id" => $post->id,

tests/database/testing.sqlite

64 KB
Binary file not shown.

0 commit comments

Comments
 (0)