Zend框架:使用Zend_Test入门

4
有人成功设置了Zend_Test吗?您使用的方法/方法是什么,以及如何运行测试/测试套件?
我已经安装并使用PHPUnit。现在我正在尝试编写一些简单的控制器测试。Zend Framework文档假定已设置自动加载,但我还没有这样做。那么您用来自动加载正确文件的方法是什么?我通常会在普通的引导文件中这样做,但我不想在我的测试中混杂很多包含和设置路径。一个抽象控制器测试用例类是否是最好的选择?
那关于像文档使用的引导插件呢...您是这样启动测试的,还是您喜欢以不同的方式执行?我想尽可能重用常规引导文件中的内容。我应该如何DRY up我的引导文件,以供测试和正常使用?
以下是我的测试:
<?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...
}

我该怎么办?

3个回答

3

以下是我所做的,使它能够正常工作:

首先,我们需要解决自动加载问题。我们将通过创建一个文件来解决这个问题,所有测试都将包含在其中,并将其放置在测试目录中。注意:我基本上是从我的 /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/ 目录下。我不会将我的引导程序用作插件,因为我的引导程序文件与 QuickStart 教程 的工作方式相同。我只需通过将位置设置为 public $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');
    }

}

就是这样!如果我的Bootstrap文件已经是一个插件,那么这可能会更加复杂,但由于它不是,所以非常容易。


1

不要使用

require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();

将其转换为

require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

registerAutoLoad()自1.8.0版起已过时


0

我经常使用一个简单的TestHelper.php文件来进行一些基本的初始化工作。这个文件被包含在每个测试用例文件中。其中一件事情是注册Zend Framework自动加载器,因为我在使用过滤器、验证器和表单时遇到了很多依赖问题。手动跟踪所有所需的文件并在测试用例中手动包含它们几乎是不可能的。

你可以将自动加载初始化和包含路径的设置移动到你的引导插件中,因为这个过程对于真实应用程序应该是相同的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接