Completed
Push — master ( b4619a...ab6ee6 )
by Kamil
12:06 queued 06:35
created

TokenBasedUserContextSpec   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_implements_user_context_interface() 0 4 1
A it_returns_user_from_token() 0 10 1
A it_returns_null_if_user_from_token_is_anonymous() 0 9 1
A it_returns_null_if_no_token_is_set_in_token_storage() 0 6 1
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