Bom_Basti's avatar

Empty database after test

Hey guys

I want to start with TDD and have a strange problem:

I've added these lines to my phpunit.xml:

<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"/>
        <env name="DB_DATABASE" value=":memory:"/>
</php>

and this is my test file:

namespace Tests\Feature\Admin;

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

class AdminTest extends TestCase
{
    use RefreshDatabase;

    /** @test */
    function a_user_cant_see_admin_area()
    {
        $this->actingAs(factory(User::class)->create());

        $this->get('/admin/index')
            ->assertStatus(403);
    }
}

I thought I've set everything up to use the memory database and don't test against my MySQL database. But after every test my MySQL database is empty.

What am I doing wrong?

0 likes
5 replies
tykus's avatar

Have you done something with your database.php config file to override the stock config?

Bom_Basti's avatar

Thank you for your response @tykus

This is my config/database.php:

<?php

return [
    
    'default' => env('DB_CONNECTION', 'mysql'),

    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
            'modes' => [
                'STRICT_TRANS_TABLES',
                'NO_ZERO_IN_DATE',
                'NO_ZERO_DATE',
                'ERROR_FOR_DIVISION_BY_ZERO',
                'NO_AUTO_CREATE_USER',
                'NO_ENGINE_SUBSTITUTION',
            ],
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '1433'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
        ],

    ],

    'migrations' => 'migrations',

    'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

];

I striped out the comment blocks.

tykus's avatar

Nothing apparently wrong there. Are you doing anything to set the default connection explicitly which would override the config?

Can you temporarily add the following tearDown method to the TestCase class to check the DB_CONNECTION environment variable used for each test?

    function tearDown()
    {
        dump(env('DB_CONNECTION'));
    }
Bom_Basti's avatar

Oh, it's so unpleasant to me.

I just realized that if I run the test from my terminal my MySQL database stays the same. Only if I run the test from PhpStorm the database got cleared.

I forgot to set the phpunit.xml in my test configuration inside PhpStorm...

I'm sorry to steal your time!

1 like

Please or to participate in this conversation.