Run PHPUnit Tests On Lando In PHPStorm

Lando makes it easy to set up local Drupal environments for development. A default installation will provide a webserver and database container, where you can install Drupal. The goal of this page is to explain how to configure Lando for testing Drupal, and connect it to PHPStorm.

Setup

core/phpunit.xml.dist 
Configuration file for phpunit. Copy to phpunit.xml in your project root. 
Set environment variables for phpunit in here. You can set them in the lando file, but they are easier to change here.

Environment variables 
SIMPLETEST_BASE_URL=http://SITE.lndo.site <-- don't use https. 
SIMPLETEST_DB=mysql://drupal10:drupal10@database/drupal10

 

PHPStorm configuration

Settings > Build, Execution, Deployment > Docker

Create a new Docker server, connect with unix socket.

Configure virtual machine path mapping (/app -> /home/USER/SITE/PATH/.

Settings > PHP

Select language level according to your project.

Add a new CLI Interpreter, Select "From Docker, Vagrant, WSL, etc."

Select Docker Compose, add configuration files for appserver from ~/.lando/compose/SITE.

Select appserver as the service, Connect to existing container (docker-compose exec).

Settings > PHP > Test Frameworks

Add a new Test Framework - PHPUnit by Remote Interpreter.

Select your CLI interpreter.

Set Default configuration file /app/phpunit.xml. Set SIMPLETEST variables in here.

Run > Edit Configurations

Edit configuration template for PHPUnit

Select your CLI interpreter.

Set Docker compose command and options exec -u www-data.

Unit tests

Minimal dependencies.

lando php /app/vendor/bin/phpunit -c /app/web/core/phpunit.xml.dist /app/web/core/tests/Drupal/Tests/Core/DrupalTest.php
Testing Drupal\Tests\Core\DrupalTest
..................................                                34 / 34 (100%)
Time: 00:00.067, Memory: 12.00 MB
OK (34 tests, 77 assertions)

Kernel tests

Bootstraps Drupal for database use.

SIMPLETEST_DB and SIMPLETEST_BASE_URL defaults are set correctly by lando.

See https://github.com/lando/core/blob/main/plugins/lando-recipes/types/drupaly/builder.js#L61

lando php /app/vendor/bin/phpunit -c /app/web/core/phpunit.xml.dist /app/web/core/tests/Drupal/KernelTests/Core/Entity/EntityFieldTest.php
Testing Drupal\KernelTests\Core\Entity\EntityFieldTest
.............                                                     13 / 13 (100%)

Time: 00:38.586, Memory: 10.00 MB

OK (13 tests, 1841 assertions)

Functional Tests

Load pages and check output.

lando php /app/vendor/bin/phpunit -c /app/phpunit.xml /app/web/core/tests/Drupal/FunctionalTests/Image/ToolkitSetupFormTest.php
Testing Drupal\FunctionalTests\Image\ToolkitSetupFormTest
..                                                                  2 / 2 (100%)
Time: 00:09.087, Memory: 8.00 MB
OK (2 tests, 33 assertions)

Functional Javascript Tests

Need chromedriver configured.

Nightwatch Tests

New Javascript Test format.

I'm not able to run these from PHPStorm. https://youtrack.jetbrains.com/issue/WI-72409/Run-nightwatch-tests-in-d…

lando nightwatch tests/Drupal/Nightwatch/Tests/Olivero/oliveroColorTest.js
yarn run v1.22.19
$ node -r dotenv-safe/config ./node_modules/.bin/nightwatch --config ./tests/Drupal/Nightwatch/nightwatch.conf.js tests/Drupal/Nightwatch/Tests/Olivero/oliveroColorTest.js

[Olivero/Olivero Color Test] Test Suite
──────────────────────────────────────────────────────────────────────────────
ℹ Connected to chrome on port 9515 (142ms).
  Using: chrome (106.0.5249.103) on LINUX.

Tips

Uncheck "Force break at first line when no path mapping specified" if your debugger is stopping on "Standard input code".

Image
PHPStorm xdebug don't break on no path mapping

https://agile.coop/blog/drupal-phpunit-tests-lando/ 
https://mglaman.dev/blog/setup-drupal-commerce-ci-and-behat-testing 
https://mglaman.dev/blog/running-drupals-phpunit-test-suites-ddev

Chromedriver 
https://gist.github.com/mikeshiyan/a91b6aadcf76734dfad08dd5811119de 
https://gist.github.com/segovia94/271dee02a907e57e0c3559e83dbb7783

https://github.com/drupal/core/tree/10.1.x/tests

https://www.npmjs.com/package/selenium-standalone

https://hub.docker.com/r/drupalci/webdriver-chromedriver/tags

https://mglaman.dev/blog/run-drupaltestsbrowsertestbase-sqlite

https://drupalcommerce.org/blog/45322/commerce-2x-unit-kernel-and-functional-tests-oh-my

https://www.drupal.org/docs/automated-testing/phpunit-in-drupal/running-phpunit-javascript-tests#s-other-ways-to-install-and-run-chromedriver 
https://git.drupalcode.org/project/drupalci_environments/-/blob/product…;
https://www.drupal.org/project/drupalci_environments/issues/3191472

PHPStorm issues 
https://blog.jetbrains.com/phpstorm/2016/11/docker-remote-interpreters/ 
https://youtrack.jetbrains.com/issue/WI-37986 
https://youtrack.jetbrains.com/issue/WI-37986/docker-compose-should-support-exec-and-re-use-existing-containers 
https://youtrack.jetbrains.com/issue/WI-34591/Add-Run-options-field-to-container-properties-for-Docker-interpreter 
https://youtrack.jetbrains.com/issue/WI-55125/Support-docker-exec-for-Docker-PHP-interpreters 
https://mglaman.dev/blog/test-driven-development-phpstorm-auto-testing-enabled

DDev 
https://susi.dev/fully-integrate-ddev-and-phpstorm-including-unit-tests-with-coverage-update-2021/ 
https://susi.dev/ddev-phpstorm-integration/

https://drupalize.me/blog/debug-any-drupals-phpunit-tests-phpstorm-ddev-local-environment

https://mglaman.dev/blog/running-drupals-functionaljavascript-tests-ddev

Docker 
https://www.pascallandau.com/blog/phpstorm-docker-xdebug-3-php-8-1-in-2022/ 
https://www.pascallandau.com/blog/docker-from-scratch-for-php-applications-in-2022

https://dev.to/rukykf/setting-up-phpstorm-to-work-seamlessly-with-docker-compose-xdebug-and-phpunit-for-local-development-dob

https://aschmelyun.com/blog/fixing-permissions-issues-with-docker-compose-and-php/

https://vsupalov.com/docker-shared-permissions/

Tags