To configure PhpStorm to run PHPUnit tests using Laravel Sail, follow these steps:
-
Open the Settings/Preferences dialog by pressing
Ctrl+Alt+Sor by selectingFile | Settingsfor Windows and Linux orPhpStorm | Preferencesfor macOS. -
In the left-hand pane, select
Languages & Frameworks | PHP. -
In the right-hand pane, click the
...button next to theCLI Interpreterfield. -
In the
CLI Interpretersdialog, click the+button to add a new interpreter. -
Select
From Docker, Vagrant, VM, Remote...from the list of options and clickNext. -
In the
Dockerdialog, selectDocker Compose. -
In the
Docker Composedialog, select thedocker-compose.ymlfile for your Laravel Sail project. -
In the
Servicefield, enter the name of the service that contains PHP, which is usuallyapp. -
Click
OKto save the interpreter settings. -
In the left-hand pane, select
Languages & Frameworks | PHP | Test Frameworks. -
In the right-hand pane, click the
+button to add a new test framework. -
Select
PHPUnitfrom the list of options and clickNext. -
In the
PHPUnitdialog, select theUse Composer autoloaderoption. -
In the
PHPUnit Libraryfield, enter the path to thephpunitexecutable in your Laravel Sail project, which is usually/var/www/vendor/bin/phpunit. -
Click
OKto save the test framework settings. -
Open the
Runmenu and selectEdit Configurations. -
Click the
+button to add a new configuration and selectPHPUnit. -
In the
PHPUnitdialog, select the interpreter you just created from theInterpreterdropdown. -
In the
Test Runnerfield, selectPHPUnit via Composer. -
In the
Test Scopefield, selectDefined in the configuration file. -
In the
Configuration filefield, enter the path to yourphpunit.xmlfile in your Laravel Sail project, which is usually/var/www/phpunit.xml. -
Click
OKto save the configuration. -
You can now run your PHPUnit tests using Laravel Sail by selecting the configuration you just created from the
Runmenu.
Example configuration file:
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.5/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
<ini name="memory_limit" value="-1"/>
<server name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="test"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value=""/>
</php>
</phpunit>