lando / php

The Official Lando PHP plugin.
https://docs.lando.dev/php
GNU General Public License v3.0
16 stars 16 forks source link

Lando + PHPStorm + PHPUnit #6

Open Tigatok opened 4 years ago

Tigatok commented 4 years ago

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 commented 4 years ago

I found this in the Lando slack search:

https://imgur.com/a/XcpZAs7

hailwood commented 4 years ago

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 commented 4 years ago

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

joshmiller83 commented 4 years ago

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

Step 1: Configure PHPStorm Docker

image

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.

Step 3: Configure PHPStorm PHPUnit

Finally, we need to tell PHPStorm about PHPUnit and where to find it's executable command 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.

image

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:

quentint commented 4 years ago

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 commented 4 years ago

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[bot] commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

Hello,

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

quentint commented 2 years ago

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

sebastienserre commented 2 years ago

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

quentint commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

Thank you @sheanhoxie, it works.

mortona42 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.