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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
<?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\Tests\Integration\Database\Fixtures\Post;
use Illuminate\Tests\Integration\Database\Fixtures\PostStringyKey;
class EloquentDeleteTest extends DatabaseTestCase
{
protected function afterRefreshingDatabase()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->nullable();
$table->timestamps();
});
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('body')->nullable();
$table->integer('post_id');
$table->timestamps();
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
});
}
public function testDeleteWithLimit()
{
if ($this->driver === 'sqlsrv') {
$this->markTestSkipped('The limit keyword is not supported on MSSQL.');
}
for ($i = 1; $i <= 10; $i++) {
Comment::create([
'post_id' => Post::create()->id,
]);
}
Post::latest('id')->limit(1)->delete();
$this->assertCount(9, Post::all());
Post::join('comments', 'comments.post_id', '=', 'posts.id')
->where('posts.id', '>', 8)
->orderBy('posts.id')
->limit(1)
->delete();
$this->assertCount(8, Post::all());
}
public function testForceDeletedEventIsFired()
{
$role = Role::create([]);
$this->assertInstanceOf(Role::class, $role);
Role::observe(new RoleObserver);
$role->delete();
$this->assertNull(RoleObserver::$model);
$role->forceDelete();
$this->assertEquals($role->id, RoleObserver::$model->id);
}
public function testForceDeletingEventIsFired()
{
$role = Role::create([]);
$this->assertInstanceOf(Role::class, $role);
Role::observe(new RoleObserver());
$role->forceDelete();
$this->assertEquals($role->id, RoleObserver::$model->id);
}
public function testDeleteQuietly()
{
$_SERVER['(-_-)'] = '\(^_^)/';
Post::deleting(fn () => $_SERVER['(-_-)'] = null);
Post::deleted(fn () => $_SERVER['(-_-)'] = null);
$post = Post::query()->create([]);
$result = $post->deleteQuietly();
$this->assertEquals('\(^_^)/', $_SERVER['(-_-)']);
$this->assertTrue($result);
$this->assertFalse($post->exists);
// For a soft-deleted model:
Role::deleting(fn () => $_SERVER['(-_-)'] = null);
Role::deleted(fn () => $_SERVER['(-_-)'] = null);
Role::softDeleted(fn () => $_SERVER['(-_-)'] = null);
$role = Role::create([]);
$result = $role->deleteQuietly();
$this->assertTrue($result);
$this->assertEquals('\(^_^)/', $_SERVER['(-_-)']);
unset($_SERVER['(-_-)']);
}
public function testDestroy()
{
Schema::create('my_posts', function (Blueprint $table) {
$table->increments('my_id');
$table->timestamps();
});
PostStringyKey::unguard();
PostStringyKey::query()->create([]);
PostStringyKey::query()->create([]);
PostStringyKey::query()->getConnection()->enableQueryLog();
PostStringyKey::retrieved(fn ($model) => $_SERVER['destroy']['retrieved'][] = $model->my_id);
PostStringyKey::deleting(fn ($model) => $_SERVER['destroy']['deleting'][] = $model->my_id);
PostStringyKey::deleted(fn ($model) => $_SERVER['destroy']['deleted'][] = $model->my_id);
$_SERVER['destroy'] = [];
PostStringyKey::destroy(1, 2, 3, 4);
$this->assertEquals([1, 2], $_SERVER['destroy']['retrieved']);
$this->assertEquals([1, 2], $_SERVER['destroy']['deleting']);
$this->assertEquals([1, 2], $_SERVER['destroy']['deleted']);
$logs = PostStringyKey::query()->getConnection()->getQueryLog();
$this->assertEquals(0, PostStringyKey::query()->count());
$this->assertStringStartsWith('select * from "my_posts" where "my_id" in (', str_replace(['`', '[', ']'], '"', $logs[0]['query']));
$this->assertStringStartsWith('delete from "my_posts" where "my_id" = ', str_replace(['`', '[', ']'], '"', $logs[1]['query']));
$this->assertEquals([1], $logs[1]['bindings']);
$this->assertStringStartsWith('delete from "my_posts" where "my_id" = ', str_replace(['`', '[', ']'], '"', $logs[2]['query']));
$this->assertEquals([2], $logs[2]['bindings']);
// Total of 3 queries.
$this->assertCount(3, $logs);
PostStringyKey::reguard();
unset($_SERVER['destroy']);
Schema::drop('my_posts');
}
}
class Comment extends Model
{
public $table = 'comments';
protected $fillable = ['post_id'];
}
class Role extends Model
{
use SoftDeletes;
public $table = 'roles';
protected $guarded = [];
}
class RoleObserver
{
public static $model;
public function forceDeleted($model)
{
static::$model = $model;
}
}
|