duanaoshu1989 2010-07-21 20:58
浏览 90
已采纳

如何使用Zend MVC和Zend_Test进行单元测试的bootstrap?

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/Application.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?

  • 写回答

2条回答 默认 最新

  • dongzhangji4824 2010-07-21 21:26
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法