|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Symnedi\Security\Tests\Core\Authentication\Token; |
|
6
|
|
|
|
|
7
|
|
|
use Nette\Http\UserStorage; |
|
8
|
|
|
use Nette\Security\Identity; |
|
9
|
|
|
use Nette\Security\User; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use Symnedi\Security\Core\Authentication\Token\NetteTokenAdapter; |
|
12
|
|
|
|
|
13
|
|
|
final class NetteTokenAdapterTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var NetteTokenAdapter |
|
17
|
|
|
*/ |
|
18
|
|
|
private $netteTokenAdapter; |
|
19
|
|
|
|
|
20
|
|
|
protected function setUp() |
|
21
|
|
|
{ |
|
22
|
|
|
$userStorageMock = $this->prophesize(UserStorage::class); |
|
23
|
|
|
$userStorageMock->setAuthenticated('...')->willReturn('...'); |
|
24
|
|
|
|
|
25
|
|
|
$identityMock = $this->prophesize(Identity::class); |
|
26
|
|
|
$identityMock->getData()->willReturn('attributes'); |
|
27
|
|
|
|
|
28
|
|
|
$userMock = $this->prophesize(User::class); |
|
29
|
|
|
$userMock->getRoles()->willReturn(['user']); |
|
30
|
|
|
$userMock->getIdentity()->willReturn($identityMock->reveal()); |
|
31
|
|
|
$userMock->isLoggedIn()->willReturn(true); |
|
32
|
|
|
$userMock->getStorage()->willReturn($userStorageMock->reveal()); |
|
33
|
|
|
|
|
34
|
|
|
$this->netteTokenAdapter = (new NetteTokenAdapter()); |
|
35
|
|
|
$this->netteTokenAdapter->setUser($userMock->reveal()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testSetGetUser() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->assertInstanceOf(User::class, $this->netteTokenAdapter->getUser()); |
|
41
|
|
|
$this->netteTokenAdapter->setUser('...'); |
|
|
|
|
|
|
42
|
|
|
$this->assertSame('...', $this->netteTokenAdapter->getUser()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testGetRoles() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->assertSame(['user'], $this->netteTokenAdapter->getRoles()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testGetCredentials() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->assertInstanceOf(Identity::class, $this->netteTokenAdapter->getCredentials()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testIsAuthenticated() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->assertTrue($this->netteTokenAdapter->isAuthenticated()); |
|
58
|
|
|
$this->netteTokenAdapter->setAuthenticated('...'); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testGetAttributes() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->assertSame(['attributes'], $this->netteTokenAdapter->getAttributes()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @expectedException \Symnedi\Security\Exception\NotImplementedException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testToString() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->netteTokenAdapter->__toString(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @expectedException \Symnedi\Security\Exception\NotImplementedException |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testSerialize() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->netteTokenAdapter->serialize(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @expectedException \Symnedi\Security\Exception\NotImplementedException |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testUnserialize() |
|
86
|
|
|
{ |
|
87
|
|
|
$this->netteTokenAdapter->unserialize('...'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testGetUsername() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->assertFalse($this->netteTokenAdapter->getUsername()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @expectedException \Symnedi\Security\Exception\NotImplementedException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function testEraseCredentials() |
|
99
|
|
|
{ |
|
100
|
|
|
$this->netteTokenAdapter->eraseCredentials(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @expectedException \Symnedi\Security\Exception\NotImplementedException |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testSetAttributes() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->netteTokenAdapter->setAttributes(['someKey' => 'someValue']); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testHasAttribute() |
|
112
|
|
|
{ |
|
113
|
|
|
$this->assertFalse($this->netteTokenAdapter->hasAttribute('someKey')); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testGetAttribute() |
|
117
|
|
|
{ |
|
118
|
|
|
$this->assertFalse($this->netteTokenAdapter->getAttribute('someKey')); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @expectedException \Symnedi\Security\Exception\NotImplementedException |
|
123
|
|
|
*/ |
|
124
|
|
|
public function testSetAttribute() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->netteTokenAdapter->setAttribute('someKey', 'someValue'); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
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: