1
votes

So I'm trying to write unit tests for a Laravel application like this:

protected function setUp()
{
    parent::setUp();
    $this->disableMiddlewareForAllTests();
    $this->withoutExceptionHandling();
}

public function test_login_badRequest()
{
    $response = $this->call('post', '/login');
    $response->assertOk();
    $response->assertJson(['status' => 400]);
}

The above works fine, and gives this output:

Test 'Tests\Unit\LoginTest::test_login_badRequest' started
Test 'Tests\Unit\LoginTest::test_login_badRequest' ended
Time: 69 ms, Memory: 12.00MB
OK (1 test, 2 assertions)

But the next test is another story:

public function test_login_goodRequest()
{
    $response = $this->call('post', '/login', [
        'email' => '[email protected]',
        'password' => 'P4ssw0rd'
    ]);

    $response->assertOk();
    $response->assertJson(['status' => 200]);
}

As soon as the test hits this line of code in the application:

/* Retrieve User associated with posted email address */
$user = $this->userModel
    ->where('email', $request->post('email'))
    ->get();
die('here');// <-- Happens in browser, does not happen in tests.

This is the error:

1) Tests\Unit\LoginTest::test_login_goodRequest
PDOException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
/var/www/html/projects/laravel-template/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68 ...

I've been banging my head against this one for a few days, any ideas what's going on here, and how to get that die to print in the tests? Cheers.

-- Laradock, Docker, nginx, mysql, PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Apr 5 2018 08:53:57) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.4-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

EDITED:

.env:

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Z+p6uA3G2DyCCFBXOPgrWwls1U6z/vi8Zi2r4eKHxEY=
APP_DEBUG=true
DEBUGBAR_ENABLED=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=sample_database
DB_USERNAME=root
DB_PASSWORD=root

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
1
Please post your docker-compose.yml and .envAlex Karshin
@alex docker-compose.yml is 900 lines of code which part are you interested in and I'll paste it.Donal.Lynch.Msc
Just make sure that DB_HOST in .env is the same as the mysql container name. They do not specify container_name in their original docker-compose.yml, therefore docker could name your containers as application_mysql or something. If you run docker ps, you will see the container names. Also, make sure PHPUnit uses the same mysql container name when connecting, if it doesn't take the value from .env.Alex Karshin
I am 95% sure this is the issue. If it actually helps you, I'll post it as the answer.Alex Karshin
Ok brilliant, docker ps says: CONTAINER ID,IMAGE,COMMAND,CREATED,STATUS,PORTS,NAMES: f10f6deb314a, laradock_mysql, "docker-entrypoint.s…",2 weeks ago, Up 2 hours, 0.0.0.0:3306->3306/tcp, laradock_mysql_1Donal.Lynch.Msc

1 Answers

1
votes

In the chat we found out you were running the tests from your host. It is possible to run tests from host, you must expose your ports to the host though. I think laradock does that, though I'm not sure if you had it in your setting (the github version does). Although, it's a docker anti-pattern.

Containers are designed to contain all the communication between them and from host you should only be doing up and down.

Hence, the best solution is to enter your workspace container with docker exec -it workspace bash or docker-compose exec workspace bash and run tests from there.