ワークフローに単体テストを追加することに興味があり、PHP 5.3.4 および PHPUnit 3.7.13 を実行する Zend Framework 1.11 アプリケーションでいくつかのテストを作成し始めました。
私が作成したテストの 1 つは<h1>
、アプリケーションをロードするときに、応答での発生をチェックしたいと考えています。ビュー スクリプトのみをレンダリングする場合はテストに合格しますが、メイン アプリケーションの application.ini にレイアウト パスを追加すると、応答でビュー スクリプトが返されないため、テストは失敗します (アプリケーションを表示するときにエラーは発生しません)。ブラウザー、レイアウト、およびビューが正しくレンダリングされます)。
テストを強制終了し、assertQueryContentContains() が DOM 要素をチェックするために使用するコンテンツを出力すると、layout.phtml が読み込まれているがフラット ファイルのように扱われていることが示され、PHP コードが解析されるのではなく文字列として表示されます。
単体テストで MVC ディスパッチ ループ全体をロードし、ブラウザを介して実行された場合と同じ方法でリクエストのコンテンツを返す方法はありますか?
前もって感謝します!
アプリケーション/設定/application.ini
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
tests/bootstrap.php
/// 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();
テスト/アプリケーション/コントローラー/IndexControllerTest.php
public function setUp()
{
$this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
parent::setUp();
}
public function testIndexAction()
{
$params = array('action' => 'index', 'controller' => 'index', 'module' => 'default');
$urlParams = $this->urlizeOptions($params);
$url = $this->url($urlParams);
$this->dispatch($url);
// assertions
$this->assertModule($urlParams['module']);
$this->assertController($urlParams['controller']);
$this->assertAction($urlParams['action']);
$this->assertQueryContentContains("div#jt h1", "Unit Testing");
}
更新 #1 $this->getResponse()->getBody(); を印刷するときにこれを少し明確にするためだけに 画面に表示されます:
/**
* HTML
*/
<div class="container">
<?= $this->partial( 'partials/header.phtml' ); ?>
<?= $this->layout()->content; ?>
<div class="footer">
<p>© Company 2013</p>
</div>
</div>
/**
* More HTML
*/
layout()->content; を期待するとき ?> テスト中のビュー スクリプトのコンテンツを表示します。