Skip to content

Commit 2c93875

Browse files
committed
Save the time emails are sent
1 parent e8fffcf commit 2c93875

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

features/bootstrap/DoctrineContext.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@ public function theUserHasANewEmailAddress(string $emailAddress, string $verific
280280
$user
281281
->setNewEmailAddress($emailAddress)
282282
->setNewEmailConfirmationToken($this->passwordHasher->hashPassword($user, $verificationToken))
283-
->setNewEmailAddressChangeRequestedAt(new \DateTime($emailSentAt))
284-
;
283+
->setNewEmailAddressChangeRequestedAt(new \DateTime($emailSentAt));
285284
$this->manager->flush();
286285
}
287286

src/Action/User/PasswordRequestAction.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public function __invoke(Request $request, string $username)
5353

5454
$this->entityManager->flush();
5555
$passwordResetSuccess = $this->mailer->sendPasswordResetEmail($user);
56-
if ($passwordResetSuccess) {
57-
$user->setPasswordRequestedAt(new \DateTime());
58-
$this->entityManager->flush();
59-
}
6056

6157
$response = new Response(null, $passwordResetSuccess ? Response::HTTP_OK : Response::HTTP_SERVICE_UNAVAILABLE);
6258
$response->setCache([

src/Helper/User/UserMailer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Silverback\ApiComponentsBundle\Helper\User;
1515

16+
use Doctrine\ORM\EntityManagerInterface;
1617
use Psr\Container\ContainerInterface;
1718
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
1819
use Silverback\ApiComponentsBundle\Exception\MailerTransportException;
@@ -47,20 +48,35 @@ public function sendPasswordResetEmail(AbstractUser $user): bool
4748
{
4849
$email = $this->container->get(PasswordResetEmailFactory::class)->create($user, $this->context);
4950

51+
if ($email) {
52+
$user->setPasswordRequestedAt(new \DateTime());
53+
$this->container->get(EntityManagerInterface::class)->flush();
54+
}
55+
5056
return $this->send($email);
5157
}
5258

5359
public function sendChangeEmailConfirmationEmail(AbstractUser $user): bool
5460
{
5561
$email = $this->container->get(ChangeEmailConfirmationEmailFactory::class)->create($user, $this->context);
5662

63+
if ($email) {
64+
$user->setNewEmailAddressChangeRequestedAt(new \DateTime());
65+
$this->container->get(EntityManagerInterface::class)->flush();
66+
}
67+
5768
return $this->send($email);
5869
}
5970

6071
public function sendEmailVerifyEmail(AbstractUser $user): bool
6172
{
6273
$email = $this->container->get(VerifyEmailFactory::class)->create($user, $this->context);
6374

75+
if ($email) {
76+
$user->setEmailAddressVerificationRequestedAt(new \DateTime());
77+
$this->container->get(EntityManagerInterface::class)->flush();
78+
}
79+
6480
return $this->send($email);
6581
}
6682

src/Resources/config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@
11931193
PasswordChangedEmailFactory::class => new Reference(PasswordChangedEmailFactory::class),
11941194
VerifyEmailFactory::class => new Reference(VerifyEmailFactory::class),
11951195
'logger' => new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
1196+
new Reference(EntityManagerInterface::class),
11961197
]),
11971198
'', // injected in dependency injection
11981199
]

tests/Helper/User/UserMailerTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Silverback\ApiComponentsBundle\Tests\Helper\User;
1515

16+
use Doctrine\ORM\EntityManagerInterface;
1617
use Monolog\Logger;
1718
use PHPUnit\Framework\MockObject\MockObject;
1819
use PHPUnit\Framework\TestCase;
@@ -96,8 +97,10 @@ public function test_exception_thrown_if_mailer_send_throws_exception(): void
9697
};
9798
$templateEmail = new TemplatedEmail();
9899

100+
$additionalExpectations = $this->createEmMockExpectation();
101+
99102
$loggerMock = $this->createMock(Logger::class);
100-
$factoryMock = $this->getFactoryFromContainerMock(PasswordResetEmailFactory::class, [['logger', $loggerMock]]);
103+
$factoryMock = $this->getFactoryFromContainerMock(PasswordResetEmailFactory::class, [...$additionalExpectations, ['logger', $loggerMock]]);
101104

102105
$factoryMock
103106
->expects(self::once())
@@ -126,7 +129,9 @@ public function test_send_password_reset_email(): void
126129
protected ?string $username = 'test_send_password_reset_email';
127130
};
128131

129-
$this->expectFactoryCallAndSendMailerMethod(PasswordResetEmailFactory::class, $user);
132+
$additionalExpectations = $this->createEmMockExpectation();
133+
134+
$this->expectFactoryCallAndSendMailerMethod(PasswordResetEmailFactory::class, $user, $additionalExpectations);
130135

131136
$this->userMailer->sendPasswordResetEmail($user);
132137
}
@@ -137,7 +142,9 @@ public function test_send_change_email_verification_email(): void
137142
protected ?string $username = 'test_send_change_email_verification_email';
138143
};
139144

140-
$this->expectFactoryCallAndSendMailerMethod(ChangeEmailConfirmationEmailFactory::class, $user);
145+
$additionalExpectations = $this->createEmMockExpectation();
146+
147+
$this->expectFactoryCallAndSendMailerMethod(ChangeEmailConfirmationEmailFactory::class, $user, $additionalExpectations);
141148

142149
$this->userMailer->sendChangeEmailConfirmationEmail($user);
143150
}
@@ -186,11 +193,11 @@ public function test_send_password_changed_email(): void
186193
$this->userMailer->sendPasswordChangedEmail($user);
187194
}
188195

189-
private function expectFactoryCallAndSendMailerMethod(string $factoryClass, AbstractUser $user): void
196+
private function expectFactoryCallAndSendMailerMethod(string $factoryClass, AbstractUser $user, array $additionalExpectations = []): void
190197
{
191198
$templateEmail = new TemplatedEmail();
192199

193-
$factoryMock = $this->getFactoryFromContainerMock($factoryClass);
200+
$factoryMock = $this->getFactoryFromContainerMock($factoryClass, $additionalExpectations);
194201

195202
$factoryMock
196203
->expects(self::once())
@@ -201,6 +208,19 @@ private function expectFactoryCallAndSendMailerMethod(string $factoryClass, Abst
201208
$this->expectMailerSendMethod($templateEmail);
202209
}
203210

211+
private function createEmMockExpectation(): array
212+
{
213+
$emMock = $this->createMock(EntityManagerInterface::class);
214+
$emMock->expects(self::once())->method('flush');
215+
216+
return [
217+
[
218+
EntityManagerInterface::class,
219+
$emMock,
220+
],
221+
];
222+
}
223+
204224
private function getFactoryFromContainerMock(string $factory, array $additionalExpectations = []): MockObject
205225
{
206226
$factoryMock = $this->createMock(AbstractUserEmailFactory::class);

0 commit comments

Comments
 (0)