123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace Illuminate\Tests\Integration\Database\EloquentMorphToIsTest;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Tests\Integration\Database\DatabaseTestCase;
- class EloquentMorphToIsTest extends DatabaseTestCase
- {
- protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
- {
- Schema::create('posts', function (Blueprint $table) {
- $table->increments('id');
- $table->timestamps();
- });
- Schema::create('comments', function (Blueprint $table) {
- $table->increments('id');
- $table->string('commentable_type');
- $table->integer('commentable_id');
- });
- $post = Post::create();
- (new Comment)->commentable()->associate($post)->save();
- }
- public function testParentIsNotNull()
- {
- $child = Comment::first();
- $parent = null;
- $this->assertFalse($child->commentable()->is($parent));
- $this->assertTrue($child->commentable()->isNot($parent));
- }
- public function testParentIsModel()
- {
- $child = Comment::first();
- $parent = Post::first();
- $this->assertTrue($child->commentable()->is($parent));
- $this->assertFalse($child->commentable()->isNot($parent));
- }
- public function testParentIsNotAnotherModel()
- {
- $child = Comment::first();
- $parent = new Post;
- $parent->id = 2;
- $this->assertFalse($child->commentable()->is($parent));
- $this->assertTrue($child->commentable()->isNot($parent));
- }
- public function testNullParentIsNotModel()
- {
- $child = Comment::first();
- $child->commentable()->dissociate();
- $parent = Post::first();
- $this->assertFalse($child->commentable()->is($parent));
- $this->assertTrue($child->commentable()->isNot($parent));
- }
- public function testParentIsNotModelWithAnotherTable()
- {
- $child = Comment::first();
- $parent = Post::first();
- $parent->setTable('foo');
- $this->assertFalse($child->commentable()->is($parent));
- $this->assertTrue($child->commentable()->isNot($parent));
- }
- public function testParentIsNotModelWithAnotherConnection()
- {
- $child = Comment::first();
- $parent = Post::first();
- $parent->setConnection('foo');
- $this->assertFalse($child->commentable()->is($parent));
- $this->assertTrue($child->commentable()->isNot($parent));
- }
- }
- class Comment extends Model
- {
- public $timestamps = false;
- public function commentable()
- {
- return $this->morphTo();
- }
- }
- class Post extends Model
- {
- //
- }
|