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