clarkeash's avatar

DatabaseTransactions, DatabaseMigrations have no effect.

I have the following test class


use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ProvidersTest extends TestCase
{
    use DatabaseMigrations;

    /**
     * @var \Orka\Entities\User
     */
    protected $user;

    /**
     * @var \Orka\Entities\Team
     */
    protected $team;

    public function setUp()
    {
        parent::setUp();

        $team = factory(\Orka\Entities\Team::class)->create();
        $user = factory(\Orka\Entities\User::class)->create();
        $user->attachTeam($team);

        $this->user = $user;
        $this->team = $team;
    }

    /**
     * @test
     */
    public function it_shows_no_connected_providers()
    {
        $this
            ->actingAs($this->user)
            ->visit('/teams/1/providers')
            ->see('You have not connected a provider yet.')
        ;
    }
}

When running this code I get an error telling me tables do no exist, the only way I can get it to work is to call $this->runDatabaseMigrations(); in the setUp method, but as far as I know I should not need to do that. I have similar issues with DatabaseTransactions.

Laravel 5.1.23

Any ideas on why this is happening?

0 likes
10 replies
clarkeash's avatar

This seems to be a issue with phpunit, the trait provides a method with a @before annotation which is not being triggered.

I have added the raw code from the trait into my test class:

    /**
     * @before
     */
    public function runDatabaseMigrations()
    {
        $this->artisan('migrate');

        $this->beforeApplicationDestroyed(function () {
            $this->artisan('migrate:rollback');
        });
    }

And this still did not get picked up automatically, so it seems phpunit is ignoring it.

floogman's avatar

The trait's runDatabaseMigrations method is called after the setUp method has run. In the setUp method the database tables do not yet exist.

pcross's avatar

I believe I am having a similar problem. I'm using the DatabaseMigrations trait. If my test implements it's own setUp method, then traits do not work. If I remove the setup method traits work again.

It seems to be resolved by manually calling the trait method from within my setup method, but as for why the traits stop working when a setup method is implemented, I have no idea :(

1 like
johnberberich's avatar

Thanks Jeffrey. Your fix hasn't been rolled into Laravel 5.1, however, so this is still an issue there. I commented on Taylor's commit, so hopefully he'll notice and make the changes.

marcor's avatar

@JeffreyWay It seems that it is still not included in 5.4. can you please check it

2 likes

Please or to participate in this conversation.