2016-06-26 65 views
1

我試圖運行PHPunit測試訪問('/')在LAravel 5但它失敗,下面這條消息: PHPUnit在Laravel錯誤:異常''與消息'SQLSTATE [HY000] [2005]未知MySQL服務器主機'mysql'(2)'

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'); 

    } 
} 

我的應用程序在泊塢窗容器http://www.spiralout.eu/2015/12/dockervel-laravel-development.html運行),我收到此錯誤

 $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 

我的behat功能通過了! 。 我.ENV *文件看起來像:

.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 

另外:如果我刪除或更改DB_HOST = 127.0.0.1在.env.behat和我跑貝哈特它返回一個錯誤!

Connector.php線50說:

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; 
    } 

例如,當我運行DB ::表( '東西') - >獲得();它工作正常。

有人看到這個問題?

+0

似乎是一個環境配置問題,DB_HOST必須在測試環境中設置爲'mysql'。 – Devon

+0

如果您的.env中的DB_HOST設置爲'localhost'而不是'mysql',該怎麼辦? – ishadif

+0

@Devon是我的測試環境,與Laravel應用程序相同。環境? – moonlight

回答

1

.env.behat你應該使用mysql作爲你的主機名。它通過docker與該名稱不是127.0.0.1鏈接。
.env.behat DB_CONNECTION=mysql DB_HOST=mysql DB_PORT=3306 DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret

+0

我改變了它,並嘗試所有combinatations,你可以看到我發佈這個問題http://stackoverflow.com/questions/38043467/phpunit-testing-fails-only-when-visiting-the-root-page-visit-in -laravel?noredirect = 1#comment63548754_38043467我不知道有什麼問題。如果你得到它,請幫助。 – moonlight

+0

'DB_HOST = mysql' –

相關問題