Laravel 5.2 - Gate Auth问题:isset或空的非法偏移类型

时间:2016-03-22 18:26:48

标签: laravel laravel-5 phpunit

跑步:Laravel 5.2     问题:门     错误:isset中的非法偏移类型或空

这是我的引导方法中的AuthServiceProvider

    foreach ($this->getPermissions() as $permission) {
        $gate->define($permission->code, function (User $user) use ($permission) {
            if ($user->isAdmin())
                return true;
            return $user->hasPermission($permission);
        });
    }

我的权限和角色被播种到数据库......

在我的User课程中,有以下方法(上面使用过)

/**
 * Determine if the user has the given role.
 *
 * @param  mixed $role
 * @return boolean
 */
public function hasRole($role)
{
    if (is_string($role))
        return $this->roles->contains('code', $role);

    if ($role instanceof Role)
        return $this->roles()->where('id', $role->id)->count();

    if ($role instanceof Collection)
    {
        foreach ($role as $r)
            if ( ! $this->hasRole($r) )
                return false;
        return true;
    }
}

/**
 * Determine if the user may perform the given permission.
 *
 * @param  Permission $permission
 * @return boolean
 */
public function hasPermission(Permission $permission)
{
    return $this->hasRole($permission->roles);
}

现在,当我查看我的网站时,没有错。 当我运行测试时,为了确保普通用户拥有0权限,我收到错误..

我的测试课程:

/** @test */
public function a_user_with_no_role_can_do_nothing()
{
    $user = factory(User::class)->create();
    $permissions = Permission::all();

    foreach ($permissions as $perm)
        $this->assertFalse($user->can($perm));    // line 43
}

这会产生以下错误:

There was 1 error:

1) UsersTest::a_user_with_no_role_can_do_nothing
ErrorException: Illegal offset type in isset or empty

C:\xampp\htdocs\dev\itcdash\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php:321
C:\xampp\htdocs\dev\itcdash\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php:265
C:\xampp\htdocs\dev\itcdash\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php:243
C:\xampp\htdocs\dev\itcdash\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php:199
C:\xampp\htdocs\dev\itcdash\vendor\laravel\framework\src\Illuminate\Foundation\Auth\Access\Authorizable.php:18
C:\xampp\htdocs\dev\itcdash\tests\acl\UsersTest.php:43
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

为什么我收到此错误?

谢谢!

0 个答案:

没有答案