doushaiyu5065 2013-12-05 22:44
浏览 777
已采纳

运行PHPUnit测试会产生“未找到类”错误

This is a question on PHP Unit Test giving me a Class Not Found Error.

Background

I am using Zend Studio 10.5 to develop a Zend Framework 2 application. I have a few modules loaded, including ZfcUser and BjyAuthorize

FedcoUser is the module that has a Controller Guard that uses guard rules of BjyAuthorize:

<?php
namespace FedcoUser\Controller;

use Zend\View\Model\ViewModel;
use ZfcUser\Controller\UserController as ZfcUserController;

class MachinistController extends ZfcUserController
{
    public function indexAction()
    {
        if (!$this->zfcUserAuthentication()->hasIdentity()) {
            return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
        }

        return new ViewModel();
    }
}

Using Zend Studio 10.5 native menus, I have created a test case for the controller above. When I run it I get the following error:

Debug Error: FedcoUser/Controller/MachinistController.php line 8 - Class 'ZfcUser\Controller\UserController' not found

My assumption is that somehow, autoloader for PHPUnit cannot figure out where the class is being placed, because the class it cannot find is located at

vendor/zf-commons/zfc-user/src/ZfcUser/Controller/UserController.php

I am not yet familiar with how PHPUnit works to be able to fix this error. Does PHPUnit even have an autoloader? There are files called bootstrap.php, phpunit.xml, and TestConfiguration.php in my module's /test/ folder, if that helps. I tried to randomly add various modules and paths to TestConfiguration.php file as below but it did not help:

 $additionalModulePaths = array(
     'ZfcUser' => realpath('vendor/zf-commons/zfc-user/src/ZfcUser/Controller/UserController.php'),
);

Can you help?

  • 写回答

1条回答 默认 最新

  • duancan7914 2013-12-12 19:43
    关注

    I ended up loading additional namespaces into my config inside my TestConfiguration.php file:

    $additionalNamespaces = array(
        'ZfcUser\Controller' => __DIR__ . '/../../../vendor/zf-commons/zfc-user/src/ZfcUser/Controller/'
    );
    

    that fit in nicely into my bootstrap.php, which had the following code:

    // setup autoloader
    AutoloaderFactory::factory(
        array(
            'Zend\Loader\StandardAutoloader' => array(
                StandardAutoloader::AUTOREGISTER_ZF => true,
                StandardAutoloader::ACT_AS_FALLBACK => false,
                StandardAutoloader::LOAD_NS => $additionalNamespaces,
            )
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的