EloquentMorphCountEagerLoadingTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\EloquentMorphCountEagerLoadingTest;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\MorphTo;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Support\Facades\Schema;
  7. use Illuminate\Tests\Integration\Database\DatabaseTestCase;
  8. class EloquentMorphCountEagerLoadingTest extends DatabaseTestCase
  9. {
  10. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  11. {
  12. Schema::create('likes', function (Blueprint $table) {
  13. $table->increments('id');
  14. $table->unsignedInteger('post_id');
  15. });
  16. Schema::create('views', function (Blueprint $table) {
  17. $table->increments('id');
  18. $table->unsignedInteger('video_id');
  19. });
  20. Schema::create('posts', function (Blueprint $table) {
  21. $table->increments('id');
  22. });
  23. Schema::create('videos', function (Blueprint $table) {
  24. $table->increments('id');
  25. });
  26. Schema::create('comments', function (Blueprint $table) {
  27. $table->increments('id');
  28. $table->string('commentable_type');
  29. $table->integer('commentable_id');
  30. });
  31. $post = Post::create();
  32. $video = Video::create();
  33. tap((new Like)->post()->associate($post))->save();
  34. tap((new Like)->post()->associate($post))->save();
  35. tap((new View)->video()->associate($video))->save();
  36. (new Comment)->commentable()->associate($post)->save();
  37. (new Comment)->commentable()->associate($video)->save();
  38. }
  39. public function testWithMorphCountLoading()
  40. {
  41. $comments = Comment::query()
  42. ->with(['commentable' => function (MorphTo $morphTo) {
  43. $morphTo->morphWithCount([Post::class => ['likes']]);
  44. }])
  45. ->get();
  46. $this->assertTrue($comments[0]->relationLoaded('commentable'));
  47. $this->assertEquals(2, $comments[0]->commentable->likes_count);
  48. $this->assertTrue($comments[1]->relationLoaded('commentable'));
  49. $this->assertNull($comments[1]->commentable->views_count);
  50. }
  51. public function testWithMorphCountLoadingWithSingleRelation()
  52. {
  53. $comments = Comment::query()
  54. ->with(['commentable' => function (MorphTo $morphTo) {
  55. $morphTo->morphWithCount([Post::class => 'likes']);
  56. }])
  57. ->get();
  58. $this->assertTrue($comments[0]->relationLoaded('commentable'));
  59. $this->assertEquals(2, $comments[0]->commentable->likes_count);
  60. }
  61. }
  62. class Comment extends Model
  63. {
  64. public $timestamps = false;
  65. public function commentable()
  66. {
  67. return $this->morphTo();
  68. }
  69. }
  70. class Post extends Model
  71. {
  72. public $timestamps = false;
  73. public function likes()
  74. {
  75. return $this->hasMany(Like::class);
  76. }
  77. }
  78. class Video extends Model
  79. {
  80. public $timestamps = false;
  81. public function views()
  82. {
  83. return $this->hasMany(View::class);
  84. }
  85. }
  86. class Like extends Model
  87. {
  88. public $timestamps = false;
  89. public function post()
  90. {
  91. return $this->belongsTo(Post::class);
  92. }
  93. }
  94. class View extends Model
  95. {
  96. public $timestamps = false;
  97. public function video()
  98. {
  99. return $this->belongsTo(Video::class);
  100. }
  101. }