123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace Illuminate\Tests\Database;
- use Illuminate\Database\Capsule\Manager as DB;
- use Illuminate\Database\Eloquent\Model as Eloquent;
- use PHPUnit\Framework\TestCase;
- class DatabaseEloquentPolymorphicIntegrationTest extends TestCase
- {
- protected function setUp(): void
- {
- $db = new DB;
- $db->addConnection([
- 'driver' => 'sqlite',
- 'database' => ':memory:',
- ]);
- $db->bootEloquent();
- $db->setAsGlobal();
- $this->createSchema();
- }
- /**
- * Setup the database schema.
- *
- * @return void
- */
- public function createSchema()
- {
- $this->schema()->create('users', function ($table) {
- $table->increments('id');
- $table->string('email')->unique();
- $table->timestamps();
- });
- $this->schema()->create('posts', function ($table) {
- $table->increments('id');
- $table->integer('user_id');
- $table->string('title');
- $table->text('body');
- $table->timestamps();
- });
- $this->schema()->create('comments', function ($table) {
- $table->increments('id');
- $table->integer('commentable_id');
- $table->string('commentable_type');
- $table->integer('user_id');
- $table->text('body');
- $table->timestamps();
- });
- $this->schema()->create('likes', function ($table) {
- $table->increments('id');
- $table->integer('likeable_id');
- $table->string('likeable_type');
- $table->timestamps();
- });
- }
- /**
- * Tear down the database schema.
- *
- * @return void
- */
- protected function tearDown(): void
- {
- $this->schema()->drop('users');
- $this->schema()->drop('posts');
- $this->schema()->drop('comments');
- }
- public function testItLoadsRelationshipsAutomatically()
- {
- $this->seedData();
- $like = TestLikeWithSingleWith::first();
- $this->assertTrue($like->relationLoaded('likeable'));
- $this->assertEquals(TestComment::first(), $like->likeable);
- }
- public function testItLoadsChainedRelationshipsAutomatically()
- {
- $this->seedData();
- $like = TestLikeWithSingleWith::first();
- $this->assertTrue($like->likeable->relationLoaded('commentable'));
- $this->assertEquals(TestPost::first(), $like->likeable->commentable);
- }
- public function testItLoadsNestedRelationshipsAutomatically()
- {
- $this->seedData();
- $like = TestLikeWithNestedWith::first();
- $this->assertTrue($like->relationLoaded('likeable'));
- $this->assertTrue($like->likeable->relationLoaded('owner'));
- $this->assertEquals(TestUser::first(), $like->likeable->owner);
- }
- public function testItLoadsNestedRelationshipsOnDemand()
- {
- $this->seedData();
- $like = TestLike::with('likeable.owner')->first();
- $this->assertTrue($like->relationLoaded('likeable'));
- $this->assertTrue($like->likeable->relationLoaded('owner'));
- $this->assertEquals(TestUser::first(), $like->likeable->owner);
- }
- public function testItLoadsNestedMorphRelationshipsOnDemand()
- {
- $this->seedData();
- TestPost::first()->likes()->create([]);
- $likes = TestLike::with('likeable.owner')->get()->loadMorph('likeable', [
- TestComment::class => ['commentable'],
- TestPost::class => 'comments',
- ]);
- $this->assertTrue($likes[0]->relationLoaded('likeable'));
- $this->assertTrue($likes[0]->likeable->relationLoaded('owner'));
- $this->assertTrue($likes[0]->likeable->relationLoaded('commentable'));
- $this->assertTrue($likes[1]->relationLoaded('likeable'));
- $this->assertTrue($likes[1]->likeable->relationLoaded('owner'));
- $this->assertTrue($likes[1]->likeable->relationLoaded('comments'));
- }
- public function testItLoadsNestedMorphRelationshipCountsOnDemand()
- {
- $this->seedData();
- TestPost::first()->likes()->create([]);
- TestComment::first()->likes()->create([]);
- $likes = TestLike::with('likeable.owner')->get()->loadMorphCount('likeable', [
- TestComment::class => ['likes'],
- TestPost::class => 'comments',
- ]);
- $this->assertTrue($likes[0]->relationLoaded('likeable'));
- $this->assertTrue($likes[0]->likeable->relationLoaded('owner'));
- $this->assertEquals(2, $likes[0]->likeable->likes_count);
- $this->assertTrue($likes[1]->relationLoaded('likeable'));
- $this->assertTrue($likes[1]->likeable->relationLoaded('owner'));
- $this->assertEquals(1, $likes[1]->likeable->comments_count);
- $this->assertTrue($likes[2]->relationLoaded('likeable'));
- $this->assertTrue($likes[2]->likeable->relationLoaded('owner'));
- $this->assertEquals(2, $likes[2]->likeable->likes_count);
- }
- /**
- * Helpers...
- */
- protected function seedData()
- {
- $taylor = TestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
- $taylor->posts()->create(['title' => 'A title', 'body' => 'A body'])
- ->comments()->create(['body' => 'A comment body', 'user_id' => 1])
- ->likes()->create([]);
- }
- /**
- * Get a database connection instance.
- *
- * @return \Illuminate\Database\Connection
- */
- protected function connection()
- {
- return Eloquent::getConnectionResolver()->connection();
- }
- /**
- * Get a schema builder instance.
- *
- * @return \Illuminate\Database\Schema\Builder
- */
- protected function schema()
- {
- return $this->connection()->getSchemaBuilder();
- }
- }
- /**
- * Eloquent Models...
- */
- class TestUser extends Eloquent
- {
- protected $table = 'users';
- protected $guarded = [];
- public function posts()
- {
- return $this->hasMany(TestPost::class, 'user_id');
- }
- }
- /**
- * Eloquent Models...
- */
- class TestPost extends Eloquent
- {
- protected $table = 'posts';
- protected $guarded = [];
- public function comments()
- {
- return $this->morphMany(TestComment::class, 'commentable');
- }
- public function owner()
- {
- return $this->belongsTo(TestUser::class, 'user_id');
- }
- public function likes()
- {
- return $this->morphMany(TestLike::class, 'likeable');
- }
- }
- /**
- * Eloquent Models...
- */
- class TestComment extends Eloquent
- {
- protected $table = 'comments';
- protected $guarded = [];
- protected $with = ['commentable'];
- public function owner()
- {
- return $this->belongsTo(TestUser::class, 'user_id');
- }
- public function commentable()
- {
- return $this->morphTo();
- }
- public function likes()
- {
- return $this->morphMany(TestLike::class, 'likeable');
- }
- }
- class TestLike extends Eloquent
- {
- protected $table = 'likes';
- protected $guarded = [];
- public function likeable()
- {
- return $this->morphTo();
- }
- }
- class TestLikeWithSingleWith extends Eloquent
- {
- protected $table = 'likes';
- protected $guarded = [];
- protected $with = ['likeable'];
- public function likeable()
- {
- return $this->morphTo();
- }
- }
- class TestLikeWithNestedWith extends Eloquent
- {
- protected $table = 'likes';
- protected $guarded = [];
- protected $with = ['likeable.owner'];
- public function likeable()
- {
- return $this->morphTo();
- }
- }
|