简体   繁体   中英

How do I use PHPUnit with Zend Framework?

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

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:

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:

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

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

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.

Using ZF 1.10, I put some bootstrap code into tests/bootstrap.php (basically what is in (public/index.php), until $application->bootstrap().

Then I am able to run a test using

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

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

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM