/
InteractsWithAuthentication.php
149 lines (125 loc) · 3.81 KB
/
InteractsWithAuthentication.php
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
143
144
145
146
147
148
149
<?php
namespace Illuminate\Foundation\Testing\Concerns;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
trait InteractsWithAuthentication
{
/**
* Set the currently logged in user for the application.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string|null $driver
* @return $this
*/
public function actingAs(UserContract $user, $driver = null)
{
$this->be($user, $driver);
return $this;
}
/**
* Set the currently logged in user for the application.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string|null $driver
* @return $this
*/
public function be(UserContract $user, $driver = null)
{
$this->app['auth']->guard($driver)->setUser($user);
$this->app['auth']->shouldUse($driver);
return $this;
}
/**
* Assert that the user is authenticated.
*
* @param string|null $guard
* @return $this
*/
public function assertAuthenticated($guard = null)
{
$this->assertTrue($this->isAuthenticated($guard), 'The user is not authenticated');
return $this;
}
/**
* Assert that the user is not authenticated.
*
* @param string|null $guard
* @return $this
*/
public function assertGuest($guard = null)
{
$this->assertFalse($this->isAuthenticated($guard), 'The user is authenticated');
return $this;
}
/**
* Return true if the user is authenticated, false otherwise.
*
* @param string|null $guard
* @return bool
*/
protected function isAuthenticated($guard = null)
{
return $this->app->make('auth')->guard($guard)->check();
}
/**
* Assert that the user is authenticated as the given user.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string|null $guard
* @return $this
*/
public function assertAuthenticatedAs($user, $guard = null)
{
$expected = $this->app->make('auth')->guard($guard)->user();
$this->assertNotNull($expected, 'The current user is not authenticated.');
$this->assertInstanceOf(
get_class($expected), $user,
'The currently authenticated user is not who was expected'
);
$this->assertSame(
$expected->getAuthIdentifier(), $user->getAuthIdentifier(),
'The currently authenticated user is not who was expected'
);
return $this;
}
/**
* Assert that the given credentials are valid.
*
* @param array $credentials
* @param string|null $guard
* @return $this
*/
public function assertCredentials(array $credentials, $guard = null)
{
$this->assertTrue(
$this->hasCredentials($credentials, $guard), 'The given credentials are invalid.'
);
return $this;
}
/**
* Assert that the given credentials are invalid.
*
* @param array $credentials
* @param string|null $guard
* @return $this
*/
public function assertInvalidCredentials(array $credentials, $guard = null)
{
$this->assertFalse(
$this->hasCredentials($credentials, $guard), 'The given credentials are valid.'
);
return $this;
}
/**
* Return true if the credentials are valid, false otherwise.
*
* @param array $credentials
* @param string|null $guard
* @return bool
*/
protected function hasCredentials(array $credentials, $guard = null)
{
$provider = $this->app->make('auth')->guard($guard)->getProvider();
$user = $provider->retrieveByCredentials($credentials);
return $user && $provider->validateCredentials($user, $credentials);
}
}