Zend Framework:Zend_Testを始めよう
-
19-08-2019 - |
質問
Zend_Testの設定に成功した人はいますか?あなたの方法/アプローチは何でしたか?テスト/テストスイートはどのように実行しますか?
すでにPHPUnitがインストールされ、動作しています。今、私はいくつかの簡単なコントローラーテストを書き込もうとしています。 Zend Frameworkのドキュメントでは、オートロードがセットアップされていると想定していますが、これはまだ行っていません。適切なファイルを自動ロードするためにあなたはどの方法を使用しますか?通常のブートストラップファイルでこれを行いますが、多数のインクルードとパスの設定でテストを煩雑にしたくありません。抽象コントローラーのテストケースクラスを使用する方法はありますか?
ドキュメンテーションが使用するようなブートストラッププラグインはどうですか...テストをどのようにブートストラップするのですか、それとも別の方法で行いたいですか?できる限り通常のブートストラップファイルを再利用したいと思います。テストと通常の使用のためにブートストラップをどのように乾燥させるべきですか?
これまでのテストは次のとおりです。
<?php
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public function setUp()
{
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
$this->frontController->registerPlugin(new Bootstrapper('testing'));
}
public function testIndexAction()
{
$this->dispatch('/index');
$this->assertController('index');
$this->assertAction('index');
}
}
//straight from the documentation
class Bootstrapper extends Zend_Controller_Plugin_Abstract
{
/**
* @var Zend_Config
*/
protected static $_config;
/**
* @var string Current environment
*/
protected $_env;
/**
* @var Zend_Controller_Front
*/
protected $_front;
/**
* @var string Path to application root
*/
protected $_root;
/**
* Constructor
*
* Initialize environment, root path, and configuration.
*
* @param string $env
* @param string|null $root
* @return void
*/
public function __construct($env, $root = null)
{
$this->_setEnv($env);
if (null === $root) {
$root = realpath(dirname(__FILE__) . '/../../../');
}
$this->_root = $root;
$this->initPhpConfig();
$this->_front = Zend_Controller_Front::getInstance();
}
/**
* Route startup
*
* @return void
*/
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$this->initDb();
$this->initHelpers();
$this->initView();
$this->initPlugins();
$this->initRoutes();
$this->initControllers();
}
// definition of methods would follow...
}
どうすればよいですか
解決
これを機能させるために私がしたことは次のとおりです。
最初に、オートロードの問題を解決する必要があります。これを行うには、すべてのテストに含まれるファイルを作成し、testsディレクトリに配置します。注:このすべてを/public/index.phpからほとんどコピーしました。
# /tests/loader.php
<?php
define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application/'));
set_include_path( APPLICATION_PATH . '/../library' . PATH_SEPARATOR .
APPLICATION_PATH . '/models' . PATH_SEPARATOR .
APPLICATION_PATH . '/forms' . PATH_SEPARATOR .
get_include_path() );
require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();
次に、このファイルをテストに含める必要があります。テストファイルは/ tests / application / controllers /にあります。ブートストラップファイルはクイックスタートチュートリアル。場所をパブリック$ bootstrapとして設定するだけで、リンクします。 Zend_Test_PHPUnit_ControllerTestCase
が構築されると、ここで設定したブートストラップファイルが検索されます。
<?php
require_once '../../loader.php';
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public $bootstrap = '../../../application/bootstrap.php';
public function testIndexAction()
{
$this->dispatch('/index');
$this->assertController('index');
$this->assertAction('index');
}
}
これで終わりです!ブートストラップファイルが既にプラグインである場合、これはもう少し複雑かもしれませんが、そうではないので、とても簡単です。
他のヒント
使用する代わりに
require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();
変更先
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
registerAutoLoad()は1.8.0で非推奨になりました
基本的な初期化を行う簡単なTestHelper.phpを常に使用しています。このファイルは、すべてのテストケースファイルに含まれています。私がやることの1つは、Zend Frameworkのオートローダーを登録することです。特にフィルター、バリデーター、フォームを使用しているときに、大きな依存関係の問題が発生しました。すべての必要なファイルを追跡し、それらを手動でテストケースに含めることはほぼ不可能です。
この手順は実際のアプリケーションと同じであるため、オートローディング初期化とインクルードパスのセットアップをブートストラッププラグインに移動することができます。