/
AbstractTokenTest.php
124 lines (102 loc) · 3.98 KB
/
AbstractTokenTest.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Tests\Authentication\Token;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Core\User\UserInterface;
class AbstractTokenTest extends TestCase
{
/**
* @dataProvider provideUsers
*/
public function testGetUserIdentifier($user, string $username)
{
$token = new ConcreteToken(['ROLE_FOO']);
$token->setUser($user);
$this->assertEquals($username, $token->getUserIdentifier());
}
public static function provideUsers()
{
yield [new InMemoryUser('fabien', null), 'fabien'];
}
public function testEraseCredentials()
{
$token = new ConcreteToken(['ROLE_FOO']);
$user = $this->createMock(UserInterface::class);
$user->expects($this->once())->method('eraseCredentials');
$token->setUser($user);
$token->eraseCredentials();
}
public function testSerialize()
{
$token = new ConcreteToken(['ROLE_FOO', 'ROLE_BAR']);
$token->setAttributes(['foo' => 'bar']);
$uToken = unserialize(serialize($token));
$this->assertEquals($token->getRoleNames(), $uToken->getRoleNames());
$this->assertEquals($token->getAttributes(), $uToken->getAttributes());
}
public function testConstructor()
{
$token = new ConcreteToken(['ROLE_FOO']);
$this->assertEquals(['ROLE_FOO'], $token->getRoleNames());
}
public function testAttributes()
{
$attributes = ['foo' => 'bar'];
$token = new ConcreteToken();
$token->setAttributes($attributes);
$this->assertEquals($attributes, $token->getAttributes(), '->getAttributes() returns the token attributes');
$this->assertEquals('bar', $token->getAttribute('foo'), '->getAttribute() returns the value of an attribute');
$token->setAttribute('foo', 'foo');
$this->assertEquals('foo', $token->getAttribute('foo'), '->setAttribute() changes the value of an attribute');
$this->assertTrue($token->hasAttribute('foo'), '->hasAttribute() returns true if the attribute is defined');
$this->assertFalse($token->hasAttribute('oof'), '->hasAttribute() returns false if the attribute is not defined');
try {
$token->getAttribute('foobar');
$this->fail('->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
} catch (\Exception $e) {
$this->assertInstanceOf(\InvalidArgumentException::class, $e, '->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
$this->assertEquals('This token has no "foobar" attribute.', $e->getMessage(), '->getAttribute() throws an \InvalidArgumentException exception when the attribute does not exist');
}
}
/**
* @dataProvider provideUsers
*/
public function testSetUser($user)
{
$token = new ConcreteToken();
$token->setUser($user);
$this->assertSame($user, $token->getUser());
}
}
class ConcreteToken extends AbstractToken
{
private string $credentials = 'credentials_value';
public function __construct(array $roles = [], ?UserInterface $user = null)
{
parent::__construct($roles);
if (null !== $user) {
$this->setUser($user);
}
}
public function __serialize(): array
{
return [$this->credentials, parent::__serialize()];
}
public function __unserialize(array $data): void
{
[$this->credentials, $parentState] = $data;
parent::__unserialize($parentState);
}
public function getCredentials(): mixed
{
}
}