最近使用php开发composer包时,想编写单元测试测试具体方法有没有问题。但是我的开发环境都是基于laradocker搭建的,网上搜了快很多关于docker+phpunit+phpstorm的文章,但是都讲的….。

以下项目是基于laradocker搭建的,已经支持xdebug和swoole等扩展

1. 项目目录介绍

# 本地目录
/Users/hui/Project/PHP/php-tools

# docker镜像中的目录
/var/www/php-tools

2. 安装phpunit

因为我是基于laradocker 搭建的PHP开发环境,laradocker是通过docker-compose 启动服务,如我的服务列表

➜  docker_php_env git:(master) docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------------------
env_docker-in-docker_1 dockerd-entrypoint.sh Up 2375/tcp, 2376/tcp
env_nginx_1 /docker-entrypoint.sh /bin Up 0.0.0.0:443->443/tcp,
... 0.0.0.0:80->80/tcp,
0.0.0.0:9900->9900/tcp
env_php-fpm_1 docker-php-entrypoint php- Up 9000/tcp
fpm
env_redis_1 docker-entrypoint.sh redis Up 0.0.0.0:6379->6379/tcp
...
env_workspace_1 /sbin/my_init Up 0.0.0.0:2222->22/tcp

2.1 进入workspace安装

为什么要在workspace中安装? 因为我本地没有安装composer,也没有php环境,composer安装过程中经常会检测PHP的某些扩展是否已经安装,而workspace服务中已经安装composer和PHP

# 进入workspace
➜ docker_php_env git:(master) docker-compose exec workspace bash
# 安装
root@3eda27ea2482:/var/www/php-tools# composer require --dev phpunit/phpunit
Using version ^8.5 for phpunit/phpunit
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 29 installs, 0 updates, 0 removals
- Installing sebastian/version (2.0.1): Loading from cache
- Installing sebastian/type (1.1.3): Loading from cache
- Installing sebastian/resource-operations (2.0.1): Loading from cache
- Installing sebastian/recursion-context (3.0.0): Loading from cache
- Installing sebastian/object-reflector (1.1.1): Loading from cache
- Installing sebastian/object-enumerator (3.0.3): Loading from cache
- Installing sebastian/global-state (3.0.0): Loading from cache
- Installing sebastian/exporter (3.1.2): Loading from cache
- Installing sebastian/environment (4.2.3): Loading from cache
- Installing sebastian/diff (3.0.2): Loading from cache
- Installing sebastian/comparator (3.0.2): Loading from cache
- Installing phpunit/php-timer (2.1.2): Loading from cache
- Installing phpunit/php-text-template (1.2.1): Loading from cache
- Installing phpunit/php-file-iterator (2.0.2): Loading from cache
- Installing theseer/tokenizer (1.2.0): Loading from cache
- Installing sebastian/code-unit-reverse-lookup (1.0.1): Loading from cache
- Installing phpunit/php-token-stream (3.1.1): Loading from cache
- Installing phpunit/php-code-coverage (7.0.10): Loading from cache
- Installing doctrine/instantiator (1.3.1): Loading from cache
- Installing phpdocumentor/reflection-common (2.2.0): Loading from cache
- Installing symfony/polyfill-ctype (v1.18.1): Loading from cache
- Installing webmozart/assert (1.9.1): Loading from cache
- Installing phpdocumentor/type-resolver (1.3.0): Loading from cache
- Installing phpdocumentor/reflection-docblock (5.2.1): Loading from cache
- Installing phpspec/prophecy (1.11.1): Loading from cache
- Installing phar-io/version (2.0.1): Loading from cache
- Installing phar-io/manifest (1.0.3): Loading from cache
- Installing myclabs/deep-copy (1.10.1): Loading from cache
- Installing phpunit/phpunit (8.5.8): Loading from cache
sebastian/global-state suggests installing ext-uopz (*)
phpunit/php-code-coverage suggests installing ext-xdebug (^2.7.2)
phpunit/phpunit suggests installing phpunit/php-invoker (^2.0.0)
phpunit/phpunit suggests installing ext-soap (*)
phpunit/phpunit suggests installing ext-xdebug (*)
Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating autoload files

3. 配置phpstorm

3.1 步骤一: 配置CLI Interpreter

image-20200821163235329

image-20200821164107208

image-20200821164145503

image-20200821164314695

点击OK(确认)后

image-20200821164717043

3.2 步骤二: 配置目录映射和网络

image-20200821164818609

image-20200821165249751

注意点:

我在docker-compose.yaml 设置php-fpm走的网络是backend, 配置如下:

...
### PHP-FPM ##############################################
php-fpm:
build:
context: .
dockerfile: ./php-fpm/Dockerfile
args:
- PHP_VERSION=${PHP_VERSION}
volumes:
- ./php-fpm/php${PHP_VERSION}.ini:/usr/local/etc/php/php.ini
- ${APP_CODE_PATH_HOST}:${APP_CODE_PATH_CONTAINER}${APP_CODE_CONTAINER_FLAG}
expose:
- "9000"
extra_hosts:
- "dockerhost:${DOCKER_HOST_IP}"
environment:
- PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
- DOCKER_HOST=tcp://docker-in-docker:2375
- FAKETIME=${PHP_FPM_FAKETIME}
depends_on:
- workspace
networks:
- backend # 这里设置的是网络
links:
- docker-in-docker
...

**但是!但是! ! 在docker网络中找不到 !! ** 如下列出我本地的docker网络都有哪些:

➜  docker_php_env git:(master) docker network ls
NETWORK ID NAME DRIVER SCOPE
ad463cc3f333 bridge bridge local
b3581c822831 env_backend bridge local
62b15b6d0f61 env_frontend bridge local
ca0e05f7c6c1 host host local
b0bcba3683fe none null local

通过上面可以看出,本地docker网络中没有backend网卡,但是却有有个env_backend,原因是因为docker-compose 在启动服务时会在前面拼上当前目录名(如果没有设置前缀时),创建网络也是一样! 

image-20200821170126288

3.3 配置Test Frameworks

image-20200821170357467

image-20200821170455293

选完以后弹窗,选择之前配置的 CLI Interpreter
image-20200821170517414

确认后进入配置页面:

image-20200821170752139

4. 运行单元测试

4.1 创建单元测试文件Test.php

​ 创建单元测试文件Test.php,目录结构如下:

image-20200821171135728

4.2 运行

右键选择Debug方式运行
image-20200821171201925

运行效果
image-20200821171224316

5.PHP开发环境分享

基于laradocker二次封装的PHP开发环境,查看源码