title: Zend 1.11 UnitTesting Controller Test Case
body: <p>I'm interested in adding unit testing in to my workflow and have started to create some tests on a Zend Framework 1.11 application running PHP 5.3.4 and PHPUnit 3.7.13</p>
  
  <p>One of the tests I've written wants to check for the occurrence of an <code>&lt;h1&gt;</code> in the response when loading the application.  The test passes when only rendering the view script, however when I add the layout paths to the application.ini in my main application the test fails as the view script isn't returned in the response (There are no errors when viewing the application through the browser, the layout and view renders correctly).</p>
  
  <p>When killing the test and outputting the content that assertQueryContentContains() uses to check for the DOM element, it shows that layout.phtml is being loaded in but is being treated like a flat file, showing PHP code as strings rather than being parsed.</p>
  
  <p>Is there a way of getting the unit test to load the whole MVC dispatch loop and return the content of the request in the same way it would as if it's been run through the browser?</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>application/configs/application.ini</strong></p>
  
  <pre><code>includePaths.library = APPLICATION_PATH "/../library"
  bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
  bootstrap.class = "Bootstrap"
  appnamespace = "Application"
  resources.layout.layout = "layout"
  resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
  resources.frontController.params.displayExceptions = 0
  </code></pre>
  
  <p><strong>tests/bootstrap.php</strong></p>
  
  <pre><code>/// 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') : 'testing'));
  
  // Ensure library/ is on include_path
  set_include_path(implode(PATH_SEPARATOR, array(
      realpath(APPLICATION_PATH . '/../library'),
      'C:\wamp\bin\library',
      get_include_path(),
  )));
  
  require_once 'Zend/Loader/Autoloader.php';
  Zend_Loader_Autoloader::getInstance();
  </code></pre>
  
  <p><strong>tests/application/controllers/IndexControllerTest.php</strong></p>
  
  <pre><code>public function setUp()
  {
  
      $this-&gt;bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
      parent::setUp();
  }
  
  public function testIndexAction()
  {
  
      $params = array('action' =&gt; 'index', 'controller' =&gt; 'index', 'module' =&gt; 'default');
      $urlParams = $this-&gt;urlizeOptions($params);
      $url = $this-&gt;url($urlParams);
      $this-&gt;dispatch($url);
  
      // assertions
      $this-&gt;assertModule($urlParams['module']);
      $this-&gt;assertController($urlParams['controller']);
      $this-&gt;assertAction($urlParams['action']);
  
  
      $this-&gt;assertQueryContentContains("div#jt h1", "Unit Testing");
  
  }
  </code></pre>
  
  <p><strong>Update #1</strong>
  Just to make this a little clearer when printing $this->getResponse()->getBody(); to screen I see:</p>
  
  <pre><code>/**
  * HTML
  */
  &lt;div class="container"&gt;
      &lt;?= $this-&gt;partial( 'partials/header.phtml' ); ?&gt;
  
      &lt;?= $this-&gt;layout()-&gt;content; ?&gt;
  
      &lt;div class="footer"&gt;
           &lt;p&gt;&amp;copy; Company 2013&lt;/p&gt;
      &lt;/div&gt;
  
  &lt;/div&gt;
  /**
  * More HTML
  */
  </code></pre>
  
  <p>When I expect layout()->content; ?> to show the content from the view script I'm testing.</p>