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

运行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 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?