禁用所有测试中所有模型的批量分配保护
Disable mass assignment protection for all models across all tests
有没有办法在所有测试中禁用所有模型的批量分配保护,而不必一遍又一遍地复制它?
FooTest
Foo::unguard();
Bar::unguard();
Baz::unguard();
Foo::create(['column' => 'value']);
Bar::create(['column' => 'value']);
Baz::create(['column' => 'value']);
BarTest
Foo::unguard();
Bar::unguard();
Baz::unguard();
Foo::create(['column' => 'value']);
Bar::create(['column' => 'value']);
Baz::create(['column' => 'value']);
BazTest
Foo::unguard();
Bar::unguard();
Baz::unguard();
Foo::create(['column' => 'value']);
Bar::create(['column' => 'value']);
Baz::create(['column' => 'value']);
我使用 TestCase
每个测试 class 扩展,以及 Eloquen\Model
每个模型扩展。
tests/TestCase.php
<?php
namespace Tests;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, DatabaseMigrations;
public function setUp(): void
{
parent::setUp();
Model::unguard();
}
}
有没有办法在所有测试中禁用所有模型的批量分配保护,而不必一遍又一遍地复制它?
FooTest
Foo::unguard();
Bar::unguard();
Baz::unguard();
Foo::create(['column' => 'value']);
Bar::create(['column' => 'value']);
Baz::create(['column' => 'value']);
BarTest
Foo::unguard();
Bar::unguard();
Baz::unguard();
Foo::create(['column' => 'value']);
Bar::create(['column' => 'value']);
Baz::create(['column' => 'value']);
BazTest
Foo::unguard();
Bar::unguard();
Baz::unguard();
Foo::create(['column' => 'value']);
Bar::create(['column' => 'value']);
Baz::create(['column' => 'value']);
我使用 TestCase
每个测试 class 扩展,以及 Eloquen\Model
每个模型扩展。
tests/TestCase.php
<?php
namespace Tests;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, DatabaseMigrations;
public function setUp(): void
{
parent::setUp();
Model::unguard();
}
}