在Docker Terminal中运行容器中的PHPUnit

作者:James Zhu (fatindeed@hotmail.com)

创建日期:2018-03-01

概述

Docker PHP运行环境的初衷是本地不再需要安装任何PHP运行相关的软件,然而因为代码是保存在本地的,要运行PHPUnit的话,要进入容器,再执行相关命令,有些不便。本文旨在解决PHPUnit运行的便利性问题,即直接在Docker Terminal中运行PHPUnit。

效果

James@HB038 MINGW64 ~/docker-php-dev/www/testdemo (master)
$ ls
BankAccount.php  BankAccountTest.php  bootstrap.php  phpunit.xml

James@HB038 MINGW64 ~/docker-php-dev/www/testdemo (master)
$ phpunit --testdox BankAccountTest
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.

BankAccount
  Balance is initially zero
  Balance cannot become negative
  Balance cannot become negative

Time: 131 ms, Memory: 10.00MB

OK (3 tests, 3 assertions)

正文

首先,利用docker inspect获取当前本地与容器之间的目录映射。

例如:

James@HB038 MINGW64 ~/docker-php-dev (master)
$ docker inspect -f "{{json .Mounts}}" dockerphpdev_php_1
[{"Type":"bind","Source":"/c/Users/James/docker-php-dev/local/my.ini","Destination":"/usr/local/etc/php/conf.d/my.ini","Mode":"rw","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/c/Users/James/docker-php-dev/www","Destination":"/data1/www","Mode":"rw","RW":true,"Propagation":"rprivate"}]

此处使用json参数控制输出的格式为JSON格式。再安装jq,进行JSON解析。

其次,docker exec命令中有一个-w可以设置程序的工作目录,通过此参数即可实现在当前目录下运行PHPUnit。

最后,修改start.sh(Docker Toolbox解决方案),加入此方法。

phpunit () {
  jq --version 1>/dev/null 2>&1
  if [ 0 -ne "$?" ]; then
    echo "jq required"
    echo "You can get it from https://stedolan.github.io/jq/"
    exit
  fi
  PHP_CONTAINER_NAME=$(docker ps --filter "label=com.docker.compose.service=php" --format "{{.Names}}")
  CWD=$PWD
  MOUNTS=$(docker inspect -f "{{json .Mounts}}" "$PHP_CONTAINER_NAME")
  for MOUNT in $(echo $MOUNTS | jq -c '.[]'); do
    SOURCE=$(echo $MOUNT | jq -r '.Source')
    SOURCE=${SOURCE//\//\\\/}
    DESTINATION=$(echo $MOUNT | jq -r '.Destination')
    CWD=${CWD/#$SOURCE/$DESTINATION}
  done
  docker exec -i -w "$CWD" "$PHP_CONTAINER_NAME" phpunit "$@"
}
export -f phpunit

Last updated