EloquentUpdateTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Support\Facades\Schema;
  7. use Illuminate\Support\Str;
  8. class EloquentUpdateTest extends DatabaseTestCase
  9. {
  10. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  11. {
  12. Schema::create('test_model1', function (Blueprint $table) {
  13. $table->increments('id');
  14. $table->string('name')->nullable();
  15. $table->string('title')->nullable();
  16. });
  17. Schema::create('test_model2', function (Blueprint $table) {
  18. $table->increments('id');
  19. $table->string('name');
  20. $table->string('job')->nullable();
  21. $table->softDeletes();
  22. $table->timestamps();
  23. });
  24. Schema::create('test_model3', function (Blueprint $table) {
  25. $table->increments('id');
  26. $table->unsignedInteger('counter');
  27. $table->softDeletes();
  28. $table->timestamps();
  29. });
  30. }
  31. public function testBasicUpdate()
  32. {
  33. TestUpdateModel1::create([
  34. 'name' => Str::random(),
  35. 'title' => 'Ms.',
  36. ]);
  37. TestUpdateModel1::where('title', 'Ms.')->delete();
  38. $this->assertCount(0, TestUpdateModel1::all());
  39. }
  40. /** @group SkipMSSQL */
  41. public function testUpdateWithLimitsAndOrders()
  42. {
  43. for ($i = 1; $i <= 10; $i++) {
  44. TestUpdateModel1::create();
  45. }
  46. TestUpdateModel1::latest('id')->limit(3)->update(['title' => 'Dr.']);
  47. $this->assertSame('Dr.', TestUpdateModel1::find(8)->title);
  48. $this->assertNotSame('Dr.', TestUpdateModel1::find(7)->title);
  49. }
  50. public function testUpdatedAtWithJoins()
  51. {
  52. TestUpdateModel1::create([
  53. 'name' => 'Abdul',
  54. 'title' => 'Mr.',
  55. ]);
  56. TestUpdateModel2::create([
  57. 'name' => Str::random(),
  58. ]);
  59. TestUpdateModel2::join('test_model1', function ($join) {
  60. $join->on('test_model1.id', '=', 'test_model2.id')
  61. ->where('test_model1.title', '=', 'Mr.');
  62. })->update(['test_model2.name' => 'Abdul', 'job' => 'Engineer']);
  63. $record = TestUpdateModel2::find(1);
  64. $this->assertSame('Engineer: Abdul', $record->job.': '.$record->name);
  65. }
  66. public function testSoftDeleteWithJoins()
  67. {
  68. TestUpdateModel1::create([
  69. 'name' => Str::random(),
  70. 'title' => 'Mr.',
  71. ]);
  72. TestUpdateModel2::create([
  73. 'name' => Str::random(),
  74. ]);
  75. TestUpdateModel2::join('test_model1', function ($join) {
  76. $join->on('test_model1.id', '=', 'test_model2.id')
  77. ->where('test_model1.title', '=', 'Mr.');
  78. })->delete();
  79. $this->assertCount(0, TestUpdateModel2::all());
  80. }
  81. public function testIncrement()
  82. {
  83. TestUpdateModel3::create([
  84. 'counter' => 0,
  85. ]);
  86. TestUpdateModel3::create([
  87. 'counter' => 0,
  88. ])->delete();
  89. TestUpdateModel3::increment('counter');
  90. $models = TestUpdateModel3::withoutGlobalScopes()->orderBy('id')->get();
  91. $this->assertEquals(1, $models[0]->counter);
  92. $this->assertEquals(0, $models[1]->counter);
  93. }
  94. }
  95. class TestUpdateModel1 extends Model
  96. {
  97. public $table = 'test_model1';
  98. public $timestamps = false;
  99. protected $guarded = [];
  100. }
  101. class TestUpdateModel2 extends Model
  102. {
  103. use SoftDeletes;
  104. public $table = 'test_model2';
  105. protected $fillable = ['name'];
  106. }
  107. class TestUpdateModel3 extends Model
  108. {
  109. use SoftDeletes;
  110. public $table = 'test_model3';
  111. protected $fillable = ['counter'];
  112. protected $casts = ['deleted_at' => 'datetime'];
  113. }