如何在Zend Framework中使用PHPUnit?

时间:2023-01-13 10:08:34

I would like to know how to write PHPUnit tests with Zend_Test and in general with PHP.

我想知道如何用Zend_Test编写PHPUnit测试,一般用PHP编写。

6 个解决方案

#1


14  

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...

就这样...

#2


7  

They have an "Introduction to the Art of Unit Testing" on the Zend Developer Zone, which covers PHPUnit.

他们在Zend Developer Zone上有一个“单元测试艺术简介”,它涵盖了PHPUnit。

#3


2  

I found this article very useful. Also Zend_Test documentation helped a lot. With the help of these two resources, I managed to succesfully implement unit testing in the QuickStart tutorial of the Zend Framework and write few tests for it.

我发现这篇文章非常有用。 Zend_Test文档也有很多帮助。在这两个资源的帮助下,我成功地在Zend Framework的QuickStart教程中实现了单元测试,并为它编写了一些测试。

#4


1  

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 

#5


0  

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等编写了针对应用程序的测试。最重要的是在测试设置中获取足够的引导代码。

#6


0  

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);

}

#1


14  

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...

就这样...

#2


7  

They have an "Introduction to the Art of Unit Testing" on the Zend Developer Zone, which covers PHPUnit.

他们在Zend Developer Zone上有一个“单元测试艺术简介”,它涵盖了PHPUnit。

#3


2  

I found this article very useful. Also Zend_Test documentation helped a lot. With the help of these two resources, I managed to succesfully implement unit testing in the QuickStart tutorial of the Zend Framework and write few tests for it.

我发现这篇文章非常有用。 Zend_Test文档也有很多帮助。在这两个资源的帮助下,我成功地在Zend Framework的QuickStart教程中实现了单元测试,并为它编写了一些测试。

#4


1  

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 

#5


0  

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等编写了针对应用程序的测试。最重要的是在测试设置中获取足够的引导代码。

#6


0  

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);

}