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

PHPStorm remote docker-compose based PHP interpreter configuration for integration testing #21

Closed
Vinai opened this issue Oct 17, 2019 · 15 comments

Comments

@Vinai
Copy link
Contributor

Vinai commented Oct 17, 2019

In order to develop Magento extensions and core patches efficiently I want to be able to run integration tests in PHPStorm.

PHPStorm allows configuration of remote, docker-compose based, PHP interpreters.
Here is what I've done so far:

  • Service: php-debug
  • docker-compose files:
    • /usr/local/Cellar/warden/0.1.10/environments/magento2.base.yml
    • /usr/local/Cellar/warden/0.1.10/environments/magento2.darwin.yml
  • environment variables from .env:
    • WARDEN_ENV_NAME
    • WARDEN_ENV_TYPE
    • TRAEFIK_DOMAIN
    • TRAEFIK_SUBDOMAIN
    • WARDEN_WEB_ROOT (this one isn't from .env but I thought it might help)

With this configuration PHPStorm allows saving the interpreter even though it says it's unable to parse the validation script output.

Nevertheless, PHPStorm allows me to continue, even though PHPStorm can not determine the PHP version.

Then I add a path mapping to the interpreter configuration from <Project root> -> /var/www/html manually, however, PHPStorm also read an invalid mapping from the yaml:
/usr/local/Cellar/warden/0.1.10/environments/.${WARDEN_WEB_ROOT:-}/pub/media -> /var/www/html/pub/media
It seems PHPStorm doesn't like the config in magento2.darwin.yaml:
.${WARDEN_WEB_ROOT:-}/pub/media:/var/www/html/pub/media:delegated

Next I changed .${WARDEN_WEB_ROOT:-} to ${WARDEN_WEB_ROOT:-} (removed the leading .) and it indeed allowed PHPStorm to find the PHP interpreter correctly.
I'm not sure why the . is here, I'll test shortly if warden seems to work without it, too.

When I then try to set up a PHPUnit configuration with this remote interpreter, I get the message:

Can not parse PHPUnit version output: Could not open input file: vendor/phpunit/phpunit/phpunit`

I tried setting the composer autoload.php file path to

  • vendor/autoload.php
  • /var/www/html/vendor/autoload.php
  • /Volumes/CaseSensitive/Workspace/php-sites/m2-training/vendor/autoload.pnp (my local path because of the path mapping config)

No luck. This might be an issue with PHPStorm, I don't know, but it would be wonderful to get integration tests working in the IDE.

@Vinai
Copy link
Contributor Author

Vinai commented Oct 17, 2019

I did some testing what the effect of removing the leading . is.
In a nutshell, without the leading . the environment variable WARDEN_WEB_ROOT is required in .env, otherwise the mount /pub/media is considered an absolute path which of course is wrong.

A hardcoded complete path in turn means the project can't be moved to another location on the disk, but setting the environment variable to . has the desired effect.

@davidalger would you consider a PR to the init-env command so it adds WARDEN_WEB_ROOT=. to the initial .env file?
For backward compatibility the script that reads the file should probably set that value, too, if it's missing from .env at read time.

@Vinai
Copy link
Contributor Author

Vinai commented Oct 17, 2019

Interesting: the "official" Magento devbox tells users to configure setting up the PHP interpreter as a regular remote php interpreter over ssh.
Maybe it would be possible to expose ssh on the warden php services, and then it could work with a much simpler configuration?

Reference:
https://github.com/magento/magento2-kubernetes-devbox/blob/master/docs/running-tests/running-tests.md

@dani97
Copy link

dani97 commented Jul 1, 2020

@Vinai I used docker image to setup interpreter and it worked for me perfectly.

@davidalger
Copy link
Collaborator

@dani97 Care to share details of how you set it up?

@dani97
Copy link

dani97 commented Jul 3, 2020

first configure docker service https://www.jetbrains.com/help/phpstorm/docker.html
Then configure a new interpreter, with the docker server configured earlier and select the warden container for running integration tests, I used debug container so that I can debug with breakpoints
Screenshot from 2020-07-03 12-58-16

@Vinai
Copy link
Contributor Author

Vinai commented Jul 3, 2020

Thanks for the info, it's been so long and a lot of work has been done on Warden, I'll try again soon.

@Vinai
Copy link
Contributor Author

Vinai commented Jul 6, 2020

Tried to mirror your config without success.

Bildschirmfoto 2020-07-06 um 18 12 33

Bildschirmfoto 2020-07-06 um 18 14 21

Bildschirmfoto 2020-07-06 um 18 15 12

Bildschirmfoto 2020-07-06 um 18 16 12

Seems like PHPStorm is running the correct docker image, but isn't mounting the required volumes.

@Elamurugan-Nallathambi
Copy link

@Vinai @dani97 I myself have same issue.
Every-time running test execution from container (warden shell) after writing test updates from PHPStorm.

PHPStorm: 2020.2 EAP or with previous version
OS: macOS Catalina 10.15.5 (19F101)

@dani97
Copy link

dani97 commented Jul 7, 2020

@Vinai @Elamurugan-Nallathambi, I noticed that in my setup that there is no folder like /opt/project inside the container and project is under /var/www/html but it still works for me. So I did some research and found this https://blog.alejandrocelaya.com/2017/02/01/run-phpunit-tests-inside-docker-container-from-phpstorm/
Phpstorm copies the project to /opt/project of a new container and runs the unit test. So change the path mapping to /opt/project and it will work for you.

Testing started at 11:49 AM ...

[docker://wardenenv/php-fpm:7.4-magento2-debug/]:php /opt/project/vendor/phpunit/phpunit/phpunit --configuration /opt/project/dev/tests/unit/phpunit.xml --filter Magento\\Cms\\Test\\Unit\\Block\\BlockTest --test-suffix BlockTest.php /opt/project/app/code/Magento/Cms/Test/Unit/Block --teamcity
PHPUnit 9.1.5 by Sebastian Bergmann and contributors.



Time: 00:00.200, Memory: 14.00 MB

@dani97
Copy link

dani97 commented Jul 7, 2020

Adding screenshots of interpreter and phpunit config for your reference
Screenshot from 2020-07-07 12-09-41
Screenshot from 2020-07-07 12-11-02

@Vinai
Copy link
Contributor Author

Vinai commented Jul 7, 2020

I fixed the `/opt/project mapping by removing it from the "Docker Container" settings beneath the "Path Mappings".

Bildschirmfoto 2020-07-07 um 10 51 07

When I add the "Project Root => /var/www/html" path mapping there, the remote interpreter can be executed by PHPStorm.

This is good enough for unit tests, but since PHPStorm executes the docker container for each PHP execution, no other containers are linked, and so there is no access to MySQL or Redis and the integration tests can't be run.

I think the PHP interpreter will have to be configured using "Docker Compose", but I haven't been able to get that working yet since the docker compse configuration is rather complex with many files having to be included in the right order with the right environment variables being set.

@Vinai
Copy link
Contributor Author

Vinai commented Nov 12, 2020

Problem solved thanks to @lbajsarowicz

The merged docker-compose config for a project can be dumped with

$ warden env config > docker-compose.yml

Then this configuration can be selected in the PHPStorm settings for a docker-compose remote interpreter.
Select the exec option instead of create new container for the fastest startup. Anyhow, this way integration tests work fine in PHPStorm.

EDIT: I did have to add one path mapping for the PHP interpreter manually: from my local project directory to /var/www/html in the container.

@Vinai Vinai closed this as completed Nov 12, 2020
@lfolco
Copy link

lfolco commented Feb 13, 2021

Any chance how to set this up could be added to the official docs? I'm having a heck of a time getting this setup.

@naseeraslam
Copy link

naseeraslam commented Dec 22, 2023

How can I make the remote-interpreter the default? I followed the steps above, but the default PHP local interpreter is still executing. Could someone please explain how to set the remote-interpreter as the default in PhpStorm?

@naseeraslam
Copy link

naseeraslam commented Dec 22, 2023

Remote
When I click on 'PHP CS Fixer - Whole File' in suggestions, it executes the default System PHP interpreter instead of my remote interpreter.Although setting files are coorectly validated.

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

No branches or pull requests

6 participants