I run Laravel applic ation in docker container dockervel and
the test
I think there are 2 things happening in your code snippet.
The first thing is that you are defining the host as mysql
, not the driver (which i think is the one you are trying to set). Try setting the environment variable DB_CONNECTION
to mysql
instead of DB_HOST
.
The second thing is that you need to add the DatabaseMigrations
trait to your test class, try adding this:
<?php
// Imports
class MyTest extends TestCase {
use DatabaseMigrations; // This line
...
}
Without this your tests will probably fail as it will find the database, but it won't find any tables created inside it.
Hope it helps.
EDIT
Best practice for testing is to either mock dependencies that need to communicate with the database, or use an in-memory database for your tests to separate them from your real database.
In order to separate your testing database from your real database, you could use the sqlite
driver in memory. Just update your sqlite
driver in your database.php
file like this:
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
And use it as the DB_CONNECTION
environment variable in your tests (modifying phpunit.xml
):
<env name="DB_CONNECTION" value="sqlite"/>
try this:
see if mysql
container is up:
docker ps
You should see a mysql
container. If not then run dstop
& dup
try an other version much easier. It is based in PHP 7 and is ready to run phpunit tests in one comand dunit
$ dstop
$ git checkout builder
$ . ./aliases.sh
$ dup
$ dunit