Symfony4:Doctrine2可以运行,但是PHPUnit测试中没有连接(内核已启动) [英] Symfony4: Doctrine2 works but no connection in PHPUnit test (kernel booted)
问题描述
Symfony4中的奇怪问题:原理有效,我可以使用 php bin / console doctrine:schema:create
验证架构,创建数据库等。但是我的PHPUnit测试没有连接。通过运行 ./ bin / phpunit
,我得到 SQLSTATE [HY000] [2002]没有这样的文件或目录
异常。 / p>
我遵循了文档中有关启动内核的步骤:
http://symfony.com/doc/current/testing/doctrine.html
我的代码:
类PersistingResultTest扩展了KernelTestCase
{
受保护的$ em;
受保护的函数setUp()
{
$ kernel = self :: bootKernel();
$ this-> em = $ kernel-> getContainer()
-> get(’doctrine’)
-> getManager();
}
公共功能testPersistingLogFile()
{
//仅是一个简单的模式命令来测试连接
$ tool = new SchemaTool($ this- > em);
$ tool-> createSchema($ this-> em-> getMetadataFactory()-> getAllMetadata());
}
受保护的函数tearDown()
{
parent :: tearDown();
$ this-> em-> close();
$ this-> em = null; // //避免内存泄漏
}
}
有人知道为什么会这样吗?是错误吗?
编辑:
显然是 .env
文件未正确读取。我将 DATABASE_URL
环境变量移到了 doctrine.yml
文件中,现在它可以工作了。
您必须将 DATABASE_URL
放入您的 phpunit.xml
文件,例如像这样:
< php>
< ini name = error_reporting value =-1 />
< env name = KERNEL_CLASS value = App\Kernel />
< env name = APP_ENV value = test />
<!-### +教义/教义组合###->
<!-在http://docs.doctrine-project.org/projects/doctrine-dbal/zh-CN/latest/reference/configuration.html#connecting-using-a-url->中描述的格式;
<!-对于SQLite数据库,使用: sqlite:///%kernel.project_dir%/var/data.db->
<!-在config / packages / doctrine.yaml中配置数据库驱动程序和server_version->
< env name = DATABASE_URL value = mysql://root@127.0.0.1:3306 / symfony4-database />
< ;!-###-学说/学说束###->
< / php>
Strange issue in Symfony4: Doctrine works, I can validate the schema, create the database etc using php bin/console doctrine:schema:create
. But my PHPUnit test does not have a connection. By running ./bin/phpunit
I get SQLSTATE[HY000] [2002] No such file or directory
exception.
I followed the steps within the docs regarding booting up the kernel: http://symfony.com/doc/current/testing/doctrine.html
My code:
class PersistingResultTest extends KernelTestCase
{
protected $em;
protected function setUp()
{
$kernel = self::bootKernel();
$this->em = $kernel->getContainer()
->get('doctrine')
->getManager();
}
public function testPersistingLogFile()
{
// Just a simple schema command to test connection
$tool = new SchemaTool($this->em);
$tool->createSchema($this->em->getMetadataFactory()->getAllMetadata());
}
protected function tearDown()
{
parent::tearDown();
$this->em->close();
$this->em = null; // avoid memory leaks
}
}
Anyone know why this happens? A bug?
EDIT:
Apparently the .env
file is not read properly. I moved the DATABASE_URL
environment variable into the doctrine.yml
file and now it works.
You have to put the DATABASE_URL
into your phpunit.xml
file, e.g. like this:
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />
<env name="APP_ENV" value="test" />
<!-- ###+ doctrine/doctrine-bundle ### -->
<!-- Format described at http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url -->
<!-- For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db" -->
<!-- Configure your db driver and server_version in config/packages/doctrine.yaml -->
<env name="DATABASE_URL" value="mysql://root@127.0.0.1:3306/symfony4-database"/>
<!-- ###- doctrine/doctrine-bundle ### -->
</php>
这篇关于Symfony4:Doctrine2可以运行,但是PHPUnit测试中没有连接(内核已启动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!