Php Zend框架:Zend_测试入门
有人成功设置Zend_测试吗?您的方法/途径是什么?您如何运行测试/测试套件 我已经安装了PHPUnit并正在工作。现在我正在尝试编写一些简单的控制器测试。Zend Framework文档假定自动加载已经设置好,我还没有这样做。您使用什么方法自动加载适当的文件?我在正常的引导文件中这样做,但我不想用一堆包含和设置路径来扰乱我的测试。抽象控制器测试用例类会是一种选择吗 文档中使用的引导插件是如何引导测试的,还是您希望以不同的方式引导测试?我想重复使用尽可能多的常规引导文件。我应该如何干燥我的引导测试和正常使用 以下是我目前的测试:Php Zend框架:Zend_测试入门,php,unit-testing,zend-framework,testing,phpunit,Php,Unit Testing,Zend Framework,Testing,Phpunit,有人成功设置Zend_测试吗?您的方法/途径是什么?您如何运行测试/测试套件 我已经安装了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...
}
我总是使用一个简单的TestHelper.php来完成一些基本的初始化工作。该文件包含在每个测试用例文件中。我做的一件事是注册Zend Framework autoloader,因为我遇到了主要的依赖性问题,尤其是在使用过滤器、验证器和表单时。几乎不可能跟踪所有必需的文件并将其手动包含在测试用例中
您也许可以将自动加载初始化和包含路径的设置移动到您的引导插件中,因为此过程对于实际应用程序应该是相同的。以下是我为使其工作所做的:
首先,我们需要解决自动加载问题。我们将创建一个所有测试都将包含的文件,并将其放在tests目录中。注意:我几乎是从my/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();
就这样!如果我的引导文件已经是一个插件,这可能会有点复杂,但因为不是,所以非常简单。而不是使用
require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();
换成
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
registerAutoLoad()从1.8.0开始就不推荐使用
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();