mau*_*fle 7 php symfony doctrine-orm symfony4
Symfony4中的奇怪问题:Doctrine有效,我可以使用验证模式,创建数据库等php bin/console doctrine:schema:create.但我的PHPUnit测试没有连接.通过运行./bin/phpunit我得到SQLSTATE[HY000] [2002] No such file or directory例外.
我按照文档中有关启动内核的步骤进行操作:http: //symfony.com/doc/current/testing/doctrine.html
我的代码:
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
}
}
Run Code Online (Sandbox Code Playgroud)
谁知道为什么会这样?一个bug?
编辑:
显然.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" />
<!-- ###+ 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
601 次 |
| 最近记录: |