gpt4 book ai didi

Symfony 4 PHPunit 未选择数据库

转载 作者:行者123 更新时间:2023-12-02 02:57:04 24 4
gpt4 key购买 nike

我尝试使用 Symfony 4 和 phpunit 6 进行一些测试,但出现错误消息:

SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected" at /var/www/userDemo/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php line 108 F
2 / 2 (100%)

namespace App\Tests;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class RegistrationControllerTest extends WebTestCase
{
public function testSomething()
{
$client = static::createClient();
$crawler = $client->request('GET', '/');

$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertSame(0, $crawler->filter('html:contains("Hello World")')->count());
}

public function testCheckPassword(){
$client = static::createClient();

$crawler = $client->request(
'GET',
'/register'
);

$form = $crawler->selectButton('S\'inscrire')->form();

$form['user[email]'] = 'toto@email.com';
$form['user[username]'] = 'usernametest';
$form['user[fullName]'] = 'John Doe';
$form['user[password][first]'] = 'pass1';
$form['user[password][second]'] = 'pass2';

$crawler = $client->submit($form);

//echo $client->getResponse()->getContent();


$this->assertEquals(1,
$crawler->filter('li:contains("This value is not valid.")')->count()
);


}

}

我的环境文件:

APP_ENV=dev APP_SECRET=XXXX DATABASE_URL=mysql://root:@127.0.0.1:3306/userdemo

该应用程序在开发环境中运行良好

谢谢!

[编辑]只需添加 phpunit.xml.dist

<env name="DATABASE_URL" value="mysql://root:@127.0.0.1/userDemo" />

谢谢@dbrumann

最佳答案

文档解释了如何很好地解决您的问题 Changing Database Settings for Functional Tests :

If you have functional tests, you want them to interact with a real database. Most of the time you want to use a dedicated database connection to make sure not to overwrite data you entered when developing the application and also to be able to clear the database before every test.

To do this, you can override the value of the DATABASE_URL env var in the phpunit.xml.dist to use a diferent database for your tests:

<?xml version="1.0" charset="utf-8" ?>
<phpunit>
<php>
<!-- the value is the Doctrine connection string in DSN format -->
<env name="DATABASE_URL" value="mysql://USERNAME:PASSWORD@127.0.0.1/DB_NAME" />
</php>
</phpunit>

关于Symfony 4 PHPunit 未选择数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48085404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com