有没有人有任何的成功建立Zend_Test?什么是你的方法/步骤,你如何运行测试/测试套件?

我已经PHPUnit的安装和工作。现在,我尝试写一些简单的控制器的测试。 Zend框架文档假定是自动加载设置,这是我没有做过。用什么方法做的使用自动加载相应的文件?我在正常的引导文件这样做,但我不想弄乱我的测试与一群包括和建立路径。将一个抽象控制器测试用例类是去的方式?

什么样的文档引导插件使用......是你如何引导你的测试,或做你喜欢做它用不同的方式?我想重新使用尽可能多的普通引导文件的,我可以。我应该怎么干我的测试和正常使用引导程序?

下面是我的测试,到目前为止:

<?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...
}

我应该怎么办?

有帮助吗?

解决方案

下面是我做了什么来得到它的工作:

首先,我们需要解决的问题自动加载。我们将创建一个文件,所有的测试将包括做到这一点,并把它放在测试目录。注:我几乎复制所有这一切都从我/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();

其次,我们需要在我们的测试文件。我们的测试文件在/测试/应用/控制器/。我不打算用我的引导作为一个插件,因为我的引导文件的工作方式作为的快速入门教程。我只是通过设置位置为公众$引导链接到它。当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,做一些基本的初始化的东西。此文件包含在每个测试用例文件。其中一个我做的事情,是注册Zend框架自动加载磁带机,因为我使用的过滤器,验证和形式时,经历了重大的依赖性问题尤为明显。这几乎是不可能在测试的情况下手动将所有必需的文件的跟踪和包括他们。

您或许可以移动自动加载初始化和include路径到您的引导插件作为这个过程的设置应该是真正的应用程序相同的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top