Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - setting up a test database

With my laravel application I have a bunch of migrations and db seed data. In app/config/testing/database.php I have set a mysql database for testing.

I've populated the test database with migrate and db:seed commands, specifying the testing environment.

This works pretty well, but I want to call those commands each time I run phpunit. If it do it in the setUp then it takes so long as it's called on every single test.

I can also call it in the setUpBeforeClass which is better, but still longer if I am testing 10 classes.

Is there a way I can all it just once when I run phpunit ?

like image 257
Wizzard Avatar asked Mar 19 '23 01:03

Wizzard


1 Answers

For now I have done this, in the tests/TestCase.php

class TestCase extends Illuminate\Foundation\Testing\TestCase {

    public static $setupDatabase = true;

    public function setUp()
    {
        parent::setUp();
        if(self::$setupDatabase)
        {
            $this->setupDatabase();
        }
    }

    public function setupDatabase()
    {
        Artisan::call('migrate');
        Artisan::call('db:seed');
        self::$setupDatabase = false;
    }

    ....

which seems to work, but doesn't feel ideal, but means I can easily re-setup the db if I need to reset it for a test...

like image 61
Wizzard Avatar answered Mar 31 '23 20:03

Wizzard