目前这是不可能的。无论是
seeInDatabase
还是
notSeeInDatabase
,它们都会直接将数组传递给查询构建器的
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 (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
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;
}
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包与上面的代码完全相同,但被封装为Composer包。
composer require kirkbater/soft-deletes
然后在您的特定测试类中使用它:
<?php
use Kirkbater\Testing\SoftDeletes;
class MyTestClass extends TestClass {
use SoftDeletes;
}
seeIsSoftDeletedInDatabase
得到解决。 - Matt Komarnicki