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