DatabaseEloquentBelongsToManyLazyByIdTest.php 3.4 KB

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