apecengo's avatar

Problems with in-memory database testing and RefreshDatabase trait

I'm having a bit of a problem when testing with the in-memory database and the RefreshDatabase trait. It seems that the database is not being refreshed after each test.

<?php

namespace Tests\Unit\User;

use App\Name;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AttributesTest extends TestCase
{
    use RefreshDatabase;

    public function testGUIDConversion()
    {
        $user = factory(User::class)->make([
            'steamid' => '76561198053789373'
        ]);
        $this->assertEquals('f9d6b3ef5b542faced6353e7fa69f4b7', $user->guid);
    }
}

When I execute this test alone, it throws the following error:

Tests\Unit\User\AttributesTest::testGUIDConversion
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '76561198053789373' for key 'users_steamid_unique' (SQL: insert into `users` (`email`, `steamid`, `remember_token`, `guid`) values ([email protected], 76561198053789373, QrATVFge3c, f9d6b3ef5b542faced6353e7fa69f4b7))

The SteamID is marked as unique in the database, but the RefreshDatabase trait should reset it. For some reason, it doesn't. I suspect that the database is not being reset for some reason.

phpunit.xml env declarations:

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_CONNECTION" value="sqlite_test"/>
    </php>

And my sqlite_test declaration in config/database.php:

        'sqlite_test' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ],

I hope that somebody will help me figure this out.

Thanks in advance

0 likes
0 replies

Please or to participate in this conversation.