Laravel Unit Testing,如何“seeInDatabase”软删除行?

时间:2015-10-14 06:04:14

标签: php unit-testing laravel testing laravel-5

我正在进行小型单元测试,我在其中软删除一行。要将测试标记为成功,我必须找到以下行:

  • 给定的ID和
  • deleted_at不应为 null。

我可以满足第一个条件 - 因为显然我知道这个ID。

不幸的是,我不知道如何告诉seeInDatabase方法,我希望deleted_at null:

$this->seeInDatabase(
       'diary_note_categories',
       [
           'id' => 'a7e35ad0-6f00-4f88-b953-f498797042fc',
           'deleted_at' => null // should be is not null, like <> or != or whatever
       ]
 );

任何提示?

'deleted_at <>' => null 中断

'deleted_at' => ['!=' => null] 也会中断

6 个答案:

答案 0 :(得分:29)

我是这样做的:

$this->seeInDatabase('diary_note...',['id' => 'a7e35ad0'])
->notSeeInDatabase('diary_note...',['id' => 'a7e35ad0','deleted_at'=>null]);

所以我正在检查两个步骤

  1. 我检查表中是否有我们的ID记录
  2. 我检查表中是否没有带有id和deleted_at = null的记录

答案 1 :(得分:13)

目前无法实现。 seeInDatabasenotSeeInDatabase都只是将数组直接传递给查询构建器的where方法,并且不了解如何处理=以外的任何内容。传递了一个数组。

https://github.com/laravel/framework/blob/2b4b3e3084d3c467f8dfaf7ce5a6dc466068b47d/src/Illuminate/Database/Query/Builder.php#L452

public function where($column, $operator = null, $value = null, $boolean = 'and')
{
    // If the column is an array, we will assume it is an array of key-value pairs
    // and can add them each as a where clause. We will maintain the boolean we
    // received when the method was called and pass it into the nested where.
    if (is_array($column)) {
        return $this->whereNested(function ($query) use ($column) {
            foreach ($column as $key => $value) {
                $query->where($key, '=', $value);
            }
        }, $boolean);
    }

    // ...
}

选项1 - 将以下代码添加到TestCase类中,您可以从

扩展测试用例

要点:https://gist.github.com/EspadaV8/73c9b311eee96b8e8a03

<?php
/**
 * Assert that a given where condition does not matches a soft deleted record
 *
 * @param  string $table
 * @param  array  $data
 * @param  string $connection
 * @return $this
 */
protected function seeIsNotSoftDeletedInDatabase($table, array $data, $connection = null)
{
    $database = $this->app->make('db');

    $connection = $connection ?: $database->getDefaultConnection();

    $count = $database->connection($connection)
        ->table($table)
        ->where($data)
        ->whereNull('deleted_at')
        ->count();

    $this->assertGreaterThan(0, $count, sprintf(
        'Found unexpected records in database table [%s] that matched attributes [%s].', $table, json_encode($data)
    ));

    return $this;
}

/**
 * Assert that a given where condition matches a soft deleted record
 *
 * @param  string $table
 * @param  array  $data
 * @param  string $connection
 * @return $this
 */
protected function seeIsSoftDeletedInDatabase($table, array $data, $connection = null)
{
    $database = $this->app->make('db');

    $connection = $connection ?: $database->getDefaultConnection();

    $count = $database->connection($connection)
        ->table($table)
        ->where($data)
        ->whereNotNull('deleted_at')
        ->count();

    $this->assertGreaterThan(0, $count, sprintf(
        'Found unexpected records in database table [%s] that matched attributes [%s].', $table, json_encode($data)
    ));

    return $this;
}

选项2 - 安装以下编辑器包

此作曲家包与上面的代码完全相同,但已打包为Composer。

composer require kirkbater/soft-deletes

然后在特定测试类中使用它:

<?php

use Kirkbater\Testing\SoftDeletes;

class MyTestClass extends TestClass {

    use SoftDeletes;

}

答案 2 :(得分:8)

这是一个老问题,但对于那些使用更新版本的Laravel(5.4及更高版本)的人来说,现在有一个assertSoftDeleted断言:documentation

原来问题的答案现在是:

$this->assertSoftDeleted('diary_note_categories', [
    'id' => 'a7e35ad0-6f00-4f88-b953-f498797042fc'
]);

答案 3 :(得分:3)

断言给定的记录已被删除(Laravel 5.4 及更高版本)。

assertSoftDeleted(string|Model $table, array $data = [], string|null $connection = null)

id 示例:

$this->assertSoftDeleted('table_name', ['id'='value'])

模型示例:

$user = User::factory()->create();
$user->delete();
$this->assertSoftDeleted($user);

答案 4 :(得分:0)

我在Laravel 6中使用

$this->assertDatabaseMissing('stores', [
'id' => $test_data['store']->id, 'deleted_at' => null
]);

$this->assertDatabaseHas('stores', ['id' => $id]);

答案 5 :(得分:-2)

没有经过测试,但请尝试这样:

$this->seeInDatabase(
       'diary_note_categories',
       [
           'id' => 'a7e35ad0-6f00-4f88-b953-f498797042fc',
           'deleted_at' => ['deleted_at' ,'!=', null ] // should be is not null, like <> or != or whatever
       ]
 );