-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathResetPasswordCreateProcessorTest.php
More file actions
123 lines (104 loc) · 4.13 KB
/
ResetPasswordCreateProcessorTest.php
File metadata and controls
123 lines (104 loc) · 4.13 KB
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
<?php
namespace App\Tests\State;
use ApiPlatform\Metadata\Post;
use App\DTO\ResetPassword;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Security\ReCaptcha\ReCaptchaWrapper;
use App\Service\MailService;
use App\State\ResetPasswordCreateProcessor;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReCaptcha\Response;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactory;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
/**
* @internal
*/
class ResetPasswordCreateProcessorTest extends TestCase {
public const EMAIL = 'a@b.com';
public const EMAILBASE64 = 'YUBiLmNvbQ==';
private ResetPassword $resetPassword;
private MockObject|Response $recaptchaResponse;
private MockObject|UserRepository $userRepository;
private MockObject|PasswordHasherInterface $pwHasher;
private MailService|MockObject $mailService;
private ResetPasswordCreateProcessor $processor;
/**
* @throws \ReflectionException
*/
protected function setUp(): void {
$this->resetPassword = new ResetPassword();
$this->recaptchaResponse = $this->createMock(Response::class);
$recaptcha = $this->createMock(ReCaptchaWrapper::class);
$entityManager = $this->createStub(EntityManagerInterface::class);
$this->userRepository = $this->createMock(UserRepository::class);
$pwHasherFactory = $this->createMock(PasswordHasherFactory::class);
$this->pwHasher = $this->createMock(PasswordHasherInterface::class);
$this->mailService = $this->createMock(MailService::class);
$recaptcha->method('verify')->willReturn($this->recaptchaResponse);
$pwHasherFactory->method('getPasswordHasher')->willReturn($this->pwHasher);
$this->processor = new ResetPasswordCreateProcessor(
$recaptcha,
$entityManager,
$this->userRepository,
$pwHasherFactory,
$this->mailService
);
}
#[AllowMockObjectsWithoutExpectations]
public function testCreateRequiresReCaptcha() {
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(false)
;
$this->resetPassword->recaptchaToken = 'token';
$this->expectException(\Exception::class);
$this->processor->process($this->resetPassword, new Post());
}
#[AllowMockObjectsWithoutExpectations]
public function testCreateWithUnknownEmailDoesNotCreateResetKey() {
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(true)
;
$this->userRepository->expects(self::once())
->method('loadUserByIdentifier')
->willReturn(null)
;
$this->mailService->expects(self::never())
->method('sendPasswordResetLink')
;
$this->resetPassword->recaptchaToken = 'token';
$this->resetPassword->email = self::EMAIL;
$data = $this->processor->process($this->resetPassword, new Post());
self::assertThat($data, self::isNull());
}
#[AllowMockObjectsWithoutExpectations]
public function testCreateWithKnowneMailCreatesResetKey() {
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(true)
;
$user = new User();
$this->userRepository->expects(self::once())
->method('loadUserByIdentifier')
->with(self::EMAIL)
->willReturn($user)
;
$this->pwHasher->expects(self::once())
->method('hash')
->willReturnCallback(md5(...))
;
$this->mailService->expects(self::once())
->method('sendPasswordResetLink')
->with($user, $this->resetPassword)
;
$this->resetPassword->recaptchaToken = 'token';
$this->resetPassword->email = self::EMAIL;
$data = $this->processor->process($this->resetPassword, new Post());
self::assertThat($data, self::isNull());
}
}