Unit Testing with the Zend Framework with Zend_Test and PHPUnit

June 11th, 2009

I have to preface this video by saying that I’m still a bit of a novice when it comes to unit testing (especially in Zend). Also, I feel that I wouldn’t be able to take credit for the whole implementation.

Here are some great resources on unit testing in the Zend Framework to beef up your knowledge.

The trouble with these is that they’re mostly pre-1.8. I’ve taken the approach of using a command line instead of the IDE since this way it doesn’t matter if you’re using Zend Studio for Eclipse.

What’s covered:

  • Using phpunit with MAMP
  • Unit Testing Models
  • Generating Code Coverage Reports
  • Unit Testing Controllers

Don’t forget to grab the source code or browse it on google code.

 

61 Responses to “Unit Testing with the Zend Framework with Zend_Test and PHPUnit”

  1. paul says:

    oh sorry, and I am using Zend Server for php 5.3

  2. David Jung says:

    I had some trouble to debug why my logging was filing after the first unit test was run.

    I was grabbing my logger instance from Zend_Registry.

    Well, it seems that PHPUnit resets all global variables and class static variables between tests, hence the static variable Zend_Registry uses to reference its singelton instance was being destroyed.

    To work-around that I had to add a declaration to my TestCase class:

    protected $backupStaticAttributesBlacklist = array(
    ‘Zend_Registry’ => array(’_registry’)
    );

    That ensures that values set in the Zend registry are available to all unit tests.
    (obviously have to be careful if the code you’re testing interacts with the registry in any non-trivial way. Mine doesn’t).

  3. Enrique says:

    Hi,

    Just wanted to ask for a little help here..

    Following the tutorial, I managed to work my way until the creation of ControllerTestCase. When I started testing on the command line( I’m using Windows Vista, Zend Framework ), the prompt seemed to be pausing for a second then it it displays nothing. I tried running the bootstrap.php( almost similar to the bootstrap file demonstrated here ) by php and saw that error is:

    PHP Fatal Error: Cannot redeclare class phpunit_framework_testcase on line 117.

    I guess that’s why the phpunit does not display anything — the ControllerTestCase that I use( the one extending the Zend_Test_PHPUnit_ControllerTestCase ) fails.

    I’ve been searching for the same issue over the internet to no avail.

    Thanks for you help..

  4. Hi Jon,

    I’ve checked and checked and I can’t find phpunit within MAMP. I just went through and upgraded MAMP to the latest 1.8.3 and went to the directory you use: /Applications/MAMP/bin/php5/bin While the other 6 files that you show in your video are there, phpunit is not. (The other six are pear, peardev, pecl, php, php-config, phpize)

    Are you sure you didn’t install it separately first? Is it possible that it was in an older version of MAMP and has subsequently been removed?

  5. Michiel Thalen says:

    Do you know how to run this via zend studio? It seems it only can execute phpunit files (not using the configuration).
    If i run phpunit from the commandline it needs xdebug for code coverage, and that disables the debug function.

  6. melissa says:

    i want to start using phpunit for my web app and i am grateful for this video showing the basic steps. most of the pages in my app require the visitor to login . when i try to run any code via phpunit that is dispatching to the login page, i get error = ‘Zend_session is not marked as readable’.
    here is my test method code:
    public function testLoginActionPost()
    {
    $this->getRequest()
    ->setmethod(’POST’);
    $this->dispatch(”/auth/login”);
    $this->assertController(”auth”);
    $this->assertAction(”login”);
    $this->assertResponseCode(200);
    }

    and here is the error:
    There was 1 error:

    1) IndexControllerTest::testLoginActionPost
    Zend_Session_Exception: Zend_Session is not marked as readable.

    library\Zend\Session\Abstract.php:86
    library\Zend\Session\Namespace.php:391
    library\Zend\Auth\Storage\Session.php:117
    library\Zend\Auth.php:133
    application\views\helpers\BuildMenu.php:13
    library\Zend\View\Abstract.php:342
    application\layouts\layout.phtml:11
    library\Zend\View.php:108
    library\Zend\View\Abstract.php:833
    library\Zend\Layout.php:793
    library\Zend\Layout\Controller\Plugin\Layout.php:142
    library\Zend\Controller\Plugin\Broker.php:331
    library\Zend\Controller\Front.php:957
    library\Zend\Test\PHPUnit\ControllerTestCase.php:190
    tests\application\controllers\IndexControllerTest.php:45

    if i follow the code in my BuildMenu.php, it is merely truying to check
    if the user is logged in, when the exception is thrown:
    $auth = Zend_Auth::getInstance();
    if ($auth->hasIdentity()) { <——— this line causes Zend_Session_Exception: Zend_Session is not marked as readable.
    to be thrown.

  7. osdave says:

    for melissa (and maybe others), i had a similar issue and got answered in stackoverflow: http://stackoverflow.com/questions/1808148/unit-testing-phpunit-how-to-login
    i hope it helps.
    cheers

  8. osdave says:

    sorry for double posting, i forgot to check the followup notify checkbox…

  9. Mike says:

    Hi,

    My architecture is quite similar than yours, my problem is after adding that line of code in my ControllerTestCase.php file :
    “$this->application->bootstrap();”
    I still get the following error : “Zend_Controller_Exception: No default module defined for this application”
    except for this simple test :”$this->assertTrue(TRUE);”

    Any idea ?

    Thanks,

  10. jon says:

    Hi Mike,
    I would double-check your application.ini file.

  11. James says:

    @paul (late but for others who find this via Google as I did)

    There doesn’t seem to be a solution for using Xdebug with Zend Server CE 4.0.5. The “official” response is:

    “The PHP project only supports VC6 and VC9, and not VC8. Therefore I only
    have VC6 and VC9 versions for Xdebug. ”
    Source: http://xdebug.org/archives/xdebug-general/1634.html

Leave a Reply

Desktop RSS feed iPhone + iPod