wizjo
475
16
Testing

Phpunit configuration refresh my database

Posted 2 months ago by wizjo

When I run phpunit --stop-on-failure command I got an error:

Tests\Unit\ExampleTest::testBasicTest
Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'project_id' doesn't exist in table (SQL: alter table `tasks` add constraint `tasks_project_id_foreign` foreign key (`project_id`) references `projects` (`id`) on delete cascade)

After that I noticed my database is missing most of the tables, so it looks like test makes database migration reset.

i cannot find the reason of that because:

  1. My configuration points to doing tests in memory
  2. Im running only default Laravel test

Here is my configuration:

phpunit.xml

<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_testing"/>

config\database.php

    'connections' => [

        //...

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

    ],

default unit test - tests\Unit\ExampleTest.php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ExampleTest extends TestCase
{
    use DatabaseMigrations;

    public function testBasicTest()
    {
        $this->assertTrue(true);
    }
}

Commenting out line use DatabaseMigrations; makes that database is not resetting, but it doesn`t solve the problem. Database for testing should be separate.

What am I missing?

Please sign in or create an account to participate in this conversation.