使用Zend Framework/Doctrine 2.0进行单元测试

5
我想为我的Zend Framework/Doctrine 2.0应用编写单元测试,但我不太明白如何在ZF中设置单元测试。此外,我想在这些单元测试中包含Doctrine 2.0。我该如何设置?你能给我一个例子吗?
谢谢。

我也很感兴趣如何在单元测试中包含Doctrine 2。我甚至在邮件列表上发布了一个关于此问题的问题,但没有得到任何答案。 - tom
我在这个问题上取得了一些进展,等我有几分钟的时间时,将在此帖子中发布我的设置。您认为是否应该在Doctrine 2.0模型层中测试持久性,还是不必担心,只需测试简单的PHP对象模型? - clarkstachio
我认为你应该只测试模型而不是Doctrine,因为他们会编写自己的单元测试。 你在这方面有进展了吗? - tom
我会在这个周末发布一些内容。不测试持久性会有两个问题:1)无法测试存储库自定义方法,2)如果您的任何模型依赖于自动ID才能正常工作,则由于需要使用实体管理器持久化方法,这将不可能。 - clarkstachio
嗨,如果你有可行的解决方案,能否发布一下?谢谢。 - tom
看看 Ocramius 在测试 ZF2/Doctrine 应用程序方面的 Gist https://gist.github.com/Ocramius/3994325 - Intellix
1个回答

2
为了设置单元测试,我在测试目录下创建了一个phpunit配置文件(phpunit.xml)和一个TestHelper.php。该配置文件告诉phpunit需要执行哪些单元测试以及需要跳过哪些文件和文件夹的覆盖率测试。在我的配置中,所有位于应用程序和库文件夹中的单元测试文件都将被执行。
所有的单元测试都需要扩展Testhelper。 phpunit.xml
<phpunit bootstrap="./TestHelper.php" colors="true">
    <testsuite name="Your Application">
        <directory>./application</directory>
        <directory>./library</directory>
    </testsuite>
    <filter>
        <whitelist>
            <directory suffix=".php">../application/</directory>
            <directory suffix=".php">../library/App/</directory>
            <exclude>
                <directory suffix=".phtml">../application/</directory>
                <directory suffix=".php">../application/database</directory>
                <directory suffix=".php">../application/models/Entities</directory>
                <directory suffix=".php">../application/models/mapping</directory>
                <directory suffix=".php">../application/models/proxy</directory>
                <directory suffix=".php">../application/views</directory>
                <file>../application/Bootstrap.php</file>
                <file>../application/modules/admin/controllers/ErrorController.php</file>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./log/report" title="PrintConcept" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70" />
        <log type="testdox" target="./log/testdox.html" />
    </logging>
</phpunit>

TestHelper.php

<?php
error_reporting(E_ALL | E_STRICT);

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define testing application environment
define('APPLICATION_ENV', 'testing');

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/**
 * Zend_Application
 */
require_once 'Zend/Application.php';

/**
 * Base Controller Test Class
 *
 * All controller test should extend this
 */
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

abstract class BaseControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{   
    public function setUp()
    {
        $application = new Zend_Application(APPLICATION_ENV,
                             APPLICATION_PATH . '/configs/application.ini');
        $this->bootstrap = array($application->getBootstrap(), 'bootstrap');

        Zend_Session::$_unitTestEnabled = true;

        parent::setUp();
    }

    public function tearDown()
    {
        /* Tear Down Routine */
    }
}

这只涵盖了ZF和PHPunit的初始设置


感谢您的反馈。您通常在所有测试用例中扩展Zend_Test_PHPUnit_ControllerTestCase吗?还是这个类只应该在控制器测试中扩展? - clarkstachio
我的测试都是继承自BaseControllerTestCase。但是通常你只需要为控制器测试继承自BaseControllerTestCase,而所有其他测试则继承自PHPUnit_Framework_TestCase。 - tom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接