1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
<?php
namespace Jasny\Authz;
use Jasny\Authz;
use Jasny\Authz\User;
use PHPUnit_Framework_TestCase as TestCase;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
use Jasny\TestHelper;
/**
* @covers Jasny\Authz\ByGroup
*/
class ByGroupTest extends TestCase
{
use TestHelper;
/**
* @var Authz\ByGroup|MockObject
*/
protected $auth;
public function setUp()
{
$this->auth = $this->getMockForTrait(Authz\ByGroup::class);
$this->auth->method('getGroupStructure')->willReturn([
'user' => [],
'client' => ['user'],
'mod' => ['user'],
'dev' => ['user'],
'admin' => ['mod', 'dev']
]);
}
public function testGetRoles()
{
$this->assertEquals(['user', 'client', 'mod', 'dev', 'admin'], $this->auth->getRoles());
}
/**
* @expectedException \UnexpectedValueException
*/
public function testGetRolesWithInvalidStructure()
{
$this->auth = $this->getMockForTrait(Authz\ByGroup::class);
$this->auth->method('getGroupStructure')->willReturn('foo bar');
$this->auth->getRoles();
}
public function testIsWithoutUser()
{
$this->assertFalse($this->auth->is('user'));
}
public function roleProvider()
{
return [
['user', ['user' => true, 'client' => false, 'mod' => false, 'dev' => false, 'admin' => false]],
['client', ['user' => true, 'client' => true, 'mod' => false, 'dev' => false, 'admin' => false]],
['admin', ['user' => true, 'client' => false, 'mod' => true, 'dev' => true, 'admin' => true]],
[['mod', 'client'], ['user' => true, 'client' => true, 'mod' => true, 'dev' => false, 'admin' => false]],
[['user', 'foo'], ['user' => true, 'client' => false, 'mod' => false, 'dev' => false, 'admin' => false]],
];
}
/**
* @dataProvider roleProvider
*
* @param string|array $role
* @param array $expect
*/
public function testIsWithUser($role, array $expect)
{
$user = $this->createMock(User::class);
$user->method('getRole')->willReturn($role);
$this->auth->method('user')->willReturn($user);
$this->assertSame($expect['user'], $this->auth->is('user'));
$this->assertSame($expect['client'], $this->auth->is('client'));
$this->assertSame($expect['mod'], $this->auth->is('mod'));
$this->assertSame($expect['dev'], $this->auth->is('dev'));
$this->assertSame($expect['admin'], $this->auth->is('admin'));
}
public function testIsWithUnknownRole()
{
$this->assertFalse(@$this->auth->is('foo'));
$this->assertLastError(E_USER_NOTICE, "Unknown role 'foo'");
}
}
|