DatabaseEloquentPolymorphicRelationsIntegrationTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Illuminate\Database\Eloquent\Relations\Relation;
  6. use PHPUnit\Framework\TestCase;
  7. class DatabaseEloquentPolymorphicRelationsIntegrationTest extends TestCase
  8. {
  9. /**
  10. * Bootstrap Eloquent.
  11. *
  12. * @return void
  13. */
  14. protected function setUp(): void
  15. {
  16. $db = new DB;
  17. $db->addConnection([
  18. 'driver' => 'sqlite',
  19. 'database' => ':memory:',
  20. ]);
  21. $db->bootEloquent();
  22. $db->setAsGlobal();
  23. $this->createSchema();
  24. }
  25. protected function createSchema()
  26. {
  27. $this->schema('default')->create('posts', function ($table) {
  28. $table->increments('id');
  29. $table->timestamps();
  30. });
  31. $this->schema('default')->create('images', function ($table) {
  32. $table->increments('id');
  33. $table->timestamps();
  34. });
  35. $this->schema('default')->create('tags', function ($table) {
  36. $table->increments('id');
  37. $table->timestamps();
  38. });
  39. $this->schema('default')->create('taggables', function ($table) {
  40. $table->integer('eloquent_many_to_many_polymorphic_test_tag_id');
  41. $table->integer('taggable_id');
  42. $table->string('taggable_type');
  43. });
  44. }
  45. /**
  46. * Tear down the database schema.
  47. *
  48. * @return void
  49. */
  50. protected function tearDown(): void
  51. {
  52. foreach (['default'] as $connection) {
  53. $this->schema($connection)->drop('posts');
  54. $this->schema($connection)->drop('images');
  55. $this->schema($connection)->drop('tags');
  56. $this->schema($connection)->drop('taggables');
  57. }
  58. Relation::morphMap([], false);
  59. }
  60. public function testCreation()
  61. {
  62. $post = EloquentManyToManyPolymorphicTestPost::create();
  63. $image = EloquentManyToManyPolymorphicTestImage::create();
  64. $tag = EloquentManyToManyPolymorphicTestTag::create();
  65. $tag2 = EloquentManyToManyPolymorphicTestTag::create();
  66. $post->tags()->attach($tag->id);
  67. $post->tags()->attach($tag2->id);
  68. $image->tags()->attach($tag->id);
  69. $this->assertCount(2, $post->tags);
  70. $this->assertCount(1, $image->tags);
  71. $this->assertCount(1, $tag->posts);
  72. $this->assertCount(1, $tag->images);
  73. $this->assertCount(1, $tag2->posts);
  74. $this->assertCount(0, $tag2->images);
  75. }
  76. public function testEagerLoading()
  77. {
  78. $post = EloquentManyToManyPolymorphicTestPost::create();
  79. $tag = EloquentManyToManyPolymorphicTestTag::create();
  80. $post->tags()->attach($tag->id);
  81. $post = EloquentManyToManyPolymorphicTestPost::with('tags')->whereId(1)->first();
  82. $tag = EloquentManyToManyPolymorphicTestTag::with('posts')->whereId(1)->first();
  83. $this->assertTrue($post->relationLoaded('tags'));
  84. $this->assertTrue($tag->relationLoaded('posts'));
  85. $this->assertEquals($tag->id, $post->tags->first()->id);
  86. $this->assertEquals($post->id, $tag->posts->first()->id);
  87. }
  88. public function testChunkById()
  89. {
  90. $post = EloquentManyToManyPolymorphicTestPost::create();
  91. $tag1 = EloquentManyToManyPolymorphicTestTag::create();
  92. $tag2 = EloquentManyToManyPolymorphicTestTag::create();
  93. $tag3 = EloquentManyToManyPolymorphicTestTag::create();
  94. $post->tags()->attach([$tag1->id, $tag2->id, $tag3->id]);
  95. $count = 0;
  96. $iterations = 0;
  97. $post->tags()->chunkById(2, function ($tags) use (&$iterations, &$count) {
  98. $this->assertInstanceOf(EloquentManyToManyPolymorphicTestTag::class, $tags->first());
  99. $count += $tags->count();
  100. $iterations++;
  101. });
  102. $this->assertEquals(2, $iterations);
  103. $this->assertEquals(3, $count);
  104. }
  105. /**
  106. * Helpers...
  107. */
  108. /**
  109. * Get a database connection instance.
  110. *
  111. * @return \Illuminate\Database\Connection
  112. */
  113. protected function connection($connection = 'default')
  114. {
  115. return Eloquent::getConnectionResolver()->connection($connection);
  116. }
  117. /**
  118. * Get a schema builder instance.
  119. *
  120. * @return \Illuminate\Database\Schema\Builder
  121. */
  122. protected function schema($connection = 'default')
  123. {
  124. return $this->connection($connection)->getSchemaBuilder();
  125. }
  126. }
  127. /**
  128. * Eloquent Models...
  129. */
  130. class EloquentManyToManyPolymorphicTestPost extends Eloquent
  131. {
  132. protected $table = 'posts';
  133. protected $guarded = [];
  134. public function tags()
  135. {
  136. return $this->morphToMany(EloquentManyToManyPolymorphicTestTag::class, 'taggable');
  137. }
  138. }
  139. class EloquentManyToManyPolymorphicTestImage extends Eloquent
  140. {
  141. protected $table = 'images';
  142. protected $guarded = [];
  143. public function tags()
  144. {
  145. return $this->morphToMany(EloquentManyToManyPolymorphicTestTag::class, 'taggable');
  146. }
  147. }
  148. class EloquentManyToManyPolymorphicTestTag extends Eloquent
  149. {
  150. protected $table = 'tags';
  151. protected $guarded = [];
  152. public function posts()
  153. {
  154. return $this->morphedByMany(EloquentManyToManyPolymorphicTestPost::class, 'taggable');
  155. }
  156. public function images()
  157. {
  158. return $this->morphedByMany(EloquentManyToManyPolymorphicTestImage::class, 'taggable');
  159. }
  160. }