1

Zend Frameworkでの単体テストの設定に関する多くの投稿を読みましたが、単純な単体テストを1つでも実行できませんでした。問題は、ブートストラップ環境のセットアップとテストにあります。ZFWドキュメントを使用して最も簡単な方法を試しましたが、常に次のエラーが発生します。

Zend_Config_Exception:parse_ini_file(/usr/local/zend/apache2/htdocs/APPBASE/tests/application.ini [function.parse-ini-file]:ストリームを開くことができませんでした:そのようなファイルまたはディレクトリはありません

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>

これが私のブートストラップ(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(),
)));

?>

テストしようとしているコントローラー(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');
    }
}

およびControllerTestCase.php(/ 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());
 }

}

私の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

エラーメッセージのパスが、ブートストラップで指定されたパスと一致しないことに注意してください。ある時点で、「$ this-> application-> bootstrap();」という行があると思いました。通常のアプリのブートストラップを実行してアプリのパスを変更している可能性があるため、コメントアウトしましたが、それでも同じエラーが発生します。これをコメントアウトした状態でZendStudio内で「PHPユニットテストとして実行」すると、元のZendConfig例外が発生します。コマンドラインからphpunitを実行すると、アプリ内のコントローラーが見つかりません。コメントを外してコマンドラインから実行すると、ZendConfig例外が発生します。Zend Studioで実行すると、常にZendConfig例外が発生します。

アプリケーションパスを正しく設定できない理由について、誰かが洞察を提供できますか?

4

2 に答える 2

1

あなたは私が思うに間違った道のいくつかを持っているだけです。

phpunit.xmlの使用方法に基づいて、bootstapファイルを1つ上のディレクトリに移動してtests/に移動します。

次に、phpunit.xmlの1行目の最初のパスを./bootstrap.phpに変更します。

次に、ブートストラップ内のAPPLICATION_PATHのパスを/../applicationに変更します

于 2010-07-21T21:26:30.313 に答える
0

APPLICATION_ENVをこのようにテストするように設定していることを確認してください。

defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ?   getenv('APPLICATION_ENV') :'testing'));
于 2013-07-28T19:42:44.253 に答える