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

问题描述:

我想为我的Zend Framework/Doctrine 2.0应用程序编写单元测试,但我不太了解如何在ZF中设置单元测试。另外,我想在这些单元测试中包含Doctrine 2.0。我将如何着手设置?你能指点我一个例子吗?使用Zend Framework/Doctrine 2.0进行单元测试

谢谢

+0

我也有兴趣在如何将在单元测试中的学说2。我甚至在邮件列表上发布了一个关于它的问题,但我没有得到任何答案。 – tom 2010-08-26 07:18:14

+0

我已经在这个问题上取得了一些进展,并且会在几分钟后发布我的设置。你认为应该在他们的Doctrine 2.0模型层中测试持久性,还是不用担心这个问题,并将模型作为普通的php对象来测试? – clarkstachio 2010-08-27 03:25:49

+0

我认为你应该只测试模型而不是学说。他们编写自己的单元测试。 你有没有取得进展? – tom 2010-08-31 08:05:23

要设置的单元测试,我创建了PHPUnit的(phpunit.xml)的配置文件和TestHelper.php test目录。配置基本上对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的初始设置

+0

感谢您的反馈。你通常在所有的测试用例中扩展Zend_Test_PHPUnit_ControllerTestCase吗?或者这个类只能扩展用于控制器测试? – clarkstachio 2010-08-27 03:27:05

+0

我的测试全部扩展了BaseControllerTestCase。但通常情况下,您应该只从BaseControllerTestCase扩展到您的控制器测试以及PHPUnit_Framework_TestCase的所有其他测试 – tom 2010-08-27 06:01:17