php 如何在 Zend 框架中使用 PHPUnit?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/65683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use PHPUnit with Zend Framework?
提问by
I would like to know how to write PHPUnit tests with Zend_Test and in general with PHP.
我想知道如何使用 Zend_Test 和一般的 PHP 编写 PHPUnit 测试。
回答by Stefan Gehrig
I'm using Zend_Test to completely test all controllers. It's quite simple to set up, as you only have to set up your bootstrap file (the bootstrap file itself should NOT dispatch the front controller!). My base test-case class looks like this:
我正在使用 Zend_Test 来完全测试所有控制器。设置非常简单,因为您只需要设置引导程序文件(引导程序文件本身不应该调度前端控制器!)。我的基本测试用例类如下所示:
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();
}
}
where Application::setup();does all the setup up tasks which also set up the real application. A simple test then would look like this:
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');
}
}
That's all...
就这样...
回答by isuldor
They have an "Introduction to the Art of Unit Testing" on the Zend Developer Zone, which covers PHPUnit.
他们在 Zend 开发人员专区上有一篇“单元测试艺术简介”,其中涵盖了 PHPUnit。
回答by Josef Sábl
I found thisarticle very useful. Also Zend_Testdocumentation helped a lot. With the help of these two resources, I managed to succesfully implement unit testing in the QuickStart tutorialof the Zend Framework and write few tests for it.
我发现这篇文章非常有用。此外Zend_Test文档帮了不少忙。在这两个资源的帮助下,我成功地在 Zend Framework的快速入门教程中实现了单元测试,并为其编写了一些测试。
回答by Alex
Using ZF 1.10, I put some bootstrap code into tests/bootstrap.php (basically what is in (public/index.php), until $application->bootstrap().
使用 ZF 1.10,我将一些引导代码放入 tests/bootstrap.php(基本上是 (public/index.php) 中的内容,直到 $application->bootstrap())。
Then I am able to run a test using
然后我可以使用
phpunit --bootstrap ../bootstrap.php PersonControllerTest.php
回答by KdPurvesh
Plus if you using a database transaction then it would be best to delete all the transaction that is gets done via a unit test otherwise your database gets all messed.
另外,如果您使用数据库事务,那么最好删除通过单元测试完成的所有事务,否则您的数据库会变得一团糟。
so on set up
等等设置
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();
}
and on teardown all you need to do is rollback
在拆卸时,您需要做的就是回滚
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);
}
回答by Sam Corder
I haven't used Zend_Test but I have written tests against apps using Zend_MVC and the like. The biggest part is getting enough of your bootstrap code in your test setup.
我没有使用过 Zend_Test,但我已经使用 Zend_MVC 等编写了针对应用程序的测试。最重要的部分是在测试设置中获得足够的引导代码。

