Advertisement
Advertisement


How do I use PHPUnit with Zend Framework?


Question

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

2011/03/11
1
41
3/11/2011 8:55:14 PM


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

2016/01/26

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.

2009/02/21

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 
2010/03/08

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.

2008/09/15

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

}
2015/12/09

Source: https://stackoverflow.com/questions/65683
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]