简体   繁体   English

使用PhpStorm在Docker容器中运行PHPUnit

[英]Running PHPUnit within a Docker container with PhpStorm

I want to configure my PhpStorm IDE to run PHPUnit tests within my Docker container. 我想配置我的PhpStorm IDE以在我的Docker容器中运行PHPUnit测试。

It seems like I'm restricted to either using a local PHP executable, or one through SSH, as the interpreter for the tests. 似乎我只能使用本地PHP可执行文件,或者通过SSH作为测试的解释器。

I could install an SSH service on my PHP container, but it seems like a bit of a hacky solution, and articles online discourage installing an SSH service on containers. 我可以在我的PHP容器上安装SSH服务,但它看起来像是一个hacky解决方案,并且在线文章不鼓励在容器上安装SSH服务。

To try and get a local interpreter working, I tried creating a bash script that would proxy calls to PHP within the container, like this: 为了尝试让本地解释器工作,我尝试创建一个bash脚本来代理容器内对PHP的调用,如下所示:

#!/usr/bin/env bash

# Run PHP through Docker
docker exec -t mycontainer_php_1 php "$@"

This works perfectly when I run it myself, but when I point PhpStorm to it as a local PHP interpreter, it doesn't recognize it as a valid PHP executable. 当我自己运行它时,这非常有效,但当我将PhpStorm指向它作为本地PHP解释器时,它不会将其识别为有效的PHP可执行文件。

So what's a good way to get this working? 那么什么是让这个工作的好方法?

With PhpStorm now having better integration with Docker engine (including Docker for Mac), you can now just do the following (or read this article ): 由于PhpStorm现在可以更好地与Docker引擎 (包括Docker for Mac) 集成 ,您现在可以执行以下操作(或阅读本文 ):

Command line: 命令行:

  1. Pull the phpunit Docker image: docker pull phpunit/phpunit phpunit Docker镜像: docker pull phpunit/phpunit
  2. (Mac and maybe Windows) Bridge the Docker socket to the API_URL : socat -d TCP-LISTEN:2376,range=127.0.0.1/32,reuseaddr,fork UNIX:/var/run/docker.sock (Mac和Windows) 将Docker套接字桥接到API_URL socat -d TCP-LISTEN:2376,range=127.0.0.1/32,reuseaddr,fork UNIX:/var/run/docker.sock

Then Phpstorm: 那么Phpstorm:

  1. Configure connection to Docker engine: 配置与Docker引擎的连接:
    • Go to Settings -> Build, Execution, Deployment -> Docker 转到设置 - >构建,执行,部署 - > Docker
    • Create a new Docker configuration with API URL set to: 创建一个新的Docker配置,其API URL设置为:
      • Linux: unix:///var/run/docker.sock Linux: unix:///var/run/docker.sock
      • Windows and Mac: http://127.0.0.1:2376 or tcp://localhost:2376 Windows和Mac: http://127.0.0.1:2376http://127.0.0.1:2376 tcp://localhost:2376tcp://localhost:2376
  2. Configure the remote interpreter: 配置远程解释器:
    • Go to Settings -> Languages & Frameworks -> PHP 转到设置 - >语言和框架 - > PHP
    • Create a new PHP CLI interpreter by: 通过以下方式创建新的PHP CLI解释器:
      • Clicking on ... then + and 'Remote...' 单击...然后+和'远程...'
      • Select Docker with: 选择Docker
        • ServerImage: ServerImage:
        • Image name: phpunit/phpunit:latest 图片名称: phpunit/phpunit:latest
        • PHP executable: php PHP可执行文件: php
  3. Configure Phpunit: 配置Phpunit:
    • Go to Settings -> Languages & Frameworks -> PHP -> PHPUnit 转到设置 - >语言和框架 - > PHP - > PHPUnit
    • Create new Phpunit configuration ( + then 'By Remote interpreter...' and select 创建新的Phpunit配置( +然后'通过远程解释器...'并选择
    • Set Use Composer Autoloader 设置Use Composer Autoloader
    • Path to script: /opt/project/vendor/autoload.php 脚本路径: /opt/project/vendor/autoload.php
    • Default configuration file: /opt/project/phpunit.xml.dist 默认配置文件: /opt/project/phpunit.xml.dist
  4. Try to run your tests! 尝试运行测试!

This works: 这有效:

DIR=$(dirname $(readlink -f "$0"))
docker run --rm --sig-proxy=true -v ${DIR}:${DIR} -w ${DIR} --pid=host php:cli php "$@"

just put it in some file, chmod +x it => just tested with idea and is recognized just fine :) 只是把它放在一些文件中,chmod + x it =>刚刚用想法进行测试并且被认可很好:)

The trick to get all of phpstorms helper scripts to work really is to mount the directory the php executable resides in. In case this script is not in the folder of your phpunit executable and source code you will have to extend it to also mount those. 让所有phpstorms帮助程序脚本正常工作的诀窍是安装php可执行文件所在的目录。如果这个脚本不在你的phpunit可执行文件和源代码的文件夹中,你必须扩展它以安装它们。

(important side note here: they must be mounted to the same folder in container and host obv :) ) (这里重要的注意事项:它们必须安装在容器和主机obv中的同一文件夹:))

Edit after a few months now, but maybe helpful to some: Worked this out with a WordPress example here: http://original-brownbear.github.io/2015/12/23/phpunit-docker-phpstorm.html 几个月后编辑,但可能对某些人有帮助:在这里用WordPress示例解决这个问题: http//original-brownbear.github.io/2015/12/23/phpunit-docker-phpstorm.html

I know you say you heard ssh in docker containers is bad, but I think since PhpStorm doesn't support docker's exec to remotely run scripts it's not a bad option. 我知道你说你在docker容器中听到了ssh是坏事,但我认为既然PhpStorm不支持​​docker的exec来远程运行脚本,那么这不是一个糟糕的选择。 You can set up a docker container as a remote php interpreter using SSH inside PhpStorm, then just set up a remote PHPUnit configuration. 您可以在PhpStorm中使用SSH将docker容器设置为远程php解释器,然后只需设置远程PHPUnit配置。

You can see step-by-step guide here: 您可以在此处查看分步指南:
https://mickadoo.github.io/php,phpunit,docker/2016/10/12/phpunit-docker.html https://mickadoo.github.io/php,phpunit,docker/2016/10/12/phpunit-docker.html

Update: you don't need to use SSH anymore. 更新:您不再需要使用SSH。 PhpStorm now supports docker, you can run PHP unit tests just by using a PHP docker image with PHPUnit installed as a remote interpreter. PhpStorm现在支持docker,只需使用安装了PHPUnit作为远程解释器的PHP docker镜像,就可以运行PHP单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM