我正在学习单元测试,我试图解决以下问题:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for zfcUserAuthentication
Run Code Online (Sandbox Code Playgroud)
...使用给出的唯一答案:
所以我的setUp函数看起来一样.不幸的是,我收到错误消息:
Zend\Mvc\Exception\InvalidPluginException: Plugin of type Mock_ZfcUserAuthentication_868bf824 is invalid; must implement Zend\Mvc\Controller\Plugin\PluginInterface
Run Code Online (Sandbox Code Playgroud)
它是由代码的这一部分引起的(在我的代码中以相同的方式分开):
$this -> controller->getPluginManager()
->setService('zfcUserAuthentication', $authMock); // Error refers to this line.
Run Code Online (Sandbox Code Playgroud)
$ authMock对象显然没有实现plugininterface,我需要实现它来传递到setService.
$ authMock是不是要通过那里,因为它用于单元测试?我应该使用不同的(面向单元测试的)setService方法吗?
我需要一种方法来处理登录到我的应用程序,或者我的单元测试毫无意义.
谢谢你的建议.
===编辑(11/02/2013)===
我想专注于这一部分以澄清,因为我认为这是问题领域:
// Getting mock of authentication object, which is used as a plugin.
$authMock = $this->getMock('ZfcUser\Controller\Plugin\ZfcUserAuthentication');
// Some expectations of the authentication service.
$authMock -> expects($this->any())
-> method('hasIdentity')
-> will($this->returnValue(true));
$authMock -> expects($this->any())
-> method('getIdentity')
-> …Run Code Online (Sandbox Code Playgroud) 我ZFCUser成功安装了.现在我想知道是否有办法全局检查身份验证.
如维基中所述,有几种方法可以检查身份验证.它们都有效,但是我必须在每一个动作中都使用check-if-clause 吗?我的所有网站都只能在登录时访问,如果没有,您应该被重新路由到登录页面.
有谁知道我是否可以把这个逻辑放在一个中心位置?
我在尝试对使用ZfcUser进行身份验证的操作进行单元测试时遇到问题.我需要一些方法来模拟ZfcUser Controller插件,但我不太确定如何做到这一点.我已成功地为表格和模型生成了一些单元测试,但是控制器需要大量注入的对象并导致问题.有谁知道如何设置ZfcUser模拟器来成功测试控制器?
这是我的测试(从ZF2教程复制):
<?php
namespace SmsTest\Controller;
use SmsTest\Bootstrap;
use Sms\Controller\SmsController;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;
use PHPUnit_Framework_TestCase;
class SmsControllerTest extends PHPUnit_Framework_TestCase
{
protected $controller;
protected $request;
protected $response;
protected $routeMatch;
protected $event;
protected function setUp()
{
$serviceManager = Bootstrap::getServiceManager();
$this->controller = new SmsController();
$this->request = new Request();
$this->routeMatch = new RouteMatch(array('controller' => 'index'));
$this->event = new MvcEvent();
$config = $serviceManager->get('Config');
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);
$this->event->setRouter($router);
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event); …Run Code Online (Sandbox Code Playgroud) 我在我的Zend Framework 2应用程序中添加了模块ZfcUser.但是我必须使用现有的数据库表,它的列名与ZfcUser的默认表结构略有不同.
在ZfcUser wiki页面中,它表示如果我的模型不符合提供的界面,则可以使用自定义映射器.由于我的数据库表不同于默认值,因此我的用户实体类也与标准ZfcUser\Entity\User不同.但是我可以通过覆盖文件config/autoload/zfcuser.global.php中的设置来告诉ZfcUser轻松使用我自己的类:
'user_entity_class' => 'MyApp\Entity\MyUser',
Run Code Online (Sandbox Code Playgroud)
但到目前为止,我还没有找到告诉ZfcUser使用我的mapper类的简单方法.
我只发现映射器是由ZfcUser\Module :: getServiceConfig()创建的,在其中,我可以看到映射器从其工厂函数返回:
// ...
public function getServiceConfig()
{
return array(
// ...
'factories' => array(
// ...
'zfcuser_user_mapper' => function ($sm) {
$options = $sm->get('zfcuser_module_options');
$mapper = new Mapper\User();
$mapper->setDbAdapter($sm->get('zfcuser_zend_db_adapter'));
$entityClass = $options->getUserEntityClass();
$mapper->setEntityPrototype(new $entityClass);
$mapper->setHydrator(new Mapper\UserHydrator());
return $mapper;
},
// ...
Run Code Online (Sandbox Code Playgroud)
有没有办法让ZfcUser使用我的自定义用户映射器类?
我是在阿根廷写的,原谅我的英语很少.我有一些问题与模块ZfcUser和zfcuserDoctrineORM.我需要将它们集成到我的项目中.我正在使用Zend framework 2,doctrine 2.3和postgreSQL,这是我第一次使用这些工具.出于这个原因,有很多事情我不能很好地支配,我有我所有的模块/config/application.config.php,我的连接是在我的数据库中配置的/config/autoload/local.php
Local.php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' =>array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOPgSql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '5432',
'user' => 'postgres',
'password' => 'postgres',
'dbname' => 'ministerio',
)
)
)
),
);
application.config.php
return array(
'modules' => array(
'Application',
'DoctrineModule',
'DoctrineORMModule',
'Reeser', // Name of my module
'ZfcBase',
'ZfcUser',
'ZfcUserDoctrineORM',
),
'module_listener_options' =>array(
'config_glob_paths' =>array(
'config/autoload/{,*.}{global,local}.php',
),
'module_paths' =>array(
'./module', … 我想要做的是为注册用户添加更多属性(字段)以填写例如:我们有一个默认提供的显示名称,我希望有一些更像,出生地,出生日期等等..
我创建了自己的实体类,并使ZfcUser使用它(更具体地说:我添加了一些受保护的变量,getter和setter方法).
我的问题是,我真的不知道接下来该做什么.我知道我应该显然添加一些元素到表单,我可以做到但我怎么能"知道"zfcuser我在实体类中有更多的变量,所以它可以很好地使用数据库.
顺便说一句.我无法理解这个模块是如何工作的,例如它运行与数据库一起使用的脚本
我正在使用Zend Framework 2和ZfcUser,BjyAuthorize和Doctrine作为数据库.到目前为止,注册等工作非常顺利.我的问题是,注册用户没有分配角色,所以我想在注册期间向用户添加角色"user".
我想我可以将它附加到"注册"事件,但我不知道该怎么做.
我希望有一个人可以帮助我 ...
(我使用本教程设置zfcuser等http://samminds.com/2013/03/zfcuser-bjyauthorize-and-doctrine-working-together/)
public function onBootstrap(MvcEvent $e)
{
$zfcServiceEvents = $e->getApplication()->getServiceManager()->get('zfcuser_user_service')->getEventManager();
$zfcServiceEvents->attach('register', function($e) {
$user = $e->getParam('user');
// probably the role must be added here, with $user->addRole();
// but how do i get the user Role Entity to add from DB?
});
Run Code Online (Sandbox Code Playgroud) 我想在Zend Framework 2中使用ZfcUser任意生成用户帐户,然后我可以通过激活链接或一些临时凭证向用户发送电子邮件.我不确定ZfcUser是否可以扩展到安全地执行此操作,因此我正在寻求帮助和建议.
我需要在我的推荐信函申请中这样做.它适用于现有用户(使用ZfcUser以"正常"方式注册的人)通过为该人提供电子邮件地址来请求某人的推荐信.此时我想使用给定电子邮件的ZfcUser生成一个帐户,并通知该人已经请求他们的推荐信,并且他们应该登录并完成它.
我为每个字母请求提供了一些一次性链接,但我觉得为每个电子邮件和相关用户创建一个帐户更有意义,以便收到多个信件请求的人可以登录并成为在一个地方提出他们的所有要求.我也认为这可能是明智的,在我的数据库,这些种类的用户帐户的分隔成不同的用户表,或者至少一个标志字段添加到我的当前用户的表模式.
我应该注意到,我目前也在使用CdliTwoStageSignup和ZfcUser.我也不一定需要将它用于此,但它在工具包中.
我认为ZfcUser'州'旗帜在这里可能很有用,但它并不是我想要完成的艰难部分.
我还在Github上的ZfcUser wiki中找到了另一条线索,告诉我如何做到这一点.
我正在寻找使用ZfcUser来完成此项工作的技术帮助,但我肯定对一般的设计理念或改进持开放态度.
我想将一些页面重定向到登录页面而不是页面403.
默认情况下,BjyAuthorize将所有内容重定向到403页面.是否可以配置此行为?
我发现了这个:RedirectionStrategy.我该如何使用它?
zfcuser ×10
php ×4
bjyauthorize ×2
phpunit ×2
annotations ×1
config ×1
doctrine ×1
doctrine-orm ×1
module ×1
unit-testing ×1