Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use PHPUnit with Zend Framework?

People also ask

How do I start PHPUnit?

PHPUnit InstallationThe first command creates a folder in your current directory, test-project and the second command moves into it. The last command starts an interactive shell. Follow the prompt, filling in the details as required (the default values are fine).

Is PHPUnit a framework?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. The currently supported versions are PHPUnit 9 and PHPUnit 8.

What is PHPUnit?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.

Is PHPUnit open source?

#3) PHPUnitIt is an open source testing tool used for PHP code. It is the most widely used framework for unit testing.


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