EloquentPivotSerializationTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database;
  3. use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\MorphPivot;
  6. use Illuminate\Database\Eloquent\Relations\Pivot;
  7. use Illuminate\Database\Schema\Blueprint;
  8. use Illuminate\Queue\SerializesModels;
  9. use Illuminate\Support\Facades\Schema;
  10. class EloquentPivotSerializationTest extends DatabaseTestCase
  11. {
  12. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  13. {
  14. Schema::create('users', function (Blueprint $table) {
  15. $table->increments('id');
  16. $table->string('email');
  17. $table->timestamps();
  18. });
  19. Schema::create('projects', function (Blueprint $table) {
  20. $table->increments('id');
  21. $table->string('name');
  22. $table->timestamps();
  23. });
  24. Schema::create('project_users', function (Blueprint $table) {
  25. $table->integer('user_id');
  26. $table->integer('project_id');
  27. });
  28. Schema::create('tags', function (Blueprint $table) {
  29. $table->increments('id');
  30. $table->string('name');
  31. $table->timestamps();
  32. });
  33. Schema::create('taggables', function (Blueprint $table) {
  34. $table->integer('tag_id');
  35. $table->integer('taggable_id');
  36. $table->string('taggable_type');
  37. });
  38. }
  39. public function testPivotCanBeSerializedAndRestored()
  40. {
  41. $user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']);
  42. $project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']);
  43. $project->collaborators()->attach($user);
  44. $project = $project->fresh();
  45. $class = new PivotSerializationTestClass($project->collaborators->first()->pivot);
  46. $class = unserialize(serialize($class));
  47. $this->assertEquals($project->collaborators->first()->pivot->user_id, $class->pivot->user_id);
  48. $this->assertEquals($project->collaborators->first()->pivot->project_id, $class->pivot->project_id);
  49. $class->pivot->save();
  50. }
  51. public function testMorphPivotCanBeSerializedAndRestored()
  52. {
  53. $project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']);
  54. $tag = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag']);
  55. $project->tags()->attach($tag);
  56. $project = $project->fresh();
  57. $class = new PivotSerializationTestClass($project->tags->first()->pivot);
  58. $class = unserialize(serialize($class));
  59. $this->assertEquals($project->tags->first()->pivot->tag_id, $class->pivot->tag_id);
  60. $this->assertEquals($project->tags->first()->pivot->taggable_id, $class->pivot->taggable_id);
  61. $this->assertEquals($project->tags->first()->pivot->taggable_type, $class->pivot->taggable_type);
  62. $class->pivot->save();
  63. }
  64. public function testCollectionOfPivotsCanBeSerializedAndRestored()
  65. {
  66. $user = PivotSerializationTestUser::forceCreate(['email' => 'taylor@laravel.com']);
  67. $user2 = PivotSerializationTestUser::forceCreate(['email' => 'mohamed@laravel.com']);
  68. $project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']);
  69. $project->collaborators()->attach($user);
  70. $project->collaborators()->attach($user2);
  71. $project = $project->fresh();
  72. $class = new PivotSerializationTestCollectionClass(DatabaseCollection::make($project->collaborators->map->pivot));
  73. $class = unserialize(serialize($class));
  74. $this->assertEquals($project->collaborators[0]->pivot->user_id, $class->pivots[0]->user_id);
  75. $this->assertEquals($project->collaborators[1]->pivot->project_id, $class->pivots[1]->project_id);
  76. }
  77. public function testCollectionOfMorphPivotsCanBeSerializedAndRestored()
  78. {
  79. $tag = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag 1']);
  80. $tag2 = PivotSerializationTestTag::forceCreate(['name' => 'Test Tag 2']);
  81. $project = PivotSerializationTestProject::forceCreate(['name' => 'Test Project']);
  82. $project->tags()->attach($tag);
  83. $project->tags()->attach($tag2);
  84. $project = $project->fresh();
  85. $class = new PivotSerializationTestCollectionClass(DatabaseCollection::make($project->tags->map->pivot));
  86. $class = unserialize(serialize($class));
  87. $this->assertEquals($project->tags[0]->pivot->tag_id, $class->pivots[0]->tag_id);
  88. $this->assertEquals($project->tags[0]->pivot->taggable_id, $class->pivots[0]->taggable_id);
  89. $this->assertEquals($project->tags[0]->pivot->taggable_type, $class->pivots[0]->taggable_type);
  90. $this->assertEquals($project->tags[1]->pivot->tag_id, $class->pivots[1]->tag_id);
  91. $this->assertEquals($project->tags[1]->pivot->taggable_id, $class->pivots[1]->taggable_id);
  92. $this->assertEquals($project->tags[1]->pivot->taggable_type, $class->pivots[1]->taggable_type);
  93. }
  94. }
  95. class PivotSerializationTestClass
  96. {
  97. use SerializesModels;
  98. public $pivot;
  99. public function __construct($pivot)
  100. {
  101. $this->pivot = $pivot;
  102. }
  103. }
  104. class PivotSerializationTestCollectionClass
  105. {
  106. use SerializesModels;
  107. public $pivots;
  108. public function __construct($pivots)
  109. {
  110. $this->pivots = $pivots;
  111. }
  112. }
  113. class PivotSerializationTestUser extends Model
  114. {
  115. public $table = 'users';
  116. }
  117. class PivotSerializationTestProject extends Model
  118. {
  119. public $table = 'projects';
  120. public function collaborators()
  121. {
  122. return $this->belongsToMany(
  123. PivotSerializationTestUser::class, 'project_users', 'project_id', 'user_id'
  124. )->using(PivotSerializationTestCollaborator::class);
  125. }
  126. public function tags()
  127. {
  128. return $this->morphToMany(PivotSerializationTestTag::class, 'taggable', 'taggables', 'taggable_id', 'tag_id')
  129. ->using(PivotSerializationTestTagAttachment::class);
  130. }
  131. }
  132. class PivotSerializationTestTag extends Model
  133. {
  134. public $table = 'tags';
  135. public function projects()
  136. {
  137. return $this->morphedByMany(PivotSerializationTestProject::class, 'taggable', 'taggables', 'tag_id', 'taggable_id')
  138. ->using(PivotSerializationTestTagAttachment::class);
  139. }
  140. }
  141. class PivotSerializationTestCollaborator extends Pivot
  142. {
  143. public $table = 'project_users';
  144. }
  145. class PivotSerializationTestTagAttachment extends MorphPivot
  146. {
  147. public $table = 'taggables';
  148. }