-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathResendActivationProcessorTest.php
More file actions
153 lines (126 loc) · 4.88 KB
/
ResendActivationProcessorTest.php
File metadata and controls
153 lines (126 loc) · 4.88 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace App\Tests\State;
use ApiPlatform\Metadata\Post;
use App\DTO\UserActivation;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Security\ReCaptcha\ReCaptchaWrapper;
use App\Service\MailService;
use App\State\ResendActivationProcessor;
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReCaptcha\Response;
use function PHPUnit\Framework\isNull;
use function PHPUnit\Framework\logicalNot;
/**
* @internal
*/
class ResendActivationProcessorTest extends TestCase {
public const EMAIL = 'a@b.com';
private UserActivation $userActivation;
private MockObject|Response $recaptchaResponse;
private MockObject|UserRepository $userRepository;
private MailService|MockObject $mailService;
private ResendActivationProcessor $processor;
/**
* @throws Exception
*/
protected function setUp(): void {
$this->userActivation = new UserActivation();
$this->recaptchaResponse = $this->createMock(Response::class);
$recaptcha = $this->createMock(ReCaptchaWrapper::class);
$entityManager = $this->createStub(EntityManager::class);
$this->userRepository = $this->createMock(UserRepository::class);
$this->mailService = $this->createMock(MailService::class);
$recaptcha->method('verify')->willReturn($this->recaptchaResponse);
$this->processor = new ResendActivationProcessor(
$recaptcha,
$entityManager,
$this->userRepository,
$this->mailService
);
}
#[AllowMockObjectsWithoutExpectations]
public function testCreateRequiresReCaptcha() {
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(false)
;
$this->userActivation->recaptchaToken = 'token';
$this->expectException(\Exception::class);
$this->processor->process($this->userActivation, new Post());
}
#[AllowMockObjectsWithoutExpectations]
public function testCreateWithUnknownEmailDoesNothing() {
$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->userActivation->recaptchaToken = 'token';
$this->userActivation->email = self::EMAIL;
$data = $this->processor->process($this->userActivation, new Post());
self::assertThat($data, self::isNull());
}
#[AllowMockObjectsWithoutExpectations]
#[TestWith([User::STATE_REGISTERED])]
public function testCreateWithInactiveUserResendsActivationeMail(string $state) {
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(true)
;
$user = new User();
$user->state = $state;
$user->activationKey = 'activationKey';
$user->activationKeyHash = 'activationKey';
$this->userRepository->expects(self::once())
->method('loadUserByIdentifier')
->with(self::EMAIL)
->willReturn($user)
;
$this->mailService->expects(self::once())
->method('sendUserActivationMail')
->with($user, logicalNot(isNull()))
;
$this->userActivation->recaptchaToken = 'token';
$this->userActivation->email = self::EMAIL;
$data = $this->processor->process($this->userActivation, new Post());
self::assertThat($data, self::isNull());
}
#[AllowMockObjectsWithoutExpectations]
#[TestWith([User::STATE_NONREGISTERED])]
#[TestWith([User::STATE_ACTIVATED])]
#[TestWith([User::STATE_DELETED])]
public function testCreateWithActiveOrDeletedUserDoesNothing(string $state) {
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(true)
;
$user = new User();
$user->state = $state;
$user->activationKey = 'activationKey';
$user->activationKeyHash = 'activationKey';
$this->userRepository->expects(self::once())
->method('loadUserByIdentifier')
->with(self::EMAIL)
->willReturn($user)
;
$this->mailService->expects(self::never())
->method('sendUserActivationMail')
;
$this->userActivation->recaptchaToken = 'token';
$this->userActivation->email = self::EMAIL;
$data = $this->processor->process($this->userActivation, new Post());
self::assertThat($data, self::isNull());
}
}