【问题标题】:ZF2 - ServiceManager conflict with unit testing in Module.phpZF2 - ServiceManager 与 Module.php 中的单元测试冲突
【发布时间】:2014-08-10 13:03:25
【问题描述】:

在 Module.php 中,我实现了在允许访问受限页面之前检查用户身份验证的代码。

这是我的 Module.php

<?php

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;

class Module
{
    protected $whitelist = array('authenticate', 'home');

    public function onBootstrap(MvcEvent $event)
    {
        $application = $event->getApplication();
        $eventManager = $application->getEventManager();
        $serviceManager = $application->getServiceManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        $authService = $serviceManager->get('Zend\Authentication\AuthenticationService');
        $whitelist = $this->whitelist;
        $eventManager->attach(MvcEvent::EVENT_ROUTE, function ($e) use ($whitelist, $authService) {
            $routeMatch = $e->getRouteMatch();
            //No route match, this is a 404
            if (!$routeMatch instanceof RouteMatch) {
                return;
            }
            //Route is whitelisted
            $matchedRouteName = $routeMatch->getMatchedRouteName();
            if (in_array($matchedRouteName, $whitelist)) {
                return;
            }
            //User is authenticated
            if ($authService->hasIdentity()) {
                return;
            }
            //Redirect users
            $router = $e->getRouter();
            $url = $router->assemble(array(), array(
                'name' => 'authenticate'
            ));
            $response = $e->getResponse();
            $response->getHeaders()->addHeaderLine('Location', $url);
            $response->setStatusCode(302);
            return $response;
        }, -100);
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Zend\Authentication\AuthenticationService' => function ($serviceManager) {
                    return $serviceManager->get('doctrine.authenticationservice.orm_default');

                }
            )
        );
    }
}

这在浏览器中完美运行,但是在运行单元测试时会引发以下错误。

Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for doctrine.authenticationservice.orm_default

问题是onBoostrap中的服务管理器无法初始化认证适配器,这是有问题的代码

$authService = $serviceManager->get('Zend\Authentication\AuthenticationService');

当我禁用 $authService 所有单元测试成功运行时,我无法找出导致此问题的确切问题,这可能是什么问题?

谢谢。

【问题讨论】:

    标签: unit-testing doctrine-orm zend-framework2 phpunit


    【解决方案1】:

    因为在运行测试时,您必须配置实体管理器。

    使用 auth 模块,您有一个尚未在引导测试中配置的学说实体管理器。是吗 ?

    默认情况下,您应该有doctrine.entitymanager.orm_default,但并不是您的错误中有这个:doctrine.authenticationservice.orm_default

    我猜你必须在你的测试中添加一个实体管理器。

    【讨论】:

    • 感谢您的指点,问题是我没有加载 DoctrineOrmModule 进行测试。
    • @IbrahimAzharArmar 至少你怎么能在测试中 loa DoctrineOrmModule?
    猜你喜欢
    • 2015-04-16
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2013-01-25
    • 2014-06-22
    • 2014-05-18
    • 2018-01-21
    • 2016-12-09
    相关资源
    最近更新 更多