doujiushi9007 2016-06-26 17:02
浏览 107
已采纳

Laravel中的PHPUnit错误:异常'带消息'SQLSTATE [HY000] [2005]未知的MySQL服务器主机'mysql'(2)'

I am trying to run PHPunit test for visit('/') in LAravel 5 but it fails with this message below : (my app is running in docker container http://www.spiralout.eu/2015/12/dockervel-laravel-development.html )

    class MyTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    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 testPageControllerGet()
    {
        $this->call('GET', '/');
    }
        public function testDisplayWelcome($value='')
    {
        $this->visit('/')
        ->see('Welcome');

    }
}

and I am getting this error .

        $vendor/bin/phpunit 
PHPUnit 4.8.26 by Sebastian Bergmann and contributors.

...F

Time: 24.01 seconds, Memory: 16.00MB

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/ExampleTest.php:16

    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

My behat features passed ! My .env.* files look like :

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

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

Also : If I remove or change DB_HOST=127.0.0.1 in .env.behat and I run behat it returns an error!

The line 50 in Connector.php says :

   public function createConnection($dsn, array $config, array $options)
    {
**here it breaks**-->   $username = Arr::get($config, 'username');

        $password = Arr::get($config, 'password');

        try {
            $pdo = new PDO($dsn, $username, $password, $options);
        } catch (Exception $e) {
            $pdo = $this->tryAgainIfCausedByLostConnection(
                $e, $dsn, $username, $password, $options
            );
        }

        return $pdo;
    }

For example when I run DB ::table('something')->get(); it's working fine.

Someone sees the problem?

  • 写回答

1条回答 默认 最新

  • duanjiu3486 2016-06-28 15:29
    关注

    On .env.behat you should use the mysql as you host name. It is link through docker with that name not 127.0.0.1.
    .env.behat DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?