hepabolu's avatar

sticky database name across environments

I'm working through the Laracasts series on how to build a Forum and try to apply the instructions to my own app (Laravel 5.6).

In one of the first episodes Jeffrey defines a test database as an in memory sqlite database while he manually tests in the browser using a MySQL database.

I've tried to set up my own configuration similar to his setup but whatever I do, the unit tests drop all the tables in my local database rather than using the database that is configured in either phpunit.xml, or for the 'testing' environment (a different MySQL database called 'testing').

I've tried to fix this problem, but can't figure out what's going on. Is there any way I can troubleshoot this?

0 likes
9 replies
bobbybouwmann's avatar
Level 88

How do you run the tests?

Also did you run php artisan config:clear! It would be cached somehow!

hepabolu's avatar

I run the tests simply using

vendor/bin/phpunit

and

php artisan dusk

from the root dir of my app.

I think I've run

php artisan config:clear

just checking right now.

bobbybouwmann's avatar

What value is set to the DB_CONNECTION field in the phpunit.xml file? That should be testing in your case!

hepabolu's avatar

Looks like that was it! Thanks so much.

hepabolu's avatar

@bobbybouwmann it is set to testing. I'm pretty sure I've cleared the config cache, but it looks like it's working now.

hepabolu's avatar

Unfortunately I'm back where I started. In my code I need some complex SQL statements where MySQL and Sqlite differ in code. So I had to revert to using a MySQL database for testing. So the only thing I did in the working setup described before is change the database to 'mysql' I left everything else the same. So here are the relevant config settings:

# .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=localdb


DB_TEST_DATABASE=testing

config/database.php

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
...
],
'sqlite_testing' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => 'testing',
...
]

phpunit.xml

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

hepabolu's avatar

To answer my own question (for others who might run into this):

I narrowed the problem down to php artisan using the wrong environment even when I cleared caches.

I found this: https://hackernoon.com/how-i-destroyed-the-staging-database-b435f98569ab

and took precautions to throw an exception when the testing database wasn't used.

And I created a shell script that does three things:

  • APP_ENV=testing
  • clear all the various caches with php artisan
  • run phpunit

I can now verify that the test database is used.

Please or to participate in this conversation.