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

运行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,
            )
        )
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?