|
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
|
|
|
|