Completed
Push — theme-collector ( e0987d )
by Kamil
23:25
created

UsernameOrEmailProviderSpec   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 6
dl 0
loc 73
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_implements_symfony_user_provider_interface() 0 4 1
A it_should_extend_user_provider() 0 4 1
A it_supports_sylius_user_model() 0 4 1
A it_does_not_support_other_classes() 0 5 1
A it_loads_user_by_username() 0 11 1
A it_throws_exception_when_there_is_no_user_with_given_username_or_email() 0 11 1
A it_loads_user_by_email() 0 11 1
A it_refreshes_user() 0 8 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\UserBundle\Provider;
15
16
use PhpSpec\ObjectBehavior;
17
use Sylius\Bundle\UserBundle\Provider\AbstractUserProvider;
18
use Sylius\Bundle\UserBundle\Provider\UsernameOrEmailProvider;
19
use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
20
use Sylius\Component\User\Model\User;
21
use Sylius\Component\User\Model\UserInterface;
22
use Sylius\Component\User\Repository\UserRepositoryInterface;
23
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
24
use Symfony\Component\Security\Core\User\UserProviderInterface;
25
26
/**
27
 * @author Łukasz Chruściel <[email protected]>
28
 * @author Michał Marcinkowski <[email protected]>
29
 */
30
final class UsernameOrEmailProviderSpec extends ObjectBehavior
31
{
32
    function let(UserRepositoryInterface $userRepository, CanonicalizerInterface $canonicalizer): void
33
    {
34
        $this->beConstructedWith(User::class, $userRepository, $canonicalizer);
35
    }
36
37
    function it_implements_symfony_user_provider_interface(): void
38
    {
39
        $this->shouldImplement(UserProviderInterface::class);
40
    }
41
42
    function it_should_extend_user_provider(): void
43
    {
44
        $this->shouldHaveType(AbstractUserProvider::class);
45
    }
46
47
    function it_supports_sylius_user_model(): void
48
    {
49
        $this->supportsClass(User::class)->shouldReturn(true);
50
    }
51
52
    function it_does_not_support_other_classes(): void
53
    {
54
        $this->supportsClass('Sylius\Component\User\Model\CustomerGroupInterface')->shouldReturn(false);
55
        $this->supportsClass('Acme\Fake\Class')->shouldReturn(false);
56
    }
57
58
    function it_loads_user_by_username(
59
        UserRepositoryInterface $userRepository,
60
        CanonicalizerInterface $canonicalizer,
61
        UserInterface $user
62
    ): void {
63
        $canonicalizer->canonicalize('testUser')->willReturn('testuser');
64
65
        $userRepository->findOneBy(['usernameCanonical' => 'testuser'])->willReturn($user);
66
67
        $this->loadUserByUsername('testUser')->shouldReturn($user);
68
    }
69
70
    function it_throws_exception_when_there_is_no_user_with_given_username_or_email(
71
        UserRepositoryInterface $userRepository,
72
        CanonicalizerInterface $canonicalizer
73
    ): void {
74
        $canonicalizer->canonicalize('testUser')->willReturn('testuser');
75
76
        $userRepository->findOneBy(['usernameCanonical' => 'testuser'])->willReturn(null);
77
        $userRepository->findOneByEmail('testuser')->willReturn(null);
78
79
        $this->shouldThrow(new UsernameNotFoundException('Username "testuser" does not exist.'))->during('loadUserByUsername', ['testUser']);
80
    }
81
82
    function it_loads_user_by_email(
83
        UserRepositoryInterface $userRepository,
84
        CanonicalizerInterface $canonicalizer,
85
        UserInterface $user
86
    ): void {
87
        $canonicalizer->canonicalize('[email protected]')->willReturn('[email protected]');
88
89
        $userRepository->findOneByEmail('[email protected]')->willReturn($user);
90
91
        $this->loadUserByUsername('[email protected]')->shouldReturn($user);
92
    }
93
94
    function it_refreshes_user(UserRepositoryInterface $userRepository, User $user, UserInterface $refreshedUser): void
95
    {
96
        $userRepository->find(1)->willReturn($refreshedUser);
97
98
        $user->getId()->willReturn(1);
99
100
        $this->refreshUser($user)->shouldReturn($refreshedUser);
101
    }
102
}
103