zend framework 2 + phpunit + 多模块 + 持续集成

Posted

技术标签:

【中文标题】zend framework 2 + phpunit + 多模块 + 持续集成【英文标题】:zend framework 2 + phpunit + multiple modules + continuous integration 【发布时间】:2013-05-13 18:23:06 【问题描述】:

提前感谢任何 cmets。我刚刚开始从 Zend Framework 1 切换到 ZF2,在完成快速入门和其他几个教程之后,我注意到使用 phpunit 的“默认”方式存在不足。要么就是这样,要么我只是迷路和困惑。

默认项目的文件夹结构是

Project
| - config
| | - autoload
| | | - global.php
| | | - local.php.dist
| | - application.config.php
| - data
| - module
| | - Application
| | | - config
| | | - src
| | | - test
| | | | - ApplicationTest
| | | | - Bootstrap.php
| | | | - phpunit.xml
| | | | - TestConfig.php.dist 
| | | - view
| | | - Module.php
| | - Album
| | | - config
| | | - src
| | | - test
| | | | - AlbumTest
| | | | - Bootstrap.php
| | | | - phpunit.xml
| | | | - TestConfig.php.dist 
| | | - view
| | | - Module.php
| - public
| - vendor

我的问题是如何使用 Jenkins 和 ANT 来测试所有的 phpunit 测试套件。我理解单独测试每个模块背后的原因,但是我怎样才能正确地自动化它来获取一个 report.xml。如果我不需要在 phpunit 配置中指定每个模块,那就更好了。或 build.xml。

再次感谢您的任何 cmets。

【问题讨论】:

【参考方案1】:

当我发现问题时,我忘记回答自己的问题了。我向社区道歉,我忘记了......但为了大家的利益,这里是我如何让它工作的。

构建.xml

<target name="phpunit" description="Run unit tests with PHPUnit">
    <apply executable="../vendor/bin/phpunit" parallel="false">
        <fileset dir="$env.WORKSPACE/module" >
            <include name="**/test/phpunit.xml"/>
        </fileset>
        <arg value="--configuration" />
        <srcfile/>
    </apply>
</target>

以及每个模块的 phpunit.xml

<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="Application">
            <directory>./</directory>
        </testsuite>
    </testsuites>

<!-- Filters only matter for code coverage reporting -->
    <filter>
        <blacklist>
            <directory>../../../vendor/</directory>
            <directory>./</directory>
            <file>../Module.php</file>
        </blacklist>
    </filter>
    <logging>
        <log type="coverage-html" target="../../../build/coverage" title="Application Module" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70"/>
        <log type="coverage-clover" target="../../../build/logs/clover-Application.xml"/>
        <log type="junit" target="../../../build/logs/junit-Application.xml" logIncompleteSkipped="false"/>
    </logging>
</phpunit>

【讨论】:

【参考方案2】:

好吧,我使用以下结构。我在测试文件夹中有所有测试,并且我以与构建模块相同的方式构建测试:

Project
| - config
| | - autoload
| | | - global.php
| | | - local.php.dist
| | - application.config.php
| - data
| - module
| | - Application
| | | - config
| | | - src
| | | | - Application
| | | | | - Controller
| | | | | | - IndexController.php
| | | | | - Model
| | | | | | - Foo.php
| | | | | - Form
| | | - view
| | | - Module.php
| | - Album
| | | - config
| | | - src
| | | | - Album
| | | | | - Controller
| | | | | | - IndexController.php
| | | | | - Model
| | | | | | - Bar.php
| | | | | - Form
| | | - view
| | | - Module.php
| - public
| - vendor
| - tests
| | - unit
| | | - module
| | | | - Application
| | | | | - src
| | | | | | - Application
| | | | | | | - Controller
| | | | | | | | - IndexControllerTest.php
| | | | | | | - Model
| | | | | | | | - FooTest.php
| | | | - Album
| | | | | - src
| | | | | | - Album
| | | | | | | - Controller
| | | | | | | | - IndexControllerTest.php
| | | | | | | - Model
| | | | | | | | - BarTest.php
| | - functional
| | | - features
| - phpunit.xml
| - phpunit-ci.xml
| - behat.yml

PHPUnit 配置可能看起来像这样(简化示例,根据您的需要添加白名单、过滤器、覆盖率等):

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/unit/Bootstrap.php" colors="true" backupGlobals="false" backupStaticAttributes="false" syntaxCheck="false">
    <testsuites>
        <testsuite name="sites">
            <directory suffix="Test.php">tests/unit</directory>
        </testsuite>
    </testsuites>
</phpunit>

phpunit-ci.xml 示例:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/unit/Bootstrap.php" colors="true" backupGlobals="false" backupStaticAttributes="false" syntaxCheck="false">
    <testsuites>
        <testsuite name="sites">
            <directory suffix="Test.php">tests/unit</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>    
            <!-- Album module -->
            <directory suffix=".php">module/Album/src/Album/Model</directory>
            <directory suffix=".php">module/Album/src/Album/Controller</directory>

            <!-- Application module -->
            <directory suffix=".php">module/Application/src/Application/Model</directory>
            <directory suffix=".php">module/Application/src/Application/Controller</directory>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="build/coverage" charset="UTF-8"
             yui="true" highlight="true" lowUpperBound="40" highLowerBound="80" />
        <log type="coverage-clover" target="build/logs/clover.xml" />
        <log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false" />
    </logging>
</phpunit>

在 build.xml 中很简单:

<target name="phpunit-ci" description="Run unit tests with config file for CI">
    <sequential>

        <exec executable="$basedir/vendor/bin/phpunit" failonerror="true">
            <arg value="--version" />
        </exec>

        <exec executable="$basedir/vendor/bin/phpunit" failonerror="true">
            <arg value="-c" />
            <arg path="$basedir/phpunit-ci.xml" />
        </exec>

    </sequential>
</target>

【讨论】:

以上是关于zend framework 2 + phpunit + 多模块 + 持续集成的主要内容,如果未能解决你的问题,请参考以下文章

如何配置代码-PHPUnit版本8.5.1对Zend Framework 2.4的覆盖范围?

面向初学者的 Selenium RC 和 PHP

如何在无类 php 文件上使用 PHPUnit Skeleton Generator?

Spring学习笔记

Spring 中的 spring-security-oauth2 与 spring-security-oauth2-core

spring 3 升级 spring 4.2.5遇到的问题