longestdrive's avatar

Unable to get testing to use a testing database sqlite L5.5

I thought I had this set up cracked but I haven't. It seems no matter what I do I'm unable to get my tests to use an sqlite database to use for tests and instead keeps going to my default mysql database.

I think I've done everything as suggested in all the threads and tutorials but absolutely no luck in Laravel 5.5

In my database config I have:

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


'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],
        'sqlite_testing_memory' => [
            'driver' => 'sqlite',
            'database' => ':memory:',
            'prefix' => '',
        ],
        'sqlite_testing' => [
            'driver' => 'sqlite',
            '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,
        ],

In my Phpunit.xml I have the following:

<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_testing"/>
        <env name="DB_DEFAULT" value="sqlite_testing" />
        <env name="DB_DATABASE" value="/database/database.sqlite"/>
    </php>

I have a file - database.sqlite in my database directory

I also tried placing that file in the storage folder and changed the path to the storage folder but still didnt work.

So - what's wrong with my set up and how can I check before my tests run which database it's using?

Thanks

0 likes
4 replies
longestdrive's avatar

A bit of further testing:

If I include the following in my test set up I get:

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

        dump(env('APP_ENV'));
        dd(DB::connection()->getDatabaseName());
}

The environment is shown as local and the database name is: manager (which is on my mysql connection

so it appears the values in phpunit.xml are not being used?

longestdrive's avatar

And a bit more information:

Looking at the console I have the following:

ssh://[email protected]:2222/usr/bin/php /home/vagrant/code/golfmanager18/vendor/phpunit/phpunit/phpunit --no-configuration 

So I assume that means it's not picking up the configuration file?

How Can configure this correctly to use the configuration file at the root of my project?

longestdrive's avatar

And checking the version on command console I get:

PHPUnit 7.0.2 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.1.10-1+ubuntu16.04.1+deb.sury.org+1 with Xdebug 2.6.0
Configuration: /home/vagrant/code/golfmanager18/phpunit.xml
longestdrive's avatar
longestdrive
OP
Best Answer
Level 4

And now resolved!!!

I cleared the config cache - this appeared to work for the database but not changed the environment variable - still at local when I do this:

dump(app('env'));

Switching back to phpstorm I also specified the default configuration file and pointed it to the phpunit.xml in the project root - aghhh

So I think sorted for now

Please or to participate in this conversation.