使用 Lando 从 PHPStorm 执行 PHPUnit 测试用例
Execute PHPUnit Testcases from PHPStorm using Lando
我已经使用 Lando 提供的 Docker 容器成功配置了 PHPStorm。不过,当我尝试执行测试时,例如,core/modules/system/tests/src/Functional/System/HtaccessTest.php
会抛出以下错误:
Testing started at 1:00 AM ...
[docker://devwithlando/php:7.2-apache-2/]:php /opt/project/vendor/phpunit/phpunit/phpunit --configuration /opt/project/phpunit.xml --filter "/(Drupal\Tests\system\Functional\System\HtaccessTest::testIndexphpRewrite)( .*)?$/" --test-suffix HtaccessTest.php /opt/project/core/modules/system/tests/src/Functional/System --teamcity
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.
Testing /opt/project/core/modules/system/tests/src/Functional/System
Drupal\Core\Installer\Exception\AlreadyInstalledException : <ul>
<li>To start over, you must empty your existing database and copy <em>default.settings.php</em> over <em>settings.php</em>.</li>
<li>To upgrade an existing installation, proceed to the <a href="/update.php">update script</a>.</li>
<li>View your <a href="http://my-lando-app.lndo.site">existing site</a>.</li>
</ul>
/opt/project/core/includes/install.core.inc:534
/opt/project/core/includes/install.core.inc:114
/opt/project/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php:296
/opt/project/core/tests/Drupal/Tests/BrowserTestBase.php:573
/opt/project/core/tests/Drupal/Tests/BrowserTestBase.php:406
Time: 22.67 seconds, Memory: 6.00MB
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Process finished with exit code 2
我胡乱猜测问题可能出在数据库连接上,因此我尝试了以下所有选项,即:
mysql://drupal8:drupal8@database:3306/drupal8
mysql://drupal8:drupal8@database/drupal8
mysql://drupal8:drupal8@localhost:32860/drupal8
<!-- 32860 is nothing but the port for external_connection. -->
<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://my-lando-app.lndo.site/"/>
<!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
<env name="SIMPLETEST_DB" value="mysql://drupal8:drupal8@database:3306/drupal8"/>
<!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/app/sites/simpletest/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 phantomjs tests MINK_DRIVER_ARGS_PHANTOMJS value: '["http://127.0.0.1:8510"]' -->
<env name="MINK_DRIVER_ARGS_PHANTOMJS" 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>
我还附上了我的 lando info
命令的输出,以防万一它是否有用:
[ { service: 'appserver',
urls:
[ 'https://localhost:32861',
'http://localhost:32862',
'http://my-lando-app.lndo.site/',
'https://my-lando-app.lndo.site/' ],
type: 'php',
healthy: true,
via: 'apache',
webroot: '.',
config: { php: '/Users/rishi/.lando/config/drupal8/php.ini' },
version: '7.2',
meUser: 'www-data',
hostnames: [ 'appserver.mylandoapp.internal' ] },
{ service: 'database',
urls: [],
type: 'mysql',
healthy: true,
internal_connection: { host: 'database', port: '3306' },
external_connection: { host: '127.0.0.1', port: '32860' },
healthcheck: 'bash -c "[ -f /bitnami/mysql/.mysql_initialized ]"',
creds: { database: 'drupal8', password: 'drupal8', user: 'drupal8' },
config: { database: '/Users/rishi/.lando/config/drupal8/mysql.cnf' },
version: '5.7',
meUser: 'www-data',
hostnames: [ 'database.mylandoapp.internal' ] } ]
你需要运行
$ docker network ls
然后复制 "network id" 为 "appserver"。
现在,让我们更新您的 PHPStorm 设置。转到 "Languages > PHP > Test Frameworks"
然后单击 "Docker Container" 定义旁边的文件夹图标。
现在,将 Network Mode
更改为您在开头复制的 network id
。
我已经使用 Lando 提供的 Docker 容器成功配置了 PHPStorm。不过,当我尝试执行测试时,例如,core/modules/system/tests/src/Functional/System/HtaccessTest.php
会抛出以下错误:
Testing started at 1:00 AM ...
[docker://devwithlando/php:7.2-apache-2/]:php /opt/project/vendor/phpunit/phpunit/phpunit --configuration /opt/project/phpunit.xml --filter "/(Drupal\Tests\system\Functional\System\HtaccessTest::testIndexphpRewrite)( .*)?$/" --test-suffix HtaccessTest.php /opt/project/core/modules/system/tests/src/Functional/System --teamcity
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.
Testing /opt/project/core/modules/system/tests/src/Functional/System
Drupal\Core\Installer\Exception\AlreadyInstalledException : <ul>
<li>To start over, you must empty your existing database and copy <em>default.settings.php</em> over <em>settings.php</em>.</li>
<li>To upgrade an existing installation, proceed to the <a href="/update.php">update script</a>.</li>
<li>View your <a href="http://my-lando-app.lndo.site">existing site</a>.</li>
</ul>
/opt/project/core/includes/install.core.inc:534
/opt/project/core/includes/install.core.inc:114
/opt/project/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php:296
/opt/project/core/tests/Drupal/Tests/BrowserTestBase.php:573
/opt/project/core/tests/Drupal/Tests/BrowserTestBase.php:406
Time: 22.67 seconds, Memory: 6.00MB
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Process finished with exit code 2
我胡乱猜测问题可能出在数据库连接上,因此我尝试了以下所有选项,即:
mysql://drupal8:drupal8@database:3306/drupal8
mysql://drupal8:drupal8@database/drupal8
mysql://drupal8:drupal8@localhost:32860/drupal8
<!-- 32860 is nothing but the port for external_connection. -->
<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://my-lando-app.lndo.site/"/>
<!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
<env name="SIMPLETEST_DB" value="mysql://drupal8:drupal8@database:3306/drupal8"/>
<!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/app/sites/simpletest/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 phantomjs tests MINK_DRIVER_ARGS_PHANTOMJS value: '["http://127.0.0.1:8510"]' -->
<env name="MINK_DRIVER_ARGS_PHANTOMJS" 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>
我还附上了我的 lando info
命令的输出,以防万一它是否有用:
[ { service: 'appserver',
urls:
[ 'https://localhost:32861',
'http://localhost:32862',
'http://my-lando-app.lndo.site/',
'https://my-lando-app.lndo.site/' ],
type: 'php',
healthy: true,
via: 'apache',
webroot: '.',
config: { php: '/Users/rishi/.lando/config/drupal8/php.ini' },
version: '7.2',
meUser: 'www-data',
hostnames: [ 'appserver.mylandoapp.internal' ] },
{ service: 'database',
urls: [],
type: 'mysql',
healthy: true,
internal_connection: { host: 'database', port: '3306' },
external_connection: { host: '127.0.0.1', port: '32860' },
healthcheck: 'bash -c "[ -f /bitnami/mysql/.mysql_initialized ]"',
creds: { database: 'drupal8', password: 'drupal8', user: 'drupal8' },
config: { database: '/Users/rishi/.lando/config/drupal8/mysql.cnf' },
version: '5.7',
meUser: 'www-data',
hostnames: [ 'database.mylandoapp.internal' ] } ]
你需要运行
$ docker network ls
然后复制 "network id" 为 "appserver"。
现在,让我们更新您的 PHPStorm 设置。转到 "Languages > PHP > Test Frameworks"
然后单击 "Docker Container" 定义旁边的文件夹图标。
现在,将 Network Mode
更改为您在开头复制的 network id
。