我是ZF2的新手,我在测试工厂类时遇到了问题。
这是我的contollers.config.php
<?php
return array(
'invokables' => array(
'Application\Controller\Index' => 'Application\Controller\IndexController',
'Application\Controller\SystemStatus' => 'Application\Controller\SystemStatusController',
),
'factories' => array(
'Application\Controller\HomePage' => 'Application\Controller\Factory\HomePageControllerFactory',
),
);
我的工厂类路径是:
/app/src/module/Application/src/Application/Controller/Factory/HomePageControllerFactory.php
文件看起来像:
<?php
namespace Application\Controller\Factory;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Application\Controller\HomePageController;
class HomePageControllerFactory implements FactoryInterface {
/**
* @param ServiceLocatorInterface $sm
* @return HomePageController|mixed
*/
public function createService(ServiceLocatorInterface $sm)
{
$queryParams = $sm->getServiceLocator()->get('path\path');
$service = $sm->getServiceLocator()->get('path\path');
return new HomePageController($queryParams, $service);
}
}
我的测试之路是: /app/tests/ApplicationTest/Integration/Controller/Factory/HomePageControllerFactoryTest.php
我唯一能做的就是:
<?php
namespace ApplicationTest\Integration\Controller\Factory;
use Application\Controller\Factory\HomePageControllerFactory;
use PHPUnit_Framework_TestCase as TestCase;
class HomePageControllerFactoryTest extends TestCase
{
/**
* @var HomePageControllerFactory
*/
private $underTest;
public function setUp()
{
$this->underTest = new HomePageControllerFactory();
}
public function testCreateService()
{
/*Nothing*/
}
}
当我运行测试时,这就是输出
PHP Fatal error: Class 'Application\Controller\Factory\HomePageControllerFactory' not found in /apps/tests/ApplicationTest/Integration/Controller/Factory/HomePageControllerFactoryTest.php on line 17
Bootstrap.php位于/app/tests/Bootstrap.php
含量:
<?php
namespace ApplicationTest;
use Zend\Loader\AutoloaderFactory
, Zend\Mvc\Service\ServiceManagerConfig
, Zend\ServiceManager\ServiceManager;
date_default_timezone_set('America/New_York');
error_reporting(E_ALL | E_STRICT);
chdir(dirname(__DIR__));
require 'vendor/autoload.php';
class Bootstrap
{
private static $instance;
private $serviceManager;
public static function getInstance()
{
if (!isset(static::$instance)) {
$class = get_class();
static::$instance = new $class;
}
return static::$instance;
}
public static function init()
{
AutoloaderFactory::factory();
static::getInstance();
}
public function getServiceManager()
{
if (!isset($this->serviceManager)) {
$this->serviceManager = new ServiceManager(new ServiceManagerConfig());
}
return $this->serviceManager;
}
public function setServiceManager(ServiceManager $sm)
{
$this->serviceManager = $sm;
}
public function getConfig()
{
if (is_readable(__DIR__ . '/TestConfig.php')) {
return require __DIR__ . '/TestConfig.php';
} else {
return require './src/config/application.config.php';
}
}
private function __construct()
{
}
}
Bootstrap::init();
我坚持这个,我不知道为什么我不能实例化HomePageControllerFactory的对象
答案 0 :(得分:2)
如果你正在使用composer,那么你需要在composer.json的autoload部分中包含Application类的路径:
...
"autoload": {
"psr-0": {
"Application": "src/module/Application/src/"
}
}
...
然后重新运行composer以再次生成自动加载文件:
$ php composer.phar dump-autoload