Skip to content

Commit

Permalink
Test for eloquent destroy method (#45689)
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 authored Jan 18, 2023
1 parent f7a7a86 commit 7906436
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/Integration/Database/EloquentDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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
{
Expand Down Expand Up @@ -92,6 +93,49 @@ public function testDeleteQuietly()

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
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/Database/Fixtures/PostStringyKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Illuminate\Tests\Integration\Database\Fixtures;

use Illuminate\Database\Eloquent\Model;

class PostStringyKey extends Model
{
public $table = 'my_posts';

public $primaryKey = 'my_id';
}

0 comments on commit 7906436

Please sign in to comment.