1
votes

I am trying to learn about unit testing in Zend Framework. I have installed PHPunit (not without a few problems on the way).

I have also created a simple Zend/Doctrine 2 skeleton that works fine and successfully writes to the DB etc.

I just want to start using and understanding PHPunit, but it is giving strange errors. I have an empty IndexController that does absolutely nothing. I just want to assert that it exists. PHPunit throws up a huge list of errors relating to the Bootstrap, but the bootstrap works perfectly in practice.

It's as if phpunit doesn't respect my include paths.

ps. If you have already read this post, I have simplified the skeleton project and edited the code and errors below to reflect this. Hopefully this will make it easier to debug.

I think some code my help explain:

demian@dimbo-TP:/var/www/z2d2/tests$ phpunit --version
PHPUnit 3.6.3 by Sebastian Bergmann.


<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{

    public function setUp()
    {

      $this->bootstrap = new Zend_Application(
        'development',
        APPLICATION_PATH . '/configs/application.ini'
      );

        parent::setUp();
    }

    public function tearDown()
    {
      parent::tearDown();
    }

}


<?php

class IndexControllerTest extends ControllerTestCase
{

    public function testDoesHomePageExist() 
    {
        $this->dispatch('/');
        $this->assertController('index');
        $this->assertAction('index');

    }   

}

This is my simple bootstrap:

 <?php

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {

      /**
       *
       *
       *
       *
       */
      protected function _initConfig()
      {

        $config = new Zend_Config($this->getOptions());
        Zend_Registry::set('config', $config);

      }

      /**
       *
       *
       *
       *
       */
      protected function _initDoctrine() {

        require_once('Doctrine/Common/ClassLoader.php');

        $autoloader = Zend_Loader_Autoloader::getInstance();
        $classLoader = new \Doctrine\Common\ClassLoader('Entities',
          realpath(Zend_Registry::get('config')->resources->entityManager->connection->entities), 'loadClass');

        $autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Entities');

        $classLoader = new \Doctrine\Common\ClassLoader('Repositories',
          realpath(Zend_Registry::get('config')->resources->entityManager->connection->entities), 'loadClass');

        $autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Repositories');    

      }

    }

And Now the Errors (Sorry for the length of it):

    demian@dimbo-TP:/var/www/z2d2/tests$ phpunit
PHPUnit 3.5.15 by Sebastian Bergmann.

PHP Fatal error:  Bootstrap::_initDoctrine(): Failed opening required 'Doctrine/Common/ClassLoader.php' (include_path='/var/www/z2d2/application/../views:.:/usr/share/php:/usr/share/php/:/usr/local/share/php/library') in /var/www/z2d2/application/Bootstrap.php on line 28
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:49
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:188
PHP   5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:305
PHP   6. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:693
PHP   7. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:733
PHP   8. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:757
PHP   9. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:576
PHP  10. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:666
PHP  11. ModelTestCase->setUp() /usr/share/php/PHPUnit/Framework/TestCase.php:626
PHP  12. Zend_Application->bootstrap() /var/www/z2d2/tests/application/models/ModelTestCase.php:16
PHP  13. Zend_Application_Bootstrap_BootstrapAbstract->bootstrap() /usr/local/share/php/library/Zend/Application.php:355
PHP  14. Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap() /usr/local/share/php/library/Zend/Application/Bootstrap/BootstrapAbstract.php:586
PHP  15. Zend_Application_Bootstrap_BootstrapAbstract->_executeResource() /usr/local/share/php/library/Zend/Application/Bootstrap/BootstrapAbstract.php:622
PHP  16. Bootstrap->_initDoctrine() /usr/local/share/php/library/Zend/Application/Bootstrap/BootstrapAbstract.php:669

Fatal error: Bootstrap::_initDoctrine(): Failed opening required 'Doctrine/Common/ClassLoader.php' (include_path='/var/www/z2d2/application/../views:.:/usr/share/php:/usr/share/php/:/usr/local/share/php/library') in /var/www/z2d2/application/Bootstrap.php on line 28

Call Stack:
    0.0002     326940   1. {main}() /usr/bin/phpunit:0
    0.0424     666604   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:49
    0.0424     667084   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
    0.0924    4311848   4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:188
    0.1036    4751832   5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:305
    0.1037    4752248   6. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:693
    0.1038    4752576   7. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:733
    0.1038    4752576   8. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:757
    0.1040    4752576   9. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:576
    0.2036    4787392  10. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:666
    0.2082    4891936  11. ModelTestCase->setUp() /usr/share/php/PHPUnit/Framework/TestCase.php:626
    0.2567    5678688  12. Zend_Application->bootstrap() /var/www/z2d2/tests/application/models/ModelTestCase.php:16
    0.2568    5678732  13. Zend_Application_Bootstrap_BootstrapAbstract->bootstrap() /usr/local/share/php/library/Zend/Application.php:355
    0.2569    5678732  14. Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap() /usr/local/share/php/library/Zend/Application/Bootstrap/BootstrapAbstract.php:586
    0.2609    5699280  15. Zend_Application_Bootstrap_BootstrapAbstract->_executeResource() /usr/local/share/php/library/Zend/Application/Bootstrap/BootstrapAbstract.php:622
    0.2610    5699472  16. Bootstrap->_initDoctrine() /usr/local/share/php/library/Zend/Application/Bootstrap/BootstrapAbstract.php:669

Can anyone show me where I am going wrong? I even suspect that my PHPunit is not set up correctly

1
There is seem to be something wrong with the autoloader but I can't see it so fast. Also Zend Framework 1.x is unfortunately not compatible with PHPUnit 3.6.x but that doesn't cause this problem in the first place but it will break up your script anyway later.Kees Schepers
Thanks - I think something went wrong with the install. Should I try to uninstall? Have you got any suggestions on how I should uninstall - eg. though PEAR or the package manager? Which version would you recommend I install with zend 1.x?dimbo
I don't think your problem in this topic is the installation but after fixing that problem you will walk into this phpunit version problem. Installing a old version kind a hard but you should first delete phpunit 3.6 with all dependencies. Then you can install phpunit by specifing the version number like: pear install phpunit/PHPUnit-3.5.14 with all dependencies.Kees Schepers
I have downgraded to 3.5.15 and the problem persists - as you said it would :)dimbo
Do you have a file located on the include path at WJG/Controller/Action/Helper/Initializer.php? Is this one of your own action helpers?David Harkness

1 Answers

3
votes

I solved it in this way: it seems that PHPunit does not like the relative include paths in the main application bootstrap file - this is what causes the problem. So, in the testing bootstrap pointed to by phpunit.xml make sure you specify the include paths. I added these lines :

set_include_path(
    '.'
    . PATH_SEPARATOR . BASE_PATH . '/library'
    . PATH_SEPARATOR . get_include_path()
);