laravelmass-assignment

Disable mass assignment protection for all models across all tests


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']);

Solution

  • 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();
        }
    }