dimanche 26 juin 2016

PHPUnit testing FAILS ONLY when visiting the root page( visit('/') ) in Laravel 5

I run Laravel application in docker container dockervel and

When I do unit testing with this test

<?php

use IlluminateFoundationTestingWithoutMiddleware;
use IlluminateFoundationTestingDatabaseMigrations;
use IlluminateFoundationTestingDatabaseTransactions;
use AppUser1;

class MyTest extends TestCase
{

    public function testExample()
    {
        $this->assertTrue(true);
    }
     public function providerAllUrisWithResponseCode()
    {
        return [
            ['/', 200],
            ['/thank', 200],
            ['/non-existing', 404],
        ];
    }

    public function testDisplayThankYou($value='')
    {
        $this->visit('/thankyou')
        ->see('THank you!');

    }


    public function testNewUserRegistration()
    {
        $this->visit('/')
         ->type('Taylor', 'name')
         ->type('taylor@talor.com', 'email')         
         ->press('Register')
         ->seePageIs('/thankyou');
    }

    public function testPageControllerGet()
    {
        $this->call('GET', '/');
    }

    public function testPageControllerGettoThankYou()
    {
        $this->call('GET', 'thankyou');
    }

    public function testUser1ControllerPost()
    {
        $response= $this->call('POST', '/');
    }


}

IT FAILS ONLY on method testNewUserRegistration when $this->visit('/') it says:

There was 1 failure:

1) MyTest::testDisplayWelcome
A request to [http://localhost] failed. Received status code [500].

.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:196
.../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:80
...vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php:61
.../www/tests/MyTest.php:43

Caused by
exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql' (2)' in ...vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:55

Here are my .env file and config/database

**.env**
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

config/database.php

<?php
return [

    'fetch' => PDO::FETCH_CLASS,
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'mysql'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
        ],
    ],

    'migrations' => 'migrations',
    'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],
    ],
];

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
            <exclude>
                <file>./app/Http/routes.php</file>
            </exclude>
        </whitelist>
    </filter>
    <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_HOST" value="mysql"/>
        <env name="DB_USERNAME" value="homestead"/>
        <env name="DB_PASSWORD" value="secret"/>
    </php>
</phpunit>

Please help!

Aucun commentaire:

Enregistrer un commentaire