phpunit - ZF2 - ServiceManager 和 getServiceConfig 问题 - 无法获取或创建实例

标签 phpunit zend-framework2

我在尝试使用 ZF2 的身份验证服务时遇到了一些问题。我必须遵循 Module.php getServiceConfig 函数:

<?php

public function getServiceConfig()
{
    return array(
        'factories' => array(
            'Auth\Model\CustomerTable' =>  function($sm) {
                $tableGateway = $sm->get('CustomerTableGateway');
                $table = new CustomerTable($tableGateway);
                return $table;
            },
            'CustomerTableGateway' => function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new Customer()); // prototype pattern implemented.
                return new TableGateway('customer', $dbAdapter, null, $resultSetPrototype);
            },
            'Auth\Model\AuthStorage' => function($sm){
                return new \Auth\Model\AuthStorage('jamietech');  
            },
            'AuthService' => function($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $dbTableAuthAdapter  = new DbTableAuthAdapter($dbAdapter, 
                                          'customer','username','password');

                $authService = new AuthenticationService();
                $authService->setAdapter($dbTableAuthAdapter);
                $authService->setStorage($sm->get('Auth\Model\AuthStorage'));

                return $authService;
            },
        ),
    );
}

AuthStorage 工厂只是为我们创建了一个新的 AuthStorage 来跟踪我拥有的 rememberMe 功能,而 AuthService 工厂为我们创建了一个新的 Authentication Service。我看不到我做错了什么,但是在 AuthController.php 中运行以下代码时:

<?php

public function loginAction()
{
    //if already login, redirect to success page 

    if ($this->getAuthService()->hasIdentity()){
        return $this->redirect()->toRoute('success');
    }

    $form = new LoginForm();
    return array(
        'form'     => $form,
        'messages' => $this->flashmessenger()->getMessages()
    );
}

public function logoutAction()
{
    $this->getSessionStorage()->forgetMe();
    $this->getAuthService()->clearIdentity();
    $this->flashmessenger()->addMessage("You have logged out successfully.");

    return $this->redirect()->toRoute('auth', array('action'=>'login'));
}

PHPUnit 在运行 PHPUnit 命令时遇到以下错误:

1: "testLoginActionCanBeAccessed - Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance of Zend\Db\Adapter\Adapter

1: "testLogoutActionCanBeAccessed - session_regenerate_id(): cannot regenerate session id - headers already sent.

当运行 -process-isolation 命令时,登录和注销都会出现此错误:

"Serialization of closure is not allowed in: C;\Users\-----\AppData\Local\Temp\-----

如果有人能提供帮助那就太好了。我是 ZF 菜鸟,所以尽量不要太苛刻。

编辑:顺便说一句,global.php 文件包含 ZF2 教程应用程序中说明的 service_manager 适配器工厂。

谢谢! 杰米麦克劳伦

最佳答案

你检查过这些吗:

  • autoload_classmap.php(用于您的模块)
  • 在你的module.config.php

像这样

service_manager' => array(
 'aliases' => array(    
  'mymodule-ZendDbAdapter' => 'Zend\Db\Adapter\Adapter',
  ),
);

希望能帮到你

关于phpunit - ZF2 - ServiceManager 和 getServiceConfig 问题 - 无法获取或创建实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15815542/

相关文章:

php - 如何将选项/参数传递给 ZendFramework 2 中的 formCollection 字段集?

ubuntu - 无法在 Ubuntu 10.04 中安装最新的 phpUnit

php - Laravel - phpunit 数组断言

eclipse - 测试套件的 phpunit eclipse 配置。文档?

php - Zend Framework 2 服务管理器

php - Zend 2 Ajax JSON 多数据

php - 使用 vfsStream 测试 move_uploaded_file 和 is_uploaded_file

phpunit - PHP fatal error :未捕获错误:调用未定义的方法 PHPUnit\Util\Configuration::getExtensionConfiguration()

php - Zend 框架 2 : multiple routes to one action

php - Zend Framework 2 + Doctrine 2 框架应用程序