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

运行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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀