PHPUnit in Laravel error : Exception' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql' (2)'
PHPUnit in Laravel error : Exception' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql' (2)'
我正在尝试 运行 PHPunit 测试 visit('/') in LAravel 5 但失败并显示以下消息:
(我的应用 运行 正在 docker 容器 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');
}
}
我遇到这个错误。
$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
我的行为特征通过了!
我的 .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
另外:如果我在 .env.behat 中删除或更改 DB_HOST=127.0.0.1 并且我 运行 是 returns 一个错误!
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 ::table('something')->get();一切正常。
有人看到问题了吗?
在 .env.behat
上,您应该使用 mysql
作为您的主机名。它是 link 到 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
我正在尝试 运行 PHPunit 测试 visit('/') in LAravel 5 但失败并显示以下消息: (我的应用 运行 正在 docker 容器 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');
}
}
我遇到这个错误。
$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
我的行为特征通过了! 我的 .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
另外:如果我在 .env.behat 中删除或更改 DB_HOST=127.0.0.1 并且我 运行 是 returns 一个错误!
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 ::table('something')->get();一切正常。
有人看到问题了吗?
在 .env.behat
上,您应该使用 mysql
作为您的主机名。它是 link 到 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