如何将PHPUnit与Zend框架一起使用?

如何将PHPUnit与Zend框架一起使用?,php,unit-testing,zend-framework,testing,phpunit,Php,Unit Testing,Zend Framework,Testing,Phpunit,我想知道如何使用Zend_Test和PHP编写PHPUnit测试。我没有使用Zend_Test,但我已经针对使用Zend_MVC等的应用编写了测试。最大的部分是在测试设置中获得足够的引导代码。我使用Zend_测试来完全测试所有控制器。设置非常简单,因为您只需设置引导文件(引导文件本身不应分派前端控制器!)。我的基本测试用例类如下所示: abstract class Controller_TestCase extends Zend_Test_PHPUnit_ControllerTestCase {

我想知道如何使用Zend_Test和PHP编写PHPUnit测试。

我没有使用Zend_Test,但我已经针对使用Zend_MVC等的应用编写了测试。最大的部分是在测试设置中获得足够的引导代码。

我使用Zend_测试来完全测试所有控制器。设置非常简单,因为您只需设置引导文件(引导文件本身不应分派前端控制器!)。我的基本测试用例类如下所示:

abstract class Controller_TestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected function setUp()
    {
        $this->bootstrap=array($this, 'appBootstrap');
        Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_NonPersistent());
        parent::setUp();
    }

    protected function tearDown()
    {
        Zend_Auth::getInstance()->clearIdentity();
    }

    protected function appBootstrap()
    {
        Application::setup();
    }
}
class Controller_IndexControllerTest extends Controller_TestCase
{
    public function testShowist()
    {
        $this->dispatch('/');
        $this->assertController('index');
        $this->assertAction('list');
        $this->assertQueryContentContains('ul li a', 'Test String');
    }
}
其中
Application::setup()执行所有同样设置实际应用程序的设置任务。一个简单的测试如下所示:

abstract class Controller_TestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected function setUp()
    {
        $this->bootstrap=array($this, 'appBootstrap');
        Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_NonPersistent());
        parent::setUp();
    }

    protected function tearDown()
    {
        Zend_Auth::getInstance()->clearIdentity();
    }

    protected function appBootstrap()
    {
        Application::setup();
    }
}
class Controller_IndexControllerTest extends Controller_TestCase
{
    public function testShowist()
    {
        $this->dispatch('/');
        $this->assertController('index');
        $this->assertAction('list');
        $this->assertQueryContentContains('ul li a', 'Test String');
    }
}
就这些了…

他们在Zend开发者专区上有一个“”,它涵盖了PHPUnit。

我发现这篇文章非常有用。文档也有很大帮助。
在这两种资源的帮助下,我成功地在Zend框架中实现了单元测试,并为它编写了一些测试。

使用ZF 1.10,我将一些引导代码放入tests/bootstrap.php(基本上是(public/index.php)中的代码,直到$application->bootstrap()

然后我可以使用

phpunit --bootstrap ../bootstrap.php  PersonControllerTest.php 

另外,如果您使用数据库事务,那么最好删除通过单元测试完成的所有事务,否则您的数据库会变得一团糟

所以就成立了

public function setUp() {



    YOUR_ZEND_DB_INSTANCE::getInstance()->setUnitTestMode(true);



    YOUR_ZEND_DB_INSTANCE::getInstance()->query("BEGIN");

    YOUR_ZEND_DB_INSTANCE::getInstance()->getCache()->clear();

    // Manually Start a Doctrine Transaction so we can roll it back
    Doctrine_Manager::connection()->beginTransaction();
}
在拆卸时,您需要做的就是回滚

public function tearDown() {



    // Rollback Doctrine Transactions
    while (Doctrine_Manager::connection()->getTransactionLevel() > 0) {
        Doctrine_Manager::connection()->rollback();
    }

    Doctrine_Manager::connection()->clear();



    YOUR_ZEND_DB_INSTANCE::getInstance()->query("ROLLBACK");
    while (YOUR_ZEND_DB_INSTANCE::getInstance()->getTransactionDepth() > 0) {
        YOUR_ZEND_DB_INSTANCE::getInstance()->rollback();
    }
    YOUR_ZEND_DB_INSTANCE::getInstance()->setUnitTestMode(false);

}

你能详细说明一下吗?我喜欢你的抽象测试用例类!你把你的Controller\u TestCase放在哪里?你需要/包括任何文件吗?你如何运行测试?你是通过命令行使用PHPUnit,还是在IDE中使用插件?谢谢!我把Controller\u TestCase放在我的tests/Controller目录中-这就是我的tests/Controller目录所有的控制器测试用例都位于其中。通常我在运行单元测试时不使用自动加载,但如果我进行控制器测试,则使用Zend_Loader::registerAutoload()使用自动加载,因为在MVC运行时很难跟踪依赖项。您只需确保遵循命名约定,Zend_Loader将找到您的文件(设置include_path)。要运行我的测试,我使用PHPUnit的CLI,因为我不喜欢Eclipse PDT中的PHPUnit集成。切换窗口启动测试……实际上没有问题,特别是如果您遵循PHPUnit命名约定(与Zend Framework的约定相同)并使用TestSuite对您的测试进行分组。也许您应该看看Zend Framework本身使用的测试结构。link当前已中断