开发者

How do I bootstrap for unit testing with Zend MVC and Zend_Test?

I have read many posts about setting up unit testing in Zend Framework and I simply have not been able to get even one simple unit test to run. The issue is with setting up and testing the bootstrap environment. I tried the simplest of ways using the ZFW docs, but I always get this error:

Zend_Config_Exception: parse_ini_file(/usr/local/zend/apache2/htdocs/APPBASE/tests/application.ini[function.parse-ini-file]: failed to open stream: No such file or directory

Here is phpunit.xml:

<phpunit bootstrap="./application/bootstrap.php" colors="true">
    <testsuite name="ApplicationTestSuite">
        <directory>./application/</directory>
        <directory>./library/</directory>
    </testsuite>
    <filter>
        <whitelist>
            <directory suffix=".php">../application</directory>
            <directory suffix=".php">../application/library</directory>
            <exclude>
                <directory suffix=".phtml">../application/views</directory>
                <file>../application/Bootstrap.php</file>
            </exclude>
       </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./log/coverage" charset="UTF-8"
         yui="false" highlight="false" lowUpperBound="35" highLowerBound="70"/>
    </logging>
</phpunit>

Here is my bootstrap (tests/application/bootstrap.php):

<?php
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ?   getenv('APPLICATION_ENV') : 'development'));

set_include_path(implode(PATH_SEPARATOR, array(
   realpath(APPLICATION_PATH . '/library'),
    get_include_path(),
)));

?>

The controller I am trying to test (tests/application/controllers/AuthControllerTest.php):

<?php
require_once 'ControllerTestCase.php';
/**
 * AuthController test case.
 */
class AuthControllerTest extends ControllerTestCase
{
    /**
     * @var AuthController
     */
    private $AuthController;
    /**
     * Prepares the environment before running a test.
     */
    public function setUp ()
    {
        parent::setUp();
        // TODO Auto-generated AuthControllerTest::setUp()
        $this->AuthController = new AuthController(/* parameters */);
    }
    /**
     * Cleans up the environment after running a test.
     */
    public function tearDown ()
    {
        // TODO Auto-generated AuthControllerTest::tearDown()
        $this->AuthController = null;
        parent::tearDown();
    }


    public function testCallWithoutActionShouldRedirectToLoginAction()
    {
        $this->dispatch('/auth');
        $this->assertController('auth');
        $this->assertAction('login');
    }
}

and ControllerTestCase.php (in /test/application/controllers):

<?php
require_once 'Zend/Applicatio开发者_开发百科n.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
 public $application;

 public function setUp()
 {
  $this->bootstrap = array($this, 'appBootStrap');
  parent::setUp();
 }

    public function appBootstrap()
    {
       $this->application = new Zend_Application(
                                  APPLICATION_ENV,
                                  APPLICATION_PATH . '/configs/application.ini'
                                  );

      $this->application->bootstrap();
    }

 public function tearDown()
 {
  Zend_Controller_Front::getInstance()->resetInstance();
  $this->resetRequest();
  $this->resetResponse();
  $this->request->setPost(array());
  $this->request->setQuery(array());
 }

}

my application.ini (APPBASE/configs/application.ini):

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] = ""
resources.view.doctype = "XHTML1_STRICT"
phpSettings.date.timezone = 'America/Chicago';

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

Note that the path in the error message does not line up with the path specified in my bootstrap. I thought at one point that the line "$this->application->bootstrap();" might be executing my regular app's bootstrap and changing the app path so I commented it out, but I have the same error regardless. If I "Run as PHP Unit Test" inside Zend Studio with this commented out, I get the original Zend Config Exception. If I run phpunit from the commandline, it can't find any of the controllers in my app. When I uncomment and run from the commandline, I get the Zend Config Exception. Running in Zend Studio always results in the Zend Config exception.

Can anyone offer some insight as to why I cannot get the application path set correctly?


You just have some of the paths wrong I think.

Based on the way you have your phpunit.xml I'd move the bootstap file up one dir to tests/

Then change the first path on line 1 in phpunit.xml to ./bootstrap.php

Then change the path for APPLICATION_PATH in bootstrap to /../application


Make Sure that you set your APPLICATION_ENV to testing like this.

defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ?   getenv('APPLICATION_ENV') :'testing'));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜