Completed
Push — master ( fc0488...e7fa5b )
by Michael
10:16 queued 07:42
created

testCreateUserLogicException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the SecurityApiBundle package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace OsLab\SecurityApiBundle\Tests\User;
11
12
use OsLab\SecurityApiBundle\Security\User\InMemoryApiUserProvider;
13
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
14
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
15
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
16
use Symfony\Component\Security\Core\User\User;
17
18
/**
19
 * Class InMemoryApiProviderTest
20
 *
21
 * @author Michael COULLERET <[email protected]>
22
 * @author Florent DESPIERRES <[email protected]>
23
 */
24
class InMemoryApiProviderTest extends \PHPUnit_Framework_TestCase
25
{
26
    protected $userProvider;
27
    protected $users;
28
29
    public function setUp()
30
    {
31
        $this->users = [
32
            'api_1' => new User('abc', '1x4c40nwh96080gk70f7k5awz9k6tczqs3jr01z94849n', ['ROLE_API']),
33
            'api_2' => new User('cde', 'j6eef2w0689a6if50c365v2zq0c855ywgyt106j2b6q5h', ['ROLE_API']),
34
        ];
35
36
        $this->userProvider = new InMemoryApiUserProvider($this->users);
37
    }
38
39
    public function testGetUsernameByApiKeyWhenIsBadKey()
40
    {
41
        $user = $this->userProvider->getUsernameByApiKey('bad_api_key');
42
43
        $this->assertNull($user);
44
    }
45
46
    public function testGetUsernameByApiKey()
47
    {
48
        $user = $this->userProvider->getUsernameByApiKey('j6eef2w0689a6if50c365v2zq0c855ywgyt106j2b6q5h');
49
50
        $this->assertEquals($user, 'cde');
51
    }
52
53
    public function testShouldLoadUserByUsername2()
54
    {
55
        $this->expectException(UsernameNotFoundException::class);
56
57
        $this->userProvider->loadUserByUsername('api_bad');
58
    }
59
60
    public function testShouldLoadUserByUsername()
61
    {
62
        $user = $this->userProvider->loadUserByUsername('api_1');
63
64
        $this->assertInstanceOf(User::class, $user);
65
        $this->assertEquals($user->getRoles(), ['ROLE_API']);
66
    }
67
68
    public function testCreateUserLogicException()
69
    {
70
        $this->expectException(\LogicException::class);
71
72
        $user = new User('api_1', '1x4c40nwh96080gk70f7k5awz9k6tczqs3jr01z94849n', ['ROLE_API']);
73
74
        $this->userProvider->createUser($user);
75
    }
76
77
    public function testCreateUser()
78
    {
79
        $user = $this->getMockBuilder(AdvancedUserInterface::class)->disableOriginalConstructor()->getMock();
80
        $user->expects($this->any())
81
            ->method('getUsername')
82
            ->will($this->returnValue('api_3'))
83
        ;
84
85
        $this->userProvider->createUser($user);
86
    }
87
88
    public function testRefreshUserUnsupportedUserException()
89
    {
90
        $this->expectException(UnsupportedUserException::class);
91
92
        $this->userProvider->refreshUser(new User('abc', 'cde'));
93
    }
94
95
    public function testSupportsClass()
96
    {
97
        $supportsClass = $this->userProvider->supportsClass($this->userProvider);
1 ignored issue
show
Documentation introduced by
$this->userProvider is of type object<OsLab\SecurityApi...nMemoryApiUserProvider>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
99
        $this->assertFalse($supportsClass);
100
    }
101
}
102