EloquentCollectionLoadCountTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Integration\Database;
  3. use Illuminate\Database\Eloquent\Collection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Database\Schema\Blueprint;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema;
  9. use Illuminate\Tests\Integration\Database\DatabaseTestCase;
  10. class EloquentCollectionLoadCountTest extends DatabaseTestCase
  11. {
  12. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  13. {
  14. Schema::create('posts', function (Blueprint $table) {
  15. $table->increments('id');
  16. $table->unsignedInteger('some_default_value');
  17. $table->softDeletes();
  18. });
  19. Schema::create('comments', function (Blueprint $table) {
  20. $table->increments('id');
  21. $table->unsignedInteger('post_id');
  22. });
  23. Schema::create('likes', function (Blueprint $table) {
  24. $table->increments('id');
  25. $table->unsignedInteger('post_id');
  26. });
  27. $post = Post::create();
  28. $post->comments()->saveMany([new Comment, new Comment]);
  29. $post->likes()->save(new Like);
  30. Post::create();
  31. }
  32. public function testLoadCount()
  33. {
  34. $posts = Post::all();
  35. DB::enableQueryLog();
  36. $posts->loadCount('comments');
  37. $this->assertCount(1, DB::getQueryLog());
  38. $this->assertEquals('2', $posts[0]->comments_count);
  39. $this->assertEquals('0', $posts[1]->comments_count);
  40. $this->assertEquals('2', $posts[0]->getOriginal('comments_count'));
  41. }
  42. public function testLoadCountWithSameModels()
  43. {
  44. $posts = Post::all()->push(Post::first());
  45. DB::enableQueryLog();
  46. $posts->loadCount('comments');
  47. $this->assertCount(1, DB::getQueryLog());
  48. $this->assertEquals('2', $posts[0]->comments_count);
  49. $this->assertEquals('0', $posts[1]->comments_count);
  50. $this->assertEquals('2', $posts[2]->comments_count);
  51. }
  52. public function testLoadCountOnDeletedModels()
  53. {
  54. $posts = Post::all()->each->delete();
  55. DB::enableQueryLog();
  56. $posts->loadCount('comments');
  57. $this->assertCount(1, DB::getQueryLog());
  58. $this->assertEquals('2', $posts[0]->comments_count);
  59. $this->assertEquals('0', $posts[1]->comments_count);
  60. }
  61. public function testLoadCountWithArrayOfRelations()
  62. {
  63. $posts = Post::all();
  64. DB::enableQueryLog();
  65. $posts->loadCount(['comments', 'likes']);
  66. $this->assertCount(1, DB::getQueryLog());
  67. $this->assertEquals('2', $posts[0]->comments_count);
  68. $this->assertEquals('1', $posts[0]->likes_count);
  69. $this->assertEquals('0', $posts[1]->comments_count);
  70. $this->assertEquals('0', $posts[1]->likes_count);
  71. }
  72. public function testLoadCountDoesNotOverrideAttributesWithDefaultValue()
  73. {
  74. $post = Post::first();
  75. $post->some_default_value = 200;
  76. Collection::make([$post])->loadCount('comments');
  77. $this->assertSame(200, $post->some_default_value);
  78. $this->assertEquals('2', $post->comments_count);
  79. }
  80. }
  81. class Post extends Model
  82. {
  83. use SoftDeletes;
  84. protected $attributes = [
  85. 'some_default_value' => 100,
  86. ];
  87. public $timestamps = false;
  88. public function comments()
  89. {
  90. return $this->hasMany(Comment::class);
  91. }
  92. public function likes()
  93. {
  94. return $this->hasMany(Like::class);
  95. }
  96. }
  97. class Comment extends Model
  98. {
  99. public $timestamps = false;
  100. }
  101. class Like extends Model
  102. {
  103. public $timestamps = false;
  104. }