Skip to content

Commit badf1fe

Browse files
committed
Fix unit tests
1 parent ba1ec16 commit badf1fe

5 files changed

Lines changed: 40 additions & 11 deletions

File tree

src/ApiPlatform/Api/IriConverter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ public function getIriFromResource($resource, int $referenceType = UrlGeneratorI
4444
$id = $resource->getId();
4545
if (!$id) {
4646
// id may not exist on object anymore. Deleting a page data resource with the route on will cascade,
47-
//then mercure will want to publish the change with the IRI
47+
// then mercure will want to publish the change with the IRI
4848
$parts = explode('/', $originalIri);
4949
array_pop($parts);
5050
$parts[] = $path;
51-
return join('/', $parts);
51+
52+
return implode('/', $parts);
5253
}
54+
5355
return str_replace($id->toString(), $path, $originalIri);
5456
}
5557
}

src/Helper/User/UserMailer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,42 +46,49 @@ public function __construct(MailerInterface $mailer, ContainerInterface $contain
4646
public function sendPasswordResetEmail(AbstractUser $user): bool
4747
{
4848
$email = $this->container->get(PasswordResetEmailFactory::class)->create($user, $this->context);
49+
4950
return $this->send($email);
5051
}
5152

5253
public function sendChangeEmailConfirmationEmail(AbstractUser $user): bool
5354
{
5455
$email = $this->container->get(ChangeEmailConfirmationEmailFactory::class)->create($user, $this->context);
56+
5557
return $this->send($email);
5658
}
5759

5860
public function sendEmailVerifyEmail(AbstractUser $user): bool
5961
{
6062
$email = $this->container->get(VerifyEmailFactory::class)->create($user, $this->context);
63+
6164
return $this->send($email);
6265
}
6366

6467
public function sendWelcomeEmail(AbstractUser $user): bool
6568
{
6669
$email = $this->container->get(WelcomeEmailFactory::class)->create($user, $this->context);
70+
6771
return $this->send($email);
6872
}
6973

7074
public function sendUserEnabledEmail(AbstractUser $user): bool
7175
{
7276
$email = $this->container->get(UserEnabledEmailFactory::class)->create($user, $this->context);
77+
7378
return $this->send($email);
7479
}
7580

7681
public function sendUsernameChangedEmail(AbstractUser $user): bool
7782
{
7883
$email = $this->container->get(UsernameChangedEmailFactory::class)->create($user, $this->context);
84+
7985
return $this->send($email);
8086
}
8187

8288
public function sendPasswordChangedEmail(AbstractUser $user): bool
8389
{
8490
$email = $this->container->get(PasswordChangedEmailFactory::class)->create($user, $this->context);
91+
8592
return $this->send($email);
8693
}
8794

@@ -100,10 +107,12 @@ private function send(?RawMessage $message): bool
100107
$logger->error($exception->getMessage(), [
101108
'exception' => $exception,
102109
]);
110+
103111
return false;
104112
}
105113
throw $exception;
106114
}
115+
107116
return true;
108117
}
109118
}

src/Resources/config/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@
11901190
UsernameChangedEmailFactory::class => new Reference(UsernameChangedEmailFactory::class),
11911191
PasswordChangedEmailFactory::class => new Reference(PasswordChangedEmailFactory::class),
11921192
VerifyEmailFactory::class => new Reference(VerifyEmailFactory::class),
1193-
'logger' => new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)
1193+
'logger' => new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
11941194
]),
11951195
'', // injected in dependency injection
11961196
]

src/Serializer/Normalizer/RouteNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function normalize($object, $format = null, array $context = []): float|a
6666
$operationName = $context['operation_name'] ?? null;
6767
if ('_api_/routes_manifest/{id}{._format}_get' === $operationName) {
6868
$normalized['@id'] = str_replace('routes_manifest', 'routes', $normalized['@id']);
69+
6970
return [
7071
'resource_iris' => $this->getResourceIrisFromArray($normalized),
7172
];
@@ -106,7 +107,7 @@ private function getResourceIrisFromArray(array $resource, array $iris = []): ar
106107
}
107108
}
108109

109-
return array_filter($iris, function($iri) {
110+
return array_filter($iris, function ($iri) {
110111
return !str_contains($iri, '/.well-known/');
111112
});
112113
}

tests/Helper/User/UserMailerTest.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

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

16+
use Monolog\Logger;
1617
use PHPUnit\Framework\MockObject\MockObject;
1718
use PHPUnit\Framework\TestCase;
1819
use Psr\Container\ContainerInterface;
1920
use Silverback\ApiComponentsBundle\Entity\User\AbstractUser;
20-
use Silverback\ApiComponentsBundle\Exception\MailerTransportException;
2121
use Silverback\ApiComponentsBundle\Factory\User\Mailer\AbstractUserEmailFactory;
2222
use Silverback\ApiComponentsBundle\Factory\User\Mailer\ChangeEmailConfirmationEmailFactory;
2323
use Silverback\ApiComponentsBundle\Factory\User\Mailer\PasswordChangedEmailFactory;
@@ -96,7 +96,8 @@ public function test_exception_thrown_if_mailer_send_throws_exception(): void
9696
};
9797
$templateEmail = new TemplatedEmail();
9898

99-
$factoryMock = $this->getFactoryFromContainerMock(PasswordResetEmailFactory::class);
99+
$loggerMock = $this->createMock(Logger::class);
100+
$factoryMock = $this->getFactoryFromContainerMock(PasswordResetEmailFactory::class, [['logger', $loggerMock]]);
100101

101102
$factoryMock
102103
->expects(self::once())
@@ -111,7 +112,11 @@ public function test_exception_thrown_if_mailer_send_throws_exception(): void
111112
->with($templateEmail)
112113
->willThrowException($mockException);
113114

114-
$this->expectException(MailerTransportException::class);
115+
$loggerMock
116+
->expects(self::once())
117+
->method('error')
118+
->with($mockException->getMessage());
119+
115120
$this->userMailer->sendPasswordResetEmail($user);
116121
}
117122

@@ -196,14 +201,26 @@ private function expectFactoryCallAndSendMailerMethod(string $factoryClass, Abst
196201
$this->expectMailerSendMethod($templateEmail);
197202
}
198203

199-
private function getFactoryFromContainerMock(string $factory): MockObject
204+
private function getFactoryFromContainerMock(string $factory, array $additionalExpectations = []): MockObject
200205
{
201206
$factoryMock = $this->createMock(AbstractUserEmailFactory::class);
207+
$expectations = [
208+
[$factory, $factoryMock],
209+
...$additionalExpectations,
210+
];
211+
212+
$invokedCount = self::exactly(\count($expectations));
213+
202214
$this->containerMock
203-
->expects(self::once())
215+
->expects($invokedCount)
204216
->method('get')
205-
->with($factory)
206-
->willReturn($factoryMock);
217+
->willReturnCallback(function ($parameters) use ($invokedCount, $expectations) {
218+
$currentInvocationCount = $invokedCount->numberOfInvocations();
219+
$currentExpectation = $expectations[$currentInvocationCount - 1];
220+
$this->assertSame($currentExpectation[0], $parameters);
221+
222+
return $currentExpectation[1];
223+
});
207224

208225
return $factoryMock;
209226
}

0 commit comments

Comments
 (0)