Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lando + PHPStorm + PHPUnit #6

Open
Tigatok opened this issue May 8, 2020 · 21 comments
Open

Lando + PHPStorm + PHPUnit #6

Tigatok opened this issue May 8, 2020 · 21 comments

Comments

@Tigatok
Copy link

Tigatok commented May 8, 2020

Hi there,

Has anyone successfully set up Lando + PHPStorm + PHPUnit to run all together, such that you can run the tests right from the IDE (Not the terminal/running as lando phpunit)? If so, can you post documentation on getting them set up? I've tried a few times and every time I believe it boils down to PHPStorm running some sort of containers of their own. But it IS possible through docker-compose (non lando setup), so am wondering if there are any tips/tricks to maybe getting it to work with Lando.

Much appreciated!

@joshmiller83
Copy link

I found this in the Lando slack search:

https://imgur.com/a/XcpZAs7

@hailwood
Copy link

Yeah I've tried this several times, from my research I don't believe it's possible until PHPstorm adds the ability to use an already running container as the test runner.

Phpstorm can also connect over SSH, but I haven't been able to work out how to get it to connect into the lando appserver. If we can get that working, it will probably be the easiest path forward.

@joshmiller83
Copy link

I have a working example. Writing up a Lando guide.

@joshmiller83
Copy link

Guide write up delayed because of the effort involved. Here's the step by step that works for me on a Mac but doesn't work on Ubuntu 20.04.

Before you begin

  • start Lando lando start
  • Make sure you're working with a composer-based PHP project that has PHPUnit in its dependencies.
  • Run composer lando composer install
  • Find your Lando instance Network ID docker network ls
    • The results of that scan could be large. Look for your Lando environment's network (there should be only one per project) then copy the "Network ID" somewhere convenient.

Step 1: Configure PHPStorm Docker

  • Go to your settings pane.
  • Locate your Docker settings under the Build, Execution, Deployment menu. If you can't find it search for Docker.

image

  • Select "Build, Execution, Deploy"
  • Select "Docker"
  • Click "+"
  • Select "Docker for Mac"
  • Confirm you see "Connection successful"

Step 2: Configure PHPStorm PHP CLI

Next, we need to tell PHPStorm about Lando's PHP Image. This is where a lot of the settings are used from the "Before you begin" section.

  • Locate your PHP settings under the Languages & Frameworks section. Choose your CLI Interpreter.
    image
  1. Select "Languages & Frameworks"; 2. Select "PHP"; 3. Change your PHP version if needed; 4. Click the "..." to the right of "CLI Interpreter"
  • Locate your PHP settings under the Languages & Frameworks section. Click the "Folder" icon next to the "Docker Container" section.
  • Change the Container "Network Mode" to the Network ID from the "Before you begin" section.

  • Change the Container Path to "/app"

Step 3: Configure PHPStorm PHPUnit

Finally, we need to tell PHPStorm about PHPUnit and where to find it's executable command file.

  • Locate your Testing settings under the PHP section in the Languages & Frameworks section.

  • Create a "PHPUnit by Remote Interpreter" configuration.

  • Locate your project level phpunit.phar file.

Step 4: Create a PHPUnit Configuration

Now that you have your PHPStorm Project Settings all correctly configured (steps 1-3), the next step is to create a "Run/Debug Configuration" that will give you a push-button ability to run PHPUnit tests using Lando's Docker PHP Docker. Note: This is not the last step, so this will not likely work if you click play once you're done.

  • Locate your "Run/Debug Configuration" screen

image

  • Create a "All custom tests" PHPUnit Run configuration.
  1. Name your Test Run Configuration. Perhaps "All custom tests"; 2. Set the scope to a directory. Of course, you can change this later or create new configurations as needed; 3. If you choose directory, it must be your local and not your lando image directory; 4. Confirm your interpreter looks right. If there are problems, PHPStorm is really good at telling you what is missing.

Step 5: PHPUnit.xml Settings

The recommended approach is to keep a PHPStorm-version of phpunit.xml file called "phpunit.phpstorm.xml" at the root of your repository so you can set specific options up just for PHPStorm. Below there is a complete copy of the xml file, but here for good measure are the items that you will want to consider:

<php>
    <env name="SIMPLETEST_BASE_URL" value="http://appserver_nginx"/>
    <env name="SIMPLETEST_DB" value="mysql://drupal8:drupal8@database/drupal8#phpunit"/>
</php>
<testsuites>
    <testsuite name="unit">
      <file>./web/core/tests/TestSuites/UnitTestSuite.php</file>
    </testsuite>
</testsuites>
<filter>
  <whitelist>
      <directory>./web/modules</directory>
  </whitelist>
</filter>

Specifically, do the following:

  • Change the SIMPLETEST_BASE_URL to the value of the internal host returned when you run lando info for your primary appserver. In this case, NGINX in Lando reports "http://appserver_nginx/".
  • Set your <testsuite> inclusion files to look at ./web/core/...
  • Set your code coverage <whitelist> directories to ./web/...

@quentint
Copy link

quentint commented Jun 9, 2020

I've used your guide on Windows, here are the differences:

  1. Allow TCP socket (on Docker settings) and use it

image

image

  1. PHP path mappings and Docker container are a bit different (/opt/project/)

image

  1. Same for path to phpunit.phar

image

  1. Specify alternative configuration file (not required, your phpunit.phpstorm.xml idea looks great)

image

I hope this helps!
At least I'm sure this will help me in the future 😉

@sheanhoxie
Copy link

Thanks for the guide! I got my tests up and running, but I ran into issues on step 3

Create a "PHPUnit by Remote Interpreter" configuration.

... where if I touched the little refresh icon next to Path to phpunit.phar I would get a popup from PhpStorm saying Updating PhpUnit Version... and I was unable to close it, or cancel, force quitting PhpStorm was the only option.

Well, after force quitting, and double checking all of my settings compared to yours, I was able to get my tests running. However, even when all the settings are good, touching that refresh icon brings the dreaded unclosable Updating PhpUnit Version.. popup

@stale
Copy link

stale bot commented Apr 27, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions and please check out this if you are wondering why we auto close issues.

@quentint
Copy link

quentint commented Oct 5, 2021

Here's an updated version of this issue's steps, relying on Docker Compose and without relying on docker network ls: https://gist.github.com/quentint/6331aa9a75313ed955b2ea20d33557af

I hope this helps!
Not sure if there's a place for this, but I guess this could be linked to from (or integrated to) the Lando docs. Thoughts?

@sebastienserre
Copy link

Hello,

Is Someone kowns why it says "XDebug" extension isn't installed ?
KIbpnv2

@quentint
Copy link

Did you enable XDebug on your Lando project?
See this doc section: https://docs.lando.dev/config/php.html#using-xdebug

@sebastienserre
Copy link

Thanks @quentint , I have it setup like that
screenshot-2021-11-19-11-50
I'm on Ubuntu 20.04

@quentint
Copy link

Looks OK to me. Did you rebuild?
To be honest, I never used XDebug in this configuration (I only used PHPUnit without XDebug), so I'm not sure I'm really helpful :(

@sheanhoxie
Copy link

sheanhoxie commented Nov 27, 2021

@sebastienserre You probably need to tell PHPStorm where Xdebug is located within the Lando environment. To find out where this is, from your terminal, SSH into the Lando app with the following command:

lando ssh

and then from within the Lando app environment, enter the command:

php -i | grep -i 'extension_dir'

This command should spit out something like this, I've highlighted in yellow the portion you want to copy:

Screen Shot 2021-11-21 at 9 26 14 PM

Back in PHPStorm Run/Debug configurations screen, choose the lando CLI interpreter from the dropdown, and then click the options button as shown here:

Screen Shot 2021-11-21 at 9 09 38 PM

Paste the extensions directory path you copied from the Lando environment, into the "Additional: Debugger Extension" field, and dont forget to add the xdebug extension "xdebug.so" as shown here:

Screen Shot 2021-11-21 at 9 23 41 PM

That should resolve the issue of "'Xdebug' extension is not installed"

@sebastienserre
Copy link

Thank you @sheanhoxie, it works.

@mortona42
Copy link

I followed these instructions for linux and one difference I encountered is using "Connect to docker daemon with" = Unix socket.
Seems to be working so far, thanks everyone!

@jonnyhocks
Copy link

@joshmiller83 - Thank you so much for you write up, I have followed it and I now have PHPUnit running within Lando.

My final hurdle, is that I seem to be unable to run browser tests. I have configured my phpunit.xml file with the internal hostname provided when running lando info but I receive the following error:

Testing /app/web/modules/contrib/webform/tests/src/Functional/Handler

GuzzleHttp\Exception\ConnectException : cURL error 6: Could not resolve host: appserver.site_name.internal (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

Any help would be greatly appreciated!

@pfrenssen
Copy link
Contributor

This stopped working for me on PHPStorm 2022-2. When I try to run a test the IDE hangs on test initialization. When I downgrade back to 2022-1.4 then it works again flawlessly.

@GuyPaddock
Copy link

I got this to work in IntelliJ but my teammates running PhpStorm couldn't get it to work. Eventually, it turned out that it worked by chance for me because I had the Python plugin for IntelliJ installed, and a side effect of the Python plugin is that it remaps Windows paths into WSL paths when launching commands. PhpStorm doesn't have a Python plugin so this isn't an acceptable workaround there.

More here:
https://youtrack.jetbrains.com/issue/WI-71929

@GuyPaddock
Copy link

Re: Xdebug not being detected, I did not have to tell the IDE where the extension was located for it to work. However, on one of my systems, it was not being detected no matter what I did; this turned out to be because one of the build steps was failing when Lando was bringing the containers up, and that seemed to prevent Lando from setting up Xdebug in the container. So, if you are not getting Xdebug to work, ensure that you do not see any errors from Lando when starting the containers.

@mortona42
Copy link

I want to be able to run Drupal tests within PHPStorm, but have not been able to configure it correctly. Unit and Kernel tests will run, but Functional tests are running into issues like file permissions or accessing urls.

I configured a CLI interpreter to use docker compose and included all configuration files in ~/.lando/compose/SITE, selecting appserver as the service.

When run is selected, it's giving file permission errors:

Exception : User warning: mkdir(): Permission Denied
Drupal\Component\PhpStorage\FileStorage->createDirectory()() (Line: 123)

When exec is selected:
the input device is not a TTY

If I create a CLI interpreter using the docker image, I get:
Behat\Mink\Exception\ElementNotFoundException : Button with id|name|label|value "Log in" not found.
This seems like it's not getting the right page at the configured url, http://appserver/

Is there another way to get PHPUnit working in PHPStorm?

@mortona42
Copy link

Set the docker compose command on the run configuration to exec -u www-data to fix the permission issue.
Set the Use compose v2 setting in Build, Execution > Docker > tools to fix the TTY issue.

Now functional tests are running in PHPStorm!

You have to re-add the exec command per test, but you can fix that by editing run/debug configurations, and clicking Edit configuration templates in the lower left corner. Edit the PHPUnit test template and put the docker compose exec command there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests