DatabaseEloquentBelongsToManyChunkByIdTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use Illuminate\Database\Eloquent\Collection;
  5. use Illuminate\Database\Eloquent\Model as Eloquent;
  6. use PHPUnit\Framework\TestCase;
  7. class DatabaseEloquentBelongsToManyChunkByIdTest extends TestCase
  8. {
  9. protected function setUp(): void
  10. {
  11. $db = new DB;
  12. $db->addConnection([
  13. 'driver' => 'sqlite',
  14. 'database' => ':memory:',
  15. ]);
  16. $db->bootEloquent();
  17. $db->setAsGlobal();
  18. $this->createSchema();
  19. }
  20. /**
  21. * Setup the database schema.
  22. *
  23. * @return void
  24. */
  25. public function createSchema()
  26. {
  27. $this->schema()->create('users', function ($table) {
  28. $table->increments('id');
  29. $table->string('email')->unique();
  30. });
  31. $this->schema()->create('articles', function ($table) {
  32. $table->increments('aid');
  33. $table->string('title');
  34. });
  35. $this->schema()->create('article_user', function ($table) {
  36. $table->integer('article_id')->unsigned();
  37. $table->foreign('article_id')->references('aid')->on('articles');
  38. $table->integer('user_id')->unsigned();
  39. $table->foreign('user_id')->references('id')->on('users');
  40. });
  41. }
  42. public function testBelongsToChunkById()
  43. {
  44. $this->seedData();
  45. $user = BelongsToManyChunkByIdTestTestUser::query()->first();
  46. $i = 0;
  47. $user->articles()->chunkById(1, function (Collection $collection) use (&$i) {
  48. $i++;
  49. $this->assertEquals($i, $collection->first()->aid);
  50. });
  51. $this->assertSame(3, $i);
  52. }
  53. /**
  54. * Tear down the database schema.
  55. *
  56. * @return void
  57. */
  58. protected function tearDown(): void
  59. {
  60. $this->schema()->drop('users');
  61. $this->schema()->drop('articles');
  62. $this->schema()->drop('article_user');
  63. }
  64. /**
  65. * Helpers...
  66. */
  67. protected function seedData()
  68. {
  69. $user = BelongsToManyChunkByIdTestTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  70. BelongsToManyChunkByIdTestTestArticle::query()->insert([
  71. ['aid' => 1, 'title' => 'Another title'],
  72. ['aid' => 2, 'title' => 'Another title'],
  73. ['aid' => 3, 'title' => 'Another title'],
  74. ]);
  75. $user->articles()->sync([3, 1, 2]);
  76. }
  77. /**
  78. * Get a database connection instance.
  79. *
  80. * @return \Illuminate\Database\ConnectionInterface
  81. */
  82. protected function connection()
  83. {
  84. return Eloquent::getConnectionResolver()->connection();
  85. }
  86. /**
  87. * Get a schema builder instance.
  88. *
  89. * @return \Illuminate\Database\Schema\Builder
  90. */
  91. protected function schema()
  92. {
  93. return $this->connection()->getSchemaBuilder();
  94. }
  95. }
  96. class BelongsToManyChunkByIdTestTestUser extends Eloquent
  97. {
  98. protected $table = 'users';
  99. protected $fillable = ['id', 'email'];
  100. public $timestamps = false;
  101. public function articles()
  102. {
  103. return $this->belongsToMany(BelongsToManyChunkByIdTestTestArticle::class, 'article_user', 'user_id', 'article_id');
  104. }
  105. }
  106. class BelongsToManyChunkByIdTestTestArticle extends Eloquent
  107. {
  108. protected $primaryKey = 'aid';
  109. protected $table = 'articles';
  110. protected $keyType = 'string';
  111. public $incrementing = false;
  112. public $timestamps = false;
  113. protected $fillable = ['aid', 'title'];
  114. }