abkrim's avatar
Level 13

QueryException: Database (test.sqlite) does not exist

The scenario is somewhat complicated since I use two connections, one of mysql and another sqlite in production. On the other hand, I have the App and several packages, since it is an application to synchronize between different accounting systems.

The tests I am trying to perform at the application level.

Now when I want to test (I do not have much knowledge of the test system)

My phpunit.xml

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="DB_CONNECTION" value="sqlite2"/>
    <env name="DB_DATABASE" value="test.sqlite"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
</php>

My config/database.php

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => database_path('database.sqlite'),
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    ],

    'sqlite2' => [
        'driver' => 'sqlite',
        'database' => env('DB_DATABASE', database_path('test.sqlite')),
        'prefix' => '',
        'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
    ],

My ExampleTest.php

<?php

namespace Tests\Feature;

use Abkrim\Contawhmcs\Models\WhmcsInvoice;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    use DatabaseTransactions;

    /** @test */
    function if_test_get_whmcs_invoice()
    {
        // Given
        factory(WhmcsInvoice::class, 2)->create();
        // When
        // Then
        // Should
    }
}    

UserFactory.php

<?php

use Faker\Generator as Faker;

...
$factory->define(App\User::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'email_verified_at' => now(),
        'password' => 'y$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
        'remember_token' => str_random(10),
    ];
});
$factory->define(\Abkrim\Contawhmcs\Models\WhmcsInvoice::class, function (Faker $faker) {
    return [
        'sage_id' => null,
        'user_id' => 1,  // TODO pending create user
        'invoice_num' => config('api-sage.invoice_prefix').'1',
        'date' => $faker->dateTimeThisYear,
        'status' => "No contabilizada"
    ];
});

Sqlite file create with touch

./database/test.sqlite

Runing test show

1) Tests\Feature\ExampleTest::if_test_get_whmcs_invoice
Illuminate\Database\QueryException: Database (test.sqlite) does not exist. (SQL: PRAGMA foreign_keys = ON;)

Well, I think that I've a mistake and Test system is not create migrations on sqlite database test.

I'm working with Laravel 5.7

0 likes
3 replies
realrandyallen's avatar

Make this change in your phpunit.xml file:

<env name="DB_DATABASE" value="database/test.sqlite"/>

That should resolve the 'Database does not exist' error - after that you'll probably run into another error about your users table not existing, as you and @talinon stated the migrations are probably not being run.

abkrim's avatar
Level 13

A lot of thanks to @talinon and @realrandyallen

I learn about test in Laravel and now I get fine using sqlite in memory.

<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
<env name="DB_DATABASE" value=":memory:"/>

And also I'm using

use RefreshDatabase;
2 likes

Please or to participate in this conversation.