title: In a Zend framework 1 application, PHPUnit fails to load application.ini in Controllers but works in Models
body: <p>We are using Zend framework 1.11.11. Our server has PHPUnit 3.7.9. We know that our version of Zend Framework doesn't support PHPUnit 3.7.9. But so far we have been able to use PHPUnit for testing models.</p>
  
  <p>However, when it comes to testing controllers, PHPUnit fails. After placing <code>dispatch()</code> code in <code>try/catch block</code>, we learned that it was unable to load one property from <code>Zend_Registry</code>. It is defined <code>application.ini</code>. Same property is used in models and we have no issues there.</p>
  
  <p>Is it possible that we are missing something? Or is it just not possible to use PHPUnit 3.7.9 with our version of ZF?</p>
  
  <p>Here is phpunit.xml:</p>
  
  <pre><code>&lt;phpunit bootstrap="./application/bootstrap.php" colors="true"&gt;
      &lt;testsuite name="Precision Tests"&gt;
      &lt;directory&gt;./&lt;/directory&gt;
  &lt;/testsuite&gt;
  
  &lt;filter&gt;
      &lt;whitelist&gt;
          &lt;directory suffix=".php"&gt;../application/&lt;/directory&gt;
          &lt;directory suffix=".php"&gt;../library/&lt;/directory&gt;
          &lt;exclude&gt;
              &lt;directory suffix=".phtml"&gt;../application/&lt;/directory&gt;
              &lt;directory&gt;../library/Zend&lt;/directory&gt;
              &lt;file&gt;../application/Bootstrap.php&lt;/file&gt;
              &lt;file&gt;../application/controllers/ErrorController.php&lt;/file&gt;
          &lt;/exclude&gt;
      &lt;/whitelist&gt;
  &lt;/filter&gt;
  &lt;logging&gt;
      &lt;log type="coverage-html" target="./log/report" charset="UTF-8" yui="true" hightlight="true"
          lowupperbound="50" highlowerbound="80"&gt;
          &lt;log type="testdox" target="./log/testdox.html"&gt;
          &lt;/log&gt;
      &lt;/log&gt;
      &lt;/logging&gt;
  &lt;/phpunit&gt;
  </code></pre>
  
  <p>The bootstrap.php in test:</p>
  
  <pre><code>&lt;?php
  
  function load($name) {
      $paths = array(
          "../application/controllers",
          "../library",
          "../application/models"
      );
      foreach($paths as $dir) {
          $name = str_replace("_", "/", $name);
          if(file_exists($dir."/".$name.".php")) {
              require_once($dir."/".$name.".php");
              break;
          }
      }
  }
  
  error_reporting(E_ALL | E_STRICT);
  
  // Define path to application directory
  defined('APPLICATION_PATH')
  || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
  
  // Define application environment
  defined('APPLICATION_ENV')
  || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
  $_SERVER["APPLICATION_ENV"] = getenv('APPLICATION_ENV');
  $_SERVER["APPLICATION_ENV"] = 'development';
  
  set_include_path(implode(PATH_SEPARATOR, array(
      realpath(APPLICATION_PATH . '/../library'),
      realpath(APPLICATION_PATH . '/controllers'),
      get_include_path(),
  )));
  
  require_once 'Zend/Application.php';
  $application = new Zend_Application(
      APPLICATION_ENV,
      APPLICATION_PATH . '/configs/application.ini'
  );
  
  //setting up env variables:
  $_SERVER["REMOTE_ADDR"] = '0.0.0.0';
  $_SERVER['SERVER_NAME'] ='PHP.UNIT.TEST';
  $_SERVER['REQUEST_URI'] = 'PHPUNITTEST';
  $_SERVER['QUERY_STRING'] = 'PHPUNITTEST';
  
  require_once 'Zend/Loader/Autoloader.php';
  $autoloader = Zend_Loader_Autoloader::getInstance();
  $autoloader-&gt;registerNamespace('VP');
  $autoloader-&gt;pushAutoloader("load");
  spl_autoload_register();
  
  require_once 'ControllerTestCase.php';
  </code></pre>
  
  <p>Then here is <code>ControllerTestCase.php</code>. Our models also extend same <code>ControllerTestCase</code>:</p>
  
  <pre><code>&lt;?php
  
  require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
  
  class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
  {
      /**
      * @var Zend_Application
      */
      protected $application;
  
      public function setUp() {
          $this-&gt;bootstrap = array($this, 'appBootstrap');
          parent::setUp();
      }
  
      public function appBootstrap() {
          $this-&gt;application = new Zend_Application(APPLICATION_ENV,
              APPLICATION_PATH . '/configs/application.ini');
          $this-&gt;application-&gt;bootstrap();
      }
  }
  </code></pre>
  
  <p>And here is an example controller that doesn't load properties from application.ini:</p>
  
  <pre><code>&lt;?php
  
  class MyControllerTest extends ControllerTestCase
  {
      public function setUp()
      {
          $this-&gt;getFrontController()-&gt;setControllerDirectory(APPLICATION_PATH . '/controllers');
          Zend_Controller_Action_HelperBroker::addHelper(new Zend_Layout_Controller_Action_Helper_Layout);
      }
  
  
      public function testSubscriptionStatus()
      {
          try{
              $this-&gt;dispatch('/my/subscriptionstatus');
              $this-&gt;assertController('my');
              $this-&gt;assertAction('subscriptionstatus');
  
          } catch (Exception $e) {
              print_r($e-&gt;getTrace());
          }
      }
  }
  </code></pre>
  
  <p>And here how we setup our test models:</p>
  
  <pre><code>&lt;?php
  
  class Model_CustomerTest extends ControllerTestCase
  {
      public function testDoSearch(){
          $x = Customer::doSearch('s');
          $this-&gt;assertTrue(count($x) &gt; 0);
      }
  
  }
  </code></pre>
  
  <p>And finally here is offending code that works in models but not in controllers (when running PHPUnit):</p>
  
  <pre><code>$registry = Zend_Registry::getInstance();
  $options = $registry-&gt;webservices['logging'];
  </code></pre>
  
  <p>We have setup our PHPUnit testing environment by reading several different resources on the web. None of us are PHPUnit experts. So we just don't know what is going on.</p>
  
  <p>Thank you!</p>