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

发布于 2024-09-15 03:19:56 字数 133 浏览 7 评论 0原文

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

谢谢

I wanted to write unit tests for my Zend Framework/Doctrine 2.0 application, but I don't quite understand how to set up unit testing in ZF. Also, I would like to include Doctrine 2.0 in those unit tests. How would I go about setting this up? Can you point me to an example?

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-09-22 03:19:56

为了设置单元测试,我在测试目录中创建了 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 的初始设置

To setup the unit tests I created a configuration file for phpunit (phpunit.xml) and a TestHelper.php in the test directory. The configuration basically says to phpunit which unit test needs to be executed and wich folders and files needs to be skipped in the coverage. In my config it just are all the unit test files in the application and library folder that are going to be executed.

The Testhelper needs to be extended by all your unit tests.

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 */
    }
}

This only covers the initial setup for ZF and PHPunit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文