3

I'm just getting started with unit testing in the zend framework. I've read lots of the docs ZF and PhpUnit but there are a few things I can't figure out.

  • I have Zend Framework set up. Do I need to install PHPUnit too or is it all sorted within the framework?
  • Zend Framework has created files for the unit tests under /tests/application/controllers/ControllerNameTest.php. I assume I create my tests here.
  • How do I run a test? I'm sure this is really simple because the docs I have read assume I should know how to do this. Do I do it from the command line? If so, how?

Any help appreciated.

Thanks

3 Answers 3

4

You need to install PHPUnit, too. Zend Framework and PHPUnit are two different things. You find the installation instructions for PHPUnit here: https://github.com/sebastianbergmann/phpunit.

Basically, You can put the tests where you want. But the tests-Folder is a good place for them.

After you have installed phpunit, you can call your unit tests from the command line. Just enter the folder where you put your tests on command line and type "phpunit", this will run all tests in the folder. You can also use the --filter option to run a single test.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I now have phpunit installed and can run it from the command line. In this document: framework.zend.com/manual/en/zend.test.phpunit.html it demonstrates some tests but I can't figure out how phpunit will get the context for the whole zend framework application.
3

You need to install the phpunit. The easiest way in my opinion is using PEAR (http://www.phpunit.de/manual/current/en/installation.html#installation.pear).

For testing controllers you can replicate the same code which you found in the ControllerNameTest.php file. For testing models you can create a PHPUnit test case.

<?php 

class YourApp_Model_YourModel  extends PHPUnit_Framework_TestCase
{
    public function setUp()
    {
        $application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );

        $application->bootstrap('config')
                             ->bootstrap('defaultModuleAutoloader')
                             ->bootstrap('autoloader');

            // You might need to add few more bootstrap, depends on your needs
    }

    public function testSomeMethod(){}

I hope that helps.

Comments

0

I have found a great guide to setting everything up here with PHPUnit and Zend Framework here:

http://grover.open2space.com/content/unit-testing-zend-framework-111-and-phpunit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.