简体   繁体   English

使用ZendFramework设置控制器测试:自动加载器问题

[英]Setting up a Controller Test with ZendFramework: Autoloader issue

I'm working on a web application with Zend Framework v1.11.0, but I'm failing to set up a correct test environment for controller tests. 我正在使用Zend Framework v1.11.0开发Web应用程序,但无法为控制器测试设置正确的测试环境。 I know that there are already a lot of questions about setting up Zend test, however, after hours of research, none of these fixed my problem. 我知道关于设置Zend测试已经存在很多问题,但是,经过数小时的研究,这些都没有解决我的问题。 So here is my code: 所以这是我的代码:

Test Bootstrap: 测试引导程序:

<?php
error_reporting(E_ALL | E_STRICT);

defined('APPLICATION_PATH') || define('APPLICATION_PATH',realpath(dirname(__FILE__).'/../../application'));
define('APPLICATION_ENV', 'testing');
set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../library'), get_include_path())));

require_once ('Zend/Application.php');
require_once ('ControllerTestCase.php');

ControllerTestCase: ControllerTestCase:

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {

  public $application;

  public function setUp() {
    $this->bootstrap = array($this, 'appBootstrap');
    parent::setUp();
  }

  public function appBootstrap() {
    $this->application = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH.'/configs/application.ini');
    $this->application->bootstrap();
  }
}

Simple Test: 简单测试:

class IndexControllerTest extends ControllerTestCase {
    public function testDefaultShouldInvokeIndexAction()
    {
        $this->dispatch('/');
        $this->assertModule('default');
        $this->assertController('index');
        $this->assertAction('index');
    }
}

Everytime I run this test, PHPUnit throws a fatal error message not finding a class in my library that is used in my IndexController. 每次运行此测试时,PHPUnit都会抛出一条致命错误消息,即在我的库中找不到用于IndexController的类。 Therefore the reason must be something related to the Autoloader, I thought. 因此,我认为原因一定与自动装带器有关。 After debugging I found out, that the test runs through the regular Bootstrap, trough the Auth plugin and so on and that my libraries are included. 调试后,我发现,在测试通过正常运行的引导,线槽验证插件等等,而我的图书馆包括在内。 So I have no idea what PHPUnit's problem might be. 所以我不知道什么是PHPUnit的问题。 In browser everything works fine and non-controller tests in the commandline (eg global settings) work, too. 在浏览器中,一切正常,并且在命令行中进行非控制器测试(例如,全局设置)也可以。

I'd be very grateful for any hint what i need to do to get my controller tests working! 我很感谢任何提示,我需要做些什么才能使我的控制器测试正常工作!

edit: my directory structure looks like this (I separated my classes into different libraries for certain reasons) 编辑:我的目录结构如下所示(出于某些原因,我将类分为不同的库)

project
 - library1
 - library2
 - project_name
    ->application
    ->library3
    ->library4
    ->tests
       ->>application
          ->>>ControllerTestCase.php
          ->>>Bootstrap.php
       ->>Controller
          ->>>IndexControllerTest.php

my include paths look actually like: 我的包含路径实际上看起来像:

set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../library3'), get_include_path())));
set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../library4'), get_include_path())));
set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../../library1'), get_include_path())));
set_include_path(implode(PATH_SEPARATOR, array(realpath(APPLICATION_PATH.'/../../library2'), get_include_path())));

In my own ControllerTestCase class I set the bootstrap object in the constructor instead of the setUp function. 在我自己的ControllerTestCase类中,我在构造函数中而不是setUp函数中设置了引导对象。 So for you that would mean to change this 所以对你来说这意味着改变

public function setUp() {
    $this->bootstrap = array($this, 'appBootstrap');
    parent::setUp();
}

into 进入

public function __construct()
{
    $this->bootstrap = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini'
    );
}

and removing the appBootstrap method. 并删除appBootstrap方法。 That's really the only difference I see between your code and my own code. 这确实是我看到的您的代码和我自己的代码之间的唯一区别。 I that does not fix the problem, it's likely your path settings are off. 我无法解决问题,可能是您的路径设置已关闭。

The easiest way to have properly set up test case for Zend Controller is to use the zf tool. 为Zend Controller正确设置测试用例的最简单方法是使用zf工具。 By creating your controllers with it, you get automatically generated test cases for them. 通过使用它创建控制器,您将为它们自动生成测试用例。

zf create controller name index-action-included[=1] module

It creates the following bootstrap: 它创建以下引导程序:

    defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

require_once 'Zend/Loader/Autoloader.php';

...and the controllerTestCase: ...以及controllerTestCase:

class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{

    public function setUp()
    {
        $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
        parent::setUp();
    }

    ....tests.........
}

You have to add the include paths in the bootstrap and register any custom test autoloaders with spl_autoloader. 您必须在引导程序中添加include路径,并使用spl_autoloader注册任何自定义测试自动加载器。 Also, remember to start phpunit with -c test/phpunit.xml 另外,请记住使用-c test/phpunit.xml启动phpunit。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM