You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Drupal 9 PHPUnit功能测试数据库配置错误排查

解决Drupal PHP功能测试的数据库配置问题

问题现象

运行Drupal功能测试时触发数据库相关错误:提示指定数据库不存在但实际已创建,同时无法在数据库中创建测试表,尽管使用的数据库账号与正常运行的Drupal站点完全一致。

错误日志

PHPUnit 9.5.25 #StandWithUkraine

Testing /Applications/MAMP/htdocs/dl-staging/web/modules/custom
E                                                                   1 / 1 (100%)R

Time: 00:04.717, Memory: 12.00 MB

There was 1 error:

1) Drupal\Tests\dl_micro_mas\FunctionalJavascript\QITest::testQuotesInvoices
Drupal\Core\Installer\Exception\InstallerException: Resolve all issues below to continue the installation. For help configuring your database server, see the <a href="https://www.drupal.org/docs/8/install">installation handbook</a>, or contact your hosting provider.<ul><li>Database <em class="placeholder">dl_test/</em> not found. The server reports the following message when attempting to create the database: <em class="placeholder">SQLSTATE[HY000]: General error: 1007 Can&#039;t create database &#039;dl_test&#039;; database exists</em>.</li><li>Failed to <strong>CREATE</strong> a test table on your database server with the command <em class="placeholder">CREATE TABLE {drupal_install_test} (id int NOT NULL PRIMARY KEY)</em>. The server reports the following message: <em class="placeholder">SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected: CREATE TABLE &quot;test21167818drupal_install_test&quot; (id int NOT NULL PRIMARY KEY); Array
(
)
</em>.<p>Are you sure the configured username has the necessary permissions to create tables in the database?</p></li></ul>

当前phpunit.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!-- For how to customize PHPUnit configuration, see core/tests/README.md. -->
<!-- TODO set checkForUnintentionallyCoveredCode="true" once https://www.drupal.org/node/2626832 is resolved. -->
<!-- PHPUnit expects functional tests to be run with either a privileged user
 or your current system user. See core/tests/README.md and
 https://www.drupal.org/node/2116263 for details.
-->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="web/core/tests/bootstrap.php" colors="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" beStrictAboutChangesToGlobalState="true" failOnWarning="true" printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter" cacheResult="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
  <php>
    <!-- Set error reporting to E_ALL. -->
    <ini name="error_reporting" value="32767"/>
    <!-- Do not limit the amount of memory tests take to run. -->
    <ini name="memory_limit" value="-1"/>
    <!-- Example SIMPLETEST_BASE_URL value: http://localhost -->
    <env name="SIMPLETEST_BASE_URL" value="http://www.website.build:8888"/>
    <!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
    <env name="SIMPLETEST_DB" value="mysql://root:root@localhost:8889/dl_test/"/>
    <!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/browser_output -->
    <env name="BROWSERTEST_OUTPUT_DIRECTORY" value="browser_output"/>
    <!-- To have browsertest output use an alternative base URL. For example if
     SIMPLETEST_BASE_URL is an internal DDEV URL, you can set this to the
     external DDev URL so you can follow the links directly.
    -->
    <env name="BROWSERTEST_OUTPUT_BASE_URL" value=""/>
    <!-- To disable deprecation testing completely uncomment the next line. -->
    <env name="SYMFONY_DEPRECATIONS_HELPER" value="disabled"/>
    <!-- Example for changing the driver class for mink tests MINK_DRIVER_CLASS value: 'Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver' -->
    <env name="MINK_DRIVER_CLASS" value=""/>
    <!-- Example for changing the driver args to mink tests MINK_DRIVER_ARGS value: '["http://127.0.0.1:8510"]' -->
    <env name="MINK_DRIVER_ARGS" value=""/>
    <!-- Example for changing the driver args to webdriver tests MINK_DRIVER_ARGS_WEBDRIVER value: '["chrome", { "chromeOptions": { "w3c": false } }, "http://localhost:4444/wd/hub"]' For using the Firefox browser, replace "chrome" with "firefox" -->
    <env name="MINK_DRIVER_ARGS_WEBDRIVER" value=""/>
  </php>
  <listeners>
    <listener class="\Drupal\Tests\Listeners\DrupalListener">
    </listener>
  </listeners>
  <!-- Filter for coverage reports. -->
</phpunit>

问题原因与修复方案

  • 核心问题:SIMPLETEST_DB配置项的数据库名称末尾多了一个斜杠/,导致Drupal将数据库识别为dl_test/(实际不存在),而非已创建的dl_test。后续操作因数据库名称错误,无法选中正确数据库,进而无法创建测试表。
  • 修复步骤:修改phpunit.xml中的SIMPLETEST_DB配置,删除数据库名称后的斜杠:
    <env name="SIMPLETEST_DB" value="mysql://root:root@localhost:8889/dl_test"/>
    
  • 验证逻辑:修正后系统会正确匹配已存在的dl_test数据库,无需重复创建,同时能正常执行测试表的创建操作,解决报错问题。

内容的提问来源于stack exchange,提问作者kevinius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 16:30:58