gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:25:44 25 4
gpt4 key购买 nike

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

谢谢你

最佳答案

为了设置单元测试,我在测试目录中为 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-framework - 使用 Zend Framework/Doctrine 2.0 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3515768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com