1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace spec\Sylius\Bundle\ApiBundle\Context; |
15
|
|
|
|
16
|
|
|
use PhpSpec\ObjectBehavior; |
17
|
|
|
use Sylius\Bundle\ApiBundle\Context\UserContextInterface; |
18
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
19
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
20
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
21
|
|
|
|
22
|
|
|
final class TokenBasedUserContextSpec extends ObjectBehavior |
23
|
|
|
{ |
24
|
|
|
function let(TokenStorageInterface $tokenStorage): void |
25
|
|
|
{ |
26
|
|
|
$this->beConstructedWith($tokenStorage); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
function it_implements_user_context_interface(): void |
30
|
|
|
{ |
31
|
|
|
$this->shouldImplement(UserContextInterface::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function it_returns_user_from_token( |
35
|
|
|
TokenStorageInterface $tokenStorage, |
36
|
|
|
TokenInterface $token, |
37
|
|
|
UserInterface $user |
38
|
|
|
): void { |
39
|
|
|
$tokenStorage->getToken()->willReturn($token); |
40
|
|
|
$token->getUser()->willReturn($user); |
41
|
|
|
|
42
|
|
|
$this->getUser()->shouldReturn($user); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function it_returns_null_if_user_from_token_is_anonymous( |
46
|
|
|
TokenStorageInterface $tokenStorage, |
47
|
|
|
TokenInterface $token |
48
|
|
|
): void { |
49
|
|
|
$tokenStorage->getToken()->willReturn($token); |
50
|
|
|
$token->getUser()->willReturn('anon.'); |
51
|
|
|
|
52
|
|
|
$this->getUser()->shouldReturn(null); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
function it_returns_null_if_no_token_is_set_in_token_storage(TokenStorageInterface $tokenStorage): void |
56
|
|
|
{ |
57
|
|
|
$tokenStorage->getToken()->willReturn(null); |
58
|
|
|
|
59
|
|
|
$this->getUser()->shouldReturn(null); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|