summaryrefslogtreecommitdiffstats
path: root/tests/Authz/ByLevelTest.php
blob: c85464e8e776ad29b65c113c2a086b0c7c7e2d09 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?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\ByLevel
 */
class ByLevelTest extends TestCase
{
    use TestHelper;
    
    /**
     * @var Authz\ByLevel|MockObject
     */
    protected $auth;
    
    public function setUp()
    {
        $this->auth = $this->getMockForTrait(Authz\ByLevel::class);
        
        $this->auth->method('getAccessLevels')->willReturn([
           'user' => 1,
           'mod' => 10,
           'admin' => 100
        ]);
    }
    
    public function testGetLevelWithRole()
    {
        $this->assertSame(1, $this->auth->getLevel('user'));
        $this->assertSame(10, $this->auth->getLevel('mod'));
        $this->assertSame(100, $this->auth->getLevel('admin'));
    }
    
    /**
     * @expectedException \DomainException
     */
    public function testGetLevelWithUnknownRole()
    {
        $this->assertSame(1, $this->auth->getLevel('foo'));
    }
    
    /**
     * @expectedException \InvalidArgumentException
     */
    public function testGetLevelWithInvalidValue()
    {
        $this->assertSame(1, $this->auth->getLevel(['foo']));
    }
    
    public function testGetLevelWithLevel()
    {
        $this->assertSame(100, $this->auth->getLevel(100));
        $this->assertSame(100, $this->auth->getLevel('100'));
    }
    
    
    public function testGetRoles()
    {
        $this->assertEquals(['user', 'mod', 'admin'], $this->auth->getRoles());
    }
    
    /**
     * @expectedException \UnexpectedValueException
     */
    public function testGetRolesWithInvalidStructure()
    {
        $this->auth = $this->getMockForTrait(Authz\ByLevel::class);
        $this->auth->method('getAccessLevels')->willReturn('foo bar');
        
        $this->auth->getRoles();
    }
    
    
    public function testIsWithoutUser()
    {
        $this->assertFalse($this->auth->is('user'));
    }
    
    public function roleProvider()
    {
        return [
            ['user', ['user' => true, 'mod' => false, 'admin' => false]],
            ['mod', ['user' => true, 'mod' => true, 'admin' => false]],
            ['admin', ['user' => true, 'mod' => true, 'admin' => true]],
            [50, ['user' => true, 'mod' => true, 'admin' => false]],
            [500, ['user' => true, 'mod' => true, 'admin' => true]]
        ];
    }
    
    /**
     * @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['mod'], $this->auth->is('mod'));
        $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'");
    }
    
    public function testIsWithUnknownUserRole()
    {
        $user = $this->createMock(User::class);
        $user->method('getRole')->willReturn('foo');
        
        $this->auth->method('user')->willReturn($user);
        
        $this->assertFalse(@$this->auth->is('user'));
        $this->assertLastError(E_USER_NOTICE, "Unknown user role 'foo'");
    }
    
    public function testIsWithInvalidRole()
    {
        $user = $this->createMock(User::class);
        $user->method('getRole')->willReturn(['user']);
        
        $this->auth->method('user')->willReturn($user);
        
        $this->assertFalse(@$this->auth->is('user'));
        $this->assertLastError(E_USER_WARNING, "Expected role to be a string, not a array");
    }
}