EloquentPaginateTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6. class EloquentPaginateTest extends DatabaseTestCase
  7. {
  8. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  9. {
  10. Schema::create('posts', function (Blueprint $table) {
  11. $table->increments('id');
  12. $table->string('title')->nullable();
  13. $table->unsignedInteger('user_id')->nullable();
  14. $table->timestamps();
  15. });
  16. Schema::create('users', function ($table) {
  17. $table->increments('id');
  18. $table->timestamps();
  19. });
  20. }
  21. public function testPaginationOnTopOfColumns()
  22. {
  23. for ($i = 1; $i <= 50; $i++) {
  24. Post::create([
  25. 'title' => 'Title '.$i,
  26. ]);
  27. }
  28. $this->assertCount(15, Post::paginate(15, ['id', 'title']));
  29. }
  30. public function testPaginationWithDistinct()
  31. {
  32. for ($i = 1; $i <= 3; $i++) {
  33. Post::create(['title' => 'Hello world']);
  34. Post::create(['title' => 'Goodbye world']);
  35. }
  36. $query = Post::query()->distinct();
  37. $this->assertEquals(6, $query->get()->count());
  38. $this->assertEquals(6, $query->count());
  39. $this->assertEquals(6, $query->paginate()->total());
  40. }
  41. public function testPaginationWithDistinctAndSelect()
  42. {
  43. // This is the 'broken' behaviour, but this test is added to show backwards compatibility.
  44. for ($i = 1; $i <= 3; $i++) {
  45. Post::create(['title' => 'Hello world']);
  46. Post::create(['title' => 'Goodbye world']);
  47. }
  48. $query = Post::query()->distinct()->select('title');
  49. $this->assertEquals(2, $query->get()->count());
  50. $this->assertEquals(6, $query->count());
  51. $this->assertEquals(6, $query->paginate()->total());
  52. }
  53. public function testPaginationWithDistinctColumnsAndSelect()
  54. {
  55. for ($i = 1; $i <= 3; $i++) {
  56. Post::create(['title' => 'Hello world']);
  57. Post::create(['title' => 'Goodbye world']);
  58. }
  59. $query = Post::query()->distinct('title')->select('title');
  60. $this->assertEquals(2, $query->get()->count());
  61. $this->assertEquals(2, $query->count());
  62. $this->assertEquals(2, $query->paginate()->total());
  63. }
  64. public function testPaginationWithDistinctColumnsAndSelectAndJoin()
  65. {
  66. for ($i = 1; $i <= 5; $i++) {
  67. $user = User::create();
  68. for ($j = 1; $j <= 10; $j++) {
  69. Post::create([
  70. 'title' => 'Title '.$i,
  71. 'user_id' => $user->id,
  72. ]);
  73. }
  74. }
  75. $query = User::query()->join('posts', 'posts.user_id', '=', 'users.id')
  76. ->distinct('users.id')->select('users.*');
  77. $this->assertEquals(5, $query->get()->count());
  78. $this->assertEquals(5, $query->count());
  79. $this->assertEquals(5, $query->paginate()->total());
  80. }
  81. }
  82. class Post extends Model
  83. {
  84. protected $guarded = [];
  85. }
  86. class User extends Model
  87. {
  88. protected $guarded = [];
  89. }