longestdrive's avatar

Laravel Sail - setting up phpstorm for phpunit tests

Hi I'm trying to configure Phpstorm to enable me to run my tests using phpstorm testing tools.

I've configured the command line interpreter to use laravel sail image sail-8.2/app:latest and confirmed xdebug is present.

I set up the interpreter to use docker compose from with phpstorm settings for the remote interpreter.

When I run tests I get a mysql connection error:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed

I tried setting up another interpreter to use simply docker and select the service - but that fails and doesnt recognise phpunit being installed

How can I correctly configure phpstorm to play nicely with docker/sail to run my phpunit tests using the phpstorm tools?

0 likes
7 replies
LaryAI's avatar
Level 58

To configure PhpStorm to run PHPUnit tests using Laravel Sail, follow these steps:

  1. Open the Settings/Preferences dialog by pressing Ctrl+Alt+S or by selecting File | Settings for Windows and Linux or PhpStorm | Preferences for macOS.

  2. In the left-hand pane, select Languages & Frameworks | PHP.

  3. In the right-hand pane, click the ... button next to the CLI Interpreter field.

  4. In the CLI Interpreters dialog, click the + button to add a new interpreter.

  5. Select From Docker, Vagrant, VM, Remote... from the list of options and click Next.

  6. In the Docker dialog, select Docker Compose.

  7. In the Docker Compose dialog, select the docker-compose.yml file for your Laravel Sail project.

  8. In the Service field, enter the name of the service that contains PHP, which is usually app.

  9. Click OK to save the interpreter settings.

  10. In the left-hand pane, select Languages & Frameworks | PHP | Test Frameworks.

  11. In the right-hand pane, click the + button to add a new test framework.

  12. Select PHPUnit from the list of options and click Next.

  13. In the PHPUnit dialog, select the Use Composer autoloader option.

  14. In the PHPUnit Library field, enter the path to the phpunit executable in your Laravel Sail project, which is usually /var/www/vendor/bin/phpunit.

  15. Click OK to save the test framework settings.

  16. Open the Run menu and select Edit Configurations.

  17. Click the + button to add a new configuration and select PHPUnit.

  18. In the PHPUnit dialog, select the interpreter you just created from the Interpreter dropdown.

  19. In the Test Runner field, select PHPUnit via Composer.

  20. In the Test Scope field, select Defined in the configuration file.

  21. In the Configuration file field, enter the path to your phpunit.xml file in your Laravel Sail project, which is usually /var/www/phpunit.xml.

  22. Click OK to save the configuration.

  23. You can now run your PHPUnit tests using Laravel Sail by selecting the configuration you just created from the Run menu.

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>
2 likes
MohamedTammam's avatar

You need to run the test from the container. To run the tests, just use

sail test
longestdrive's avatar

@MohamedTammam Hi - thank you - yes, I can run the tests from the command line using sail test, that works as expected. However I want to use PhpStorm's tools to run the tests from within the IDE not as CLI. It's configuring PHPStorm which is the issue :)

longestdrive's avatar

Hi - thanks for the share I have seen that one and tried that, although referring to pest not phpunit but principles are similar. Thank you

longestdrive's avatar

Update: I managed to get phpstorm to run tests using the principles in the AI answer. It's presenting me with new issues:

  1. I need to stop sail before I run the tests in PHPStorm otherwise it complains about ports already being in use - I assume this means it's trying to start a new container? I'm new to Docker so is this expected?
  2. Once the tests are done I'm then unable to restart using Sail Up. It complains ports are in use which means the PhpStorm container is still active.
Attaching to golf-app-laravel.test-1, golf-app-mailpit-1, golf-app-meilisearch-1, golf-app-mysql-1, golf-app-phpmyadmin-1, golf-app-redis-1, golf-app-selenium-1
Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:8080 -> 0.0.0.0:0: listen tcp 0.0.0.0:8080: bind: address already in use

Re-running the tests it does display the services as connected so assume it keeps the container running after the test.

In Docker Desktop I can only see my app container, I can't see anything else running to be able to stop

I've listed containers using Docker ps and can see Selenium service running - This appears as a result of the failed sail up. If I kill that and retry I get the same issue.

So -

  1. how can I terminate the tests correctly within PhpStorm to enable me to sail up?
  2. What is the correct workflow with Docker and tests within PHPStorm?

Please or to participate in this conversation.