用phing和phpunit在发布的时候做单元测试

首先说下本文用的两个工具:

1:https://www.phing.info/

    Phing是一个PHP项目构建系统或建立一个基于工具的Apache Ant.你可以做任何它,你可以做一个传统的构建系统,如GNU make工具,它的使用简单的XML构建文件和可扩展PHP的“任务”类使其易于使用和高度灵活的构建框架

2:http://www.phpunit.cn/

    PHPUnit是一个面向PHP程序员的测试框架,这是一个xUnit的体系结构的单元测试框架

现在开始正题,我们先构建自己的发布publish.xml,大致如下:

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="publish">

    <property name="NAME" value="test" />
    <property name="MODULE" value="test" />
    <property name="RSYNC" value="/usr/bin/rsync" />
    <php expression="date('Y-m-d-H-i-s')" returnProperty="TIME"/>
    <php expression="date('YmdHis')" returnProperty="VERSION" />
    <property name="HISTORY_DIR" value="/publish/publish_history/${NAME}/${TIME}"/>

    <!---从git拉取最新代码-->
    <target name="pull">
        <echo msg="Fulling latest code from repository, this might take a few minutes."/>
        <mkdir dir="${HISTORY_DIR}" mode="0754"/>
        <exec level="info" command="cd /publish/${NAME} &amp;&amp; git pull origin master" checkreturn="true" logoutput="true"/>
    </target>

    <!---生成要发布的代码快照-->
    <target name="rsync_local" depends="pull">
        <echo msg="Rsyncing files to publish_history folder" />
        <exec level="info" command="cd /publish/${NAME} &amp;&amp; ${RSYNC} -avzR --progress --exclude-from=${EXCLUDE_FILE} * ${HISTORY_DIR}" checkreturn="true" logoutput="false" />
        <exec level="info" command="cd ${HISTORY_DIR} &amp;&amp; echo ======${VER_DESC}====== >> ./version.txt" checkreturn="true" logoutput="false" />
        <exec level="info" command="cd /publish/${NAME} &amp;&amp; git log --stat -1 --no-merges >> ${HISTORY_DIR}/version.txt" checkreturn="true" logoutput="false"/>
    </target>

    <!---替换测试域名为正式域名-->
    <target name="replace" depends="rsync_local">
        <echo msg="Replacing special string apitest.xtgxiso.com to api.xtgxiso.com" />
        <exec level="info" command="grep -rl 'apitest.xtgxiso.com' ${HISTORY_DIR} | while read file ; do sed -i 's/apitest\.xtgxiso\.com/api.xtgxiso.com/g' $file; done" checkreturn="true" logoutput="true"/>
    </target>

    <!---替换常量为动态时间,防止CDN缓存-->
    <target name="update" depends="replace">
        <echo msg="Change css version to now timestamp" />
        <exec level="info" command="grep -rl '#VERSION#' ${HISTORY_DIR} | while read file ; do sed -i 's/#VERSION#/${VERSION}/g' $file; done" checkreturn="true" logoutput="true"/>
    </target>

    <!---删除git代码库系统文件-->
    <target name="clean" depends="update">
        <echo msg="Cleaning some no need files" />
        <exec level="info" command="cd ${NAME} &amp;&amp; find ./ -name '.gitignore' | xargs rm -f" checkreturn="true" logoutput="true"/>
    </target>

    <!---将代码发布到执行单元测试的线上机器-->
    <target name="rsync_remote" depends="clean">
        <echo msg="Rsyncing updated files to remote host" />
        <exec level="info" command="cd ${HISTORY_DIR} &amp;&amp; ${RSYNC} -avzR --progress --exclude-from '/publish/exclude.txt'  * root@192.168.1.128::test/" checkreturn="true" logoutput="false"/>
        <exec level="info" command="cd ${HISTORY_DIR} &amp;&amp; ${RSYNC} -v root@192.168.1.128::test/" checkreturn="true" logoutput="true"/>
    </target>

    <!---执行单元测试,如果通过,继续,否则停止发布-->
    <target name="phpunit" depends="rsync_remote">
        <echo msg="phpunit start" />
        <ssh failonerror="true" username="test" password="test123456" host="192.168.1.128" port="22" display="true" command="/shell/phpunit.php" />
    </target>

    <!---发布到线上-->
    <target name="rsync_product" depends="phpunit">
        <echo msg="Rsyncing updated files to rsync_product host" />
        <exec level="info" command="cd ${HISTORY_DIR} &amp;&amp; ${RSYNC} -avzR --progress --exclude-from '/publish/exclude.txt'  * root@192.168.1.129::test/" checkreturn="true" logoutput="false"/>
        <exec level="info" command="cd ${HISTORY_DIR} &amp;&amp; ${RSYNC} -v root@192.168.1.129::test/" checkreturn="true" logoutput="true"/>
    </target>

    <target name="publish" depends="rsync_product">
    </target>

</project>

2:在执行单元测试的主机上写要布时运行的脚本(/shell/phpunit.php),内容大致如下:

#!/usr/local/php7/bin/php
<?php
        $output = shell_exec('/shell/phpunit.sh');
        $pos = stripos($output,"Failures");
        if ($pos === false) {
                fwrite(STDOUT,$output."\n");
        }else{
                fwrite(STDOUT,'<font color="red">'.$output."</font>\n");
                fwrite(STDERR,$output."\n");

        }

3:phpunit.sh脚本内容如下:

#!/bin/bash
cd /www/test/tests/
/usr/local/php7/bin/php /usr/local/bin/phpunit --stop-on-failure --verbose --debug

4:发布脚本如下(new_publish.sh):

#!/bin/bash

PHING='/usr/bin/phing';

display_usage(){
        echo "Usage:" $0 "<xml file>  <version description>";
}
if [[ $1 =~ '^-(h|-help)$' ]] || [ $# -eq 0 ] || [ $# -lt 2 ];
then
        display_usage
        exit
fi

BUILDXML=$1
TARGET=$3

if [[ ! -x $PHING ]];
then
        echo -e "\e[1;31mError:\e[0m phing does not exist.";
        exit;
fi

if [[ ! -f $BUILDXML ]];
then
    echo -e "\e[1;31mError:\e[0m There is no build.xml[$BUILDXML].";
    exit;
fi

$PHING -f $BUILDXML -DVER_DESC=$2

这样我们就配置好了简单的发布和单元测试集成系统.

发布命令如下:

./new_publish.sh build.xml test

此条目发表在 好文推荐, 网站开发, 网站架构 分类目录,贴了 , 标签。将固定链接加入收藏夹。

评论功能已关闭。