Disable mass assignment protection for all models across all tests

667 Views Asked by At

is there a way to disable mass assignment protection for all models across all tests without having to duplicate this over and over?
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']);
1

There are 1 best solutions below

0
Salim Djerbouh On BEST ANSWER

I figured it out using the TestCase every Test class extends, and the Eloquen\Model every model extends.
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();
    }
}