views:

6138

answers:

5

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

A: 

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.

Sam Corder
+6  A: 

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

Stefan Gehrig
Can you elaborate a little more on this? I like your abstract TestCase class! Where do you put your Controller_TestCase? Do you need to require/include any files? How do you run a test? Do you use PHPUnit via the command line, or with a plugin in an IDE? Thanks!
Andrew
I put Controller_TestCase in my tests/Controller directory - that's the directory where all controller test cases are located. Generally I don't use auto-loading when running unit-tests but if I do controller tests, I use Zend_Loader::registerAutoload() to use auto-loading because it's very...
Stefan Gehrig
... difficult to track dependencies with the MVC running. You just have to make sure that you follow the naming convention and that Zend_Loader will find your files (set include_path).To run my tests I use the CLI of PHPUnit because I don't like the PHPUnit integration in Eclipse PDT. It's...
Stefan Gehrig
... actually no problem to switch windows to start the tests - especially if you follow the PHPUnit naming convention (the same as Zend Framework's convention) and use TestSuites to group your tests.Perhaps you should have a look at the testing structure used by e.g. the Zend Framework itself.
Stefan Gehrig
+2  A: 

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

Brian
Very nice read.
sims
+1  A: 

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.

Josef Sábl
A: 

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 
Alex