Failed Conditions
Push — master ( 1b92c1...5e8dd9 )
by Arnold
03:03
created

AuthTest::testLogout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 14
rs 9.4285
c 1
b 0
f 0
nc 1
cc 1
eloc 7
nop 0
1
<?php
2
3
namespace Jasny;
4
5
use Jasny\Auth;
6
use PHPUnit_Framework_TestCase as TestCase;
7
use PHPUnit_Framework_MockObject_MockObject as MockObject;
8
9
/**
10
 * @covers Jasny\Auth
11
 */
12
class AuthTest extends TestCase
13
{
14
    /**
15
     * @var Auth|MockObject 
16
     */
17
    protected $auth;
18
    
19
    public function setUp()
20
    {
21
        $this->auth = $this->getMockForAbstractClass(Auth::class);
22
    }
23
    
24
    
25
    public function testHashPassword()
26
    {
27
        $hash = $this->auth->hashPassword('abc');
28
        $this->assertTrue(password_verify('abc', $hash));
29
    }
30
    
31
    public function invalidPasswordProvider()
32
    {
33
        return [
34
            [''],
35
            [array()],
36
            [123]
37
        ];
38
    }
39
    
40
    /**
41
     * @dataProvider invalidPasswordProvider
42
     * @expectedException \InvalidArgumentException
43
     */
44
    public function testHashPasswordWithInvalidArgument($password)
45
    {
46
        $this->auth->hashPassword($password);
47
    }
48
    
49
    
50
    public function testVerifyCredentials()
51
    {
52
        $hash = password_hash('abc', PASSWORD_BCRYPT);
53
        
54
        $user = $this->createMock(Auth\User::class);
55
        $user->method('getHashedPassword')->willReturn($hash);
56
        
57
        $this->assertTrue($this->auth->verifyCredentials($user, 'abc'));
58
        
59
        $this->assertFalse($this->auth->verifyCredentials($user, 'god'));
60
        $this->assertFalse($this->auth->verifyCredentials($user, ''));
61
    }
62
    
63
    
64
    public function testUserWithoutSessionUser()
65
    {
66
        $user = $this->createMock(Auth\User::class);
67
        $user->expects($this->never())->method('onLogin');
68
        
69
        $this->auth->expects($this->once())->method('getCurrentUserId')->willReturn(123);
70
        $this->auth->expects($this->once())->method('fetchUserById')->with(123)->willReturn($user);
71
        
72
        $this->assertSame($user, $this->auth->user());
73
    }
74
    
75
    public function testUserWithSessionUser()
76
    {
77
        $this->assertNull($this->auth->user());
78
    }
79
    
80
    
81
    /**
82
     * @return Auth|MockObject
83
     */
84
    public function testSetUser()
85
    {
86
        $user = $this->createMock(Auth\User::class);
87
        
88
        $this->auth->expects($this->once())->method('persistCurrentUser');
89
        
90
        $result = $this->auth->setUser($user);
91
        
92
        $this->assertSame($user, $result);
93
        $this->assertSame($user, $this->auth->user());
94
        
95
        return $this->auth;
96
    }
97
    
98
    public function testSetUserWithExistingUser()
99
    {
100
        $this->auth->expects($this->exactly(2))->method('persistCurrentUser');
101
102
        $this->auth->setUser($this->createMock(Auth\User::class));
103
        
104
        $user = $this->createMock(Auth\User::class);
105
        $user->expects($this->once())->method('onLogin');
106
        
107
        $result = $this->auth->setUser($user);
108
        
109
        $this->assertSame($user, $result);
110
        $this->assertSame($user, $this->auth->user());
111
    }
112
    
113
    public function testSetUserWithOnLoginFail()
114
    {
115
        $this->auth->expects($this->once())->method('persistCurrentUser');
116
117
        $oldUser = $this->createMock(Auth\User::class);
118
        $this->auth->setUser($oldUser);
119
        
120
        $user = $this->createMock(Auth\User::class);
121
        $user->expects($this->once())->method('onLogin')->willReturn(false);
122
        
123
        $this->auth->expects($this->never())->method('persistCurrentUser');
124
        
125
        $result = $this->auth->setUser($user);
126
        
127
        $this->assertNull($result);
128
        $this->assertSame($oldUser, $this->auth->user());
129
    }
130
    
131
    
132
    public function testLogin()
133
    {
134
        $hash = password_hash('abc', PASSWORD_BCRYPT);
135
        
136
        $user = $this->createMock(Auth\User::class);
137
        $user->method('getHashedPassword')->willReturn($hash);
138
        $user->expects($this->once())->method('onLogin');
139
        
140
        $this->auth->expects($this->once())->method('fetchUserByUsername')->with('john')->willReturn($user);
141
        $this->auth->expects($this->once())->method('persistCurrentUser');
142
        
143
        $result = $this->auth->login('john', 'abc');
144
        
145
        $this->assertSame($user, $result);
146
        $this->assertSame($user, $this->auth->user());
147
    }
148
    
149
    public function testLoginWithIncorrectPassword()
150
    {
151
        $hash = password_hash('abc', PASSWORD_BCRYPT);
152
        
153
        $user = $this->createMock(Auth\User::class);
154
        $user->method('getHashedPassword')->willReturn($hash);
155
        $user->expects($this->never())->method('onLogin');
156
        
157
        $this->auth->expects($this->once())->method('fetchUserByUsername')->with('john')->willReturn($user);
158
        $this->auth->expects($this->never())->method('persistCurrentUser');
159
        
160
        $result = $this->auth->login('john', 'god');
161
        
162
        $this->assertNull($result);
163
        $this->assertNull($this->auth->user());
164
    }
165
    
166
    public function testLogout()
167
    {
168
        $user = $this->createMock(Auth\User::class);
169
        $user->expects($this->once())->method('onLogout');
170
        
171
        $this->auth->setUser($user);
172
        
173
        $this->auth->logout();
174
        
175
        $this->assertNull($this->auth->user());
176
        
177
        // Logout again shouldn't really do anything
178
        $this->auth->logout();
179
    }
180
}
181