Symfony4:Doctrine2工作但PHPUnit测试中没有连接(内核启动)

时间:2018-01-09 15:06:41

标签: php symfony doctrine-orm symfony4

Symfony4中的奇怪问题:Doctrine有效,我可以使用private void showPage(int index) { if (mPdfRenderer.getPageCount() <= index) { return; } if (null != mCurrentPage) { mCurrentPage.close(); } mCurrentPage = mPdfRenderer.openPage(index); int newWidth = (int) (mVerticalScrollView.getWidth() * currentZoomLevel); int newHeight = (int) (newWidth * ((float)mCurrentPage.getHeight()/(float)mCurrentPage.getWidth())); Bitmap bitmap = Bitmap.createBitmap( newWidth, newHeight, Bitmap.Config.ARGB_8888); mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); mImageView.setImageBitmap(bitmap); updateUi(); 验证架构,创建数据库等。但我的PHPUnit测试没有连接。通过运行php bin/console doctrine:schema:create,我得到./bin/phpunit例外。

我按照文档中有关启动内核的步骤操作: http://symfony.com/doc/current/testing/doctrine.html

我的代码:

SQLSTATE[HY000] [2002] No such file or directory

任何人都知道为什么会这样吗?一个错误?

编辑:

显然,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 } } 文件无法正确读取。我将.env环境变量移动到DATABASE_URL文件中,现在它可以正常工作。

1 个答案:

答案 0 :(得分:7)

您必须将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>