Completed
Push — master ( a6f5e7...505dfd )
by John
02:13
created

JwtUserProviderTest::loadUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
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();
0 ignored issues
show
Unused Code introduced by
The call to the method KleijnWeb\JwtBundle\User\JwtUser::getSalt() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
80
        $user->getPassword();
0 ignored issues
show
Unused Code introduced by
The call to the method KleijnWeb\JwtBundle\User\JwtUser::getPassword() seems un-needed as the method has no side-effects.

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:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

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:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

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:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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