【发布时间】:2016-08-12 07:02:33
【问题描述】:
在我的 Laravel 项目中,我使用 Ldap-connector package 针对 LDAP 对用户进行身份验证
Auth 开箱即用。
在 /app/providers/AuthServiceProvider.php 我定义了一个策略来管理 LDAP 用户访问,例如:
public function boot(GateContract $gate)
{
$this->registerPolicies($gate);
$gate->define('rewards', function ($user) {
return ($user->getAdLDAP()->inGroup('Admin') || $user->getAdLDAP()->inGroup('Marketing')) ? true : false;
});
// other policies
// ...
}
在控制器中,我检查登录用户的策略,例如:
class RewardController extends Controller {
public function __construct($handler = null) {
$this->authorize('rewards');
}
// other methods
// ...
}
一切正常,如果登录用户没有Marketing 或Admin 组,控制器将抛出403 异常。
现在,对于我的 phpunit 测试,我需要模拟 LDAP 身份验证并提供对控制器的访问权限以测试其方法,否则策略会抛出 This action is unauthorized. 错误
由于实现了 ldap 身份验证驱动程序,我不认为用户模型 App/User 被使用,我不能只使用来自 here 的 Laravel 文档中的 $this->actingAs($user) 来完成它
【问题讨论】:
标签: unit-testing laravel mocking phpunit laravel-5.2