humans/laravel-setup-traits

This package is abandoned and no longer maintained. No replacement package was suggested.

Autorun traits on PHPUnit setup.

1.1.0 2021-04-10 04:21 UTC

This package is auto-updated.

Last update: 2023-02-20 14:53:06 UTC


README

Laravel now has this built-in!

This package allows traits to be ran automatically on the setup of your tests.

Here's an example.

class PostUpdateTest extends TestCase
{
    use ActingAsEdtior;
}

trait ActingAsEditor
{
    function setupActingAsEditor()
    {
        $this->editor = factory(User::class)->states('editor');
      
        $this->be($this->editor);
    }
}

Installation

Require the package via composer.

composer require humans/laravel-setup-traits --dev

Use the trait in your base test case.

use Humans\SetupTraits\SetupTraits;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use SetupTraits;
}

Create a trait you want with the method name setup + class name

use Illuminate\Support\Facades\Notification;

trait	WithoutNotifications
{
    function setupWithoutNotifications
    {
        Notification::fake();
    }
}

Then include the trait in any of your tests!

class SettingsUpdateTest extends TestCase
{
    use WithoutNotifications;
}