dockerphpunitdrupal-8lando

Execute PHPUnit Testcases from PHPStorm using Lando


I've successfully configured the PHPStorm with the Docker containers served by Lando. Still, when I'm trying to execute the test, for instance, the core/modules/system/tests/src/Functional/System/HtaccessTest.php it's throwing me the following error:

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

I made a wild guess the issue could have been with the database connection, hence I've tried with all the following options i.e.:

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>

I'm also attaching the output of my lando info command, just in case if it's helpful:

[ { 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' ] } ]

Solution

  • You need to run

    $ docker network ls
    

    Then copy the "network id" for the "appserver".

    Now, lets update your PHPStorm settings. Go to "Languages > PHP > Test Frameworks"

    enter image description here

    Then click the folder icon next to the "Docker Container" definition.

    enter image description here

    Now, change the Network Mode to your network id that you copied at the beginning.