DatabaseEloquentBelongsToManySyncTouchesParentTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\Pivot as EloquentPivot;
  6. use Illuminate\Support\Carbon;
  7. use PHPUnit\Framework\TestCase;
  8. class DatabaseEloquentBelongsToManySyncTouchesParentTest extends TestCase
  9. {
  10. protected function setUp(): void
  11. {
  12. $db = new DB;
  13. $db->addConnection([
  14. 'driver' => 'sqlite',
  15. 'database' => ':memory:',
  16. ]);
  17. $db->bootEloquent();
  18. $db->setAsGlobal();
  19. $this->createSchema();
  20. }
  21. /**
  22. * Setup the database schema.
  23. *
  24. * @return void
  25. */
  26. public function createSchema()
  27. {
  28. $this->schema()->create('articles', function ($table) {
  29. $table->string('id');
  30. $table->string('title');
  31. $table->primary('id');
  32. $table->timestamps();
  33. });
  34. $this->schema()->create('article_user', function ($table) {
  35. $table->string('article_id');
  36. $table->foreign('article_id')->references('id')->on('articles');
  37. $table->integer('user_id')->unsigned();
  38. $table->foreign('user_id')->references('id')->on('users');
  39. $table->timestamps();
  40. });
  41. $this->schema()->create('users', function ($table) {
  42. $table->increments('id');
  43. $table->string('email')->unique();
  44. $table->timestamps();
  45. });
  46. }
  47. /**
  48. * Tear down the database schema.
  49. *
  50. * @return void
  51. */
  52. protected function tearDown(): void
  53. {
  54. $this->schema()->drop('users');
  55. $this->schema()->drop('articles');
  56. $this->schema()->drop('article_user');
  57. }
  58. /**
  59. * Helpers...
  60. */
  61. protected function seedData()
  62. {
  63. DatabaseEloquentBelongsToManySyncTouchesParentTestTestUser::create(['id' => 1, 'email' => 'taylorotwell@gmail.com']);
  64. DatabaseEloquentBelongsToManySyncTouchesParentTestTestUser::create(['id' => 2, 'email' => 'anonymous@gmail.com']);
  65. DatabaseEloquentBelongsToManySyncTouchesParentTestTestUser::create(['id' => 3, 'email' => 'anoni-mous@gmail.com']);
  66. }
  67. public function testSyncWithDetachedValuesShouldTouch()
  68. {
  69. $this->seedData();
  70. Carbon::setTestNow('2021-07-19 10:13:14');
  71. $article = DatabaseEloquentBelongsToManySyncTouchesParentTestTestArticle::create(['id' => 1, 'title' => 'uuid title']);
  72. $article->users()->sync([1, 2, 3]);
  73. $this->assertSame('2021-07-19 10:13:14', $article->updated_at->format('Y-m-d H:i:s'));
  74. Carbon::setTestNow('2021-07-20 19:13:14');
  75. $result = $article->users()->sync([1, 2]);
  76. $this->assertTrue(collect($result['detached'])->count() === 1);
  77. $this->assertSame('3', collect($result['detached'])->first());
  78. $article->refresh();
  79. $this->assertSame('2021-07-20 19:13:14', $article->updated_at->format('Y-m-d H:i:s'));
  80. $user1 = DatabaseEloquentBelongsToManySyncTouchesParentTestTestUser::find(1);
  81. $this->assertNotSame('2021-07-20 19:13:14', $user1->updated_at->format('Y-m-d H:i:s'));
  82. $user2 = DatabaseEloquentBelongsToManySyncTouchesParentTestTestUser::find(2);
  83. $this->assertNotSame('2021-07-20 19:13:14', $user2->updated_at->format('Y-m-d H:i:s'));
  84. $user3 = DatabaseEloquentBelongsToManySyncTouchesParentTestTestUser::find(3);
  85. $this->assertNotSame('2021-07-20 19:13:14', $user3->updated_at->format('Y-m-d H:i:s'));
  86. }
  87. /**
  88. * Get a database connection instance.
  89. *
  90. * @return \Illuminate\Database\ConnectionInterface
  91. */
  92. protected function connection()
  93. {
  94. return Eloquent::getConnectionResolver()->connection();
  95. }
  96. /**
  97. * Get a schema builder instance.
  98. *
  99. * @return \Illuminate\Database\Schema\Builder
  100. */
  101. protected function schema()
  102. {
  103. return $this->connection()->getSchemaBuilder();
  104. }
  105. }
  106. class DatabaseEloquentBelongsToManySyncTouchesParentTestTestArticle extends Eloquent
  107. {
  108. protected $table = 'articles';
  109. protected $keyType = 'string';
  110. public $incrementing = false;
  111. protected $fillable = ['id', 'title'];
  112. public function users()
  113. {
  114. return $this
  115. ->belongsToMany(DatabaseEloquentBelongsToManySyncTouchesParentTestTestArticle::class, 'article_user', 'article_id', 'user_id')
  116. ->using(DatabaseEloquentBelongsToManySyncTouchesParentTestTestArticleUser::class)
  117. ->withTimestamps();
  118. }
  119. }
  120. class DatabaseEloquentBelongsToManySyncTouchesParentTestTestArticleUser extends EloquentPivot
  121. {
  122. protected $table = 'article_user';
  123. protected $fillable = ['article_id', 'user_id'];
  124. protected $touches = ['article'];
  125. public function article()
  126. {
  127. return $this->belongsTo(DatabaseEloquentBelongsToManySyncTouchesParentTestTestArticle::class, 'article_id', 'id');
  128. }
  129. public function user()
  130. {
  131. return $this->belongsTo(DatabaseEloquentBelongsToManySyncTouchesParentTestTestUser::class, 'user_id', 'id');
  132. }
  133. }
  134. class DatabaseEloquentBelongsToManySyncTouchesParentTestTestUser extends Eloquent
  135. {
  136. protected $table = 'users';
  137. protected $keyType = 'string';
  138. public $incrementing = false;
  139. protected $fillable = ['id', 'email'];
  140. public function articles()
  141. {
  142. return $this
  143. ->belongsToMany(DatabaseEloquentBelongsToManySyncTouchesParentTestTestArticle::class, 'article_user', 'user_id', 'article_id')
  144. ->using(DatabaseEloquentBelongsToManySyncTouchesParentTestTestArticleUser::class)
  145. ->withTimestamps();
  146. }
  147. }