Skip to content

Commit

Permalink
改进
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Feb 25, 2025
1 parent f77cc9f commit d96a7d5
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions tests/orm/ModelMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function setUp(): void
Db::execute('TRUNCATE TABLE `test_video`;');

// 创建测试数据
$post = PostModel::create([
$post = MorphPostModel::create([
'title' => 'Test Post',
'content' => 'Post content'
]);
Expand All @@ -67,14 +67,14 @@ protected function setUp(): void
// 创建评论数据
CommentModel::create([
'content' => 'Comment on post',
'morphable_type' => PostModel::class,
'morphable_type' => MorphPostModel::class,
'morphable_id' => $post->id,
'user_id' => 1
]);

CommentModel::create([
'content' => 'Another comment on post',
'morphable_type' => PostModel::class,
'morphable_type' => MorphPostModel::class,
'morphable_id' => $post->id,
'user_id' => 2
]);
Expand All @@ -89,7 +89,7 @@ protected function setUp(): void

public function testMorphOne()
{
$post = PostModel::find(1);
$post = MorphPostModel::find(1);
$this->assertNotNull($post);

// 测试获取最新的一条评论
Expand All @@ -98,27 +98,27 @@ public function testMorphOne()
$this->assertEquals('Another comment on post', $latestComment->content);

// 测试预加载
$post = PostModel::with(['latestComment'])->find(1);
$post = MorphPostModel::with(['latestComment'])->find(1);
$this->assertTrue($post->isRelationLoaded('latestComment'));
$this->assertNotNull($post->latestComment);
}

public function testMorphMany()
{
$post = PostModel::find(1);
$post = MorphPostModel::find(1);
$this->assertNotNull($post);

// 测试获取所有评论
$comments = $post->comments;
$this->assertCount(2, $comments);

// 测试预加载
$post = PostModel::with(['comments'])->find(1);
$post = MorphPostModel::with(['comments'])->find(1);
$this->assertTrue($post->isRelationLoaded('comments'));
$this->assertCount(2, $post->comments);

// 测试关联统计
$post = PostModel::withCount('comments')->find(1);
$post = MorphPostModel::withCount('comments')->find(1);
$this->assertEquals(2, $post->comments_count);

// 测试新增关联
Expand All @@ -142,7 +142,7 @@ public function testMorphTo()

// 测试获取关联的内容
$commentable = $comment->commentable;
$this->assertInstanceOf(PostModel::class, $commentable);
$this->assertInstanceOf(MorphPostModel::class, $commentable);
$this->assertEquals('Test Post', $commentable->title);

// 测试预加载
Expand All @@ -153,20 +153,18 @@ public function testMorphTo()
}
}

class PostModel extends Model
class MorphPostModel extends Model
{
protected $table = 'test_post';
protected $autoWriteTimestamp = true;

public function comments()
{
return $this->morphMany(CommentModel::class, 'morphable');
return $this->morphMany(CommentModel::class, 'commentable');
}

public function latestComment()
{
return $this->morphOne(CommentModel::class, 'morphable')
->order('id', 'desc');
return $this->morphOne(CommentModel::class, 'commentable')->order('id', 'desc');
}
}

Expand Down

0 comments on commit d96a7d5

Please # to comment.