1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\JwtBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
namespace KleijnWeb\JwtBundle\Tests\User; |
9
|
|
|
|
10
|
|
|
use KleijnWeb\JwtBundle\Authenticator\JwtToken; |
11
|
|
|
use KleijnWeb\JwtBundle\User\JwtUserProvider; |
12
|
|
|
use KleijnWeb\JwtBundle\User\JwtUser; |
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author John Kleijn <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class JwtUserProviderTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var JwtUserProvider |
22
|
|
|
*/ |
23
|
|
|
private $provider; |
24
|
|
|
|
25
|
|
|
protected function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->provider = new JwtUserProvider(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @test |
32
|
|
|
*/ |
33
|
|
|
public function canLoadUserPopulatedFromToken() |
34
|
|
|
{ |
35
|
|
|
$username = 'johndoe'; |
36
|
|
|
$claims = ['sub' => $username, 'iat' => time()]; |
37
|
|
|
$user = $this->loadUser($claims); |
38
|
|
|
$this->assertSame($username, $user->getUsername()); |
39
|
|
|
$this->assertSame($claims, $user->getClaims()); |
40
|
|
|
$this->assertSame([JwtUserProvider::BASE_ROLE], $user->getRoles()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @test |
45
|
|
|
*/ |
46
|
|
|
public function willAddGroupsFromAudienceClaims() |
47
|
|
|
{ |
48
|
|
|
$claims = ['sub' => 'johndoe', 'aud' => 'admin']; |
49
|
|
|
$user = $this->loadUser($claims); |
50
|
|
|
$this->assertSame([JwtUserProvider::BASE_ROLE, 'ROLE_ADMIN'], $user->getRoles()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
*/ |
56
|
|
|
public function onlySupportsJwtUser() |
57
|
|
|
{ |
58
|
|
|
$this->assertFalse($this->provider->supportsClass(UserInterface::class)); |
59
|
|
|
$this->assertTrue($this->provider->supportsClass(JwtUser::class)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
*/ |
65
|
|
|
public function refreshUserReturnsNewInstance() |
66
|
|
|
{ |
67
|
|
|
$username = 'johndoe'; |
68
|
|
|
$user = $this->loadUser(['sub' => $username]); |
69
|
|
|
$this->assertNotSame($user, $this->provider->refreshUser($user)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @test |
74
|
|
|
*/ |
75
|
|
|
public function canCallSuperfluousMethods() |
76
|
|
|
{ |
77
|
|
|
$user = $this->loadUser(['sub' => 'johndoe']); |
78
|
|
|
$user->eraseCredentials(); |
79
|
|
|
$user->getSalt(); |
|
|
|
|
80
|
|
|
$user->getPassword(); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $claims |
85
|
|
|
* @return JwtUser |
86
|
|
|
*/ |
87
|
|
|
private function loadUser(array $claims) |
88
|
|
|
{ |
89
|
|
|
$token = $this->createToken($claims); |
90
|
|
|
$this->provider->setClaimsUsingToken($token); |
91
|
|
|
return $this->provider->loadUserByUsername($claims['sub']); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $claims |
96
|
|
|
* |
97
|
|
|
* @return JwtToken |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
private function createToken(array $claims) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
return new JwtToken([ |
102
|
|
|
'header' => [ |
103
|
|
|
'alg' => 'HS256', |
104
|
|
|
'typ' => 'JWT', |
105
|
|
|
'kid' => 'keyOne' |
106
|
|
|
], |
107
|
|
|
'claims' => $claims, |
108
|
|
|
'secret' => 'secret' |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: