Skip to content

Commit d464fbb

Browse files
committed
Fix unit tests
1 parent ba1ec16 commit d464fbb

5 files changed

Lines changed: 41 additions & 10 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: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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;
@@ -96,7 +97,9 @@ public function test_exception_thrown_if_mailer_send_throws_exception(): void
9697
};
9798
$templateEmail = new TemplatedEmail();
9899

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

101104
$factoryMock
102105
->expects(self::once())
@@ -111,7 +114,11 @@ public function test_exception_thrown_if_mailer_send_throws_exception(): void
111114
->with($templateEmail)
112115
->willThrowException($mockException);
113116

114-
$this->expectException(MailerTransportException::class);
117+
$loggerMock
118+
->expects(self::once())
119+
->method('error')
120+
->with($mockException->getMessage());
121+
115122
$this->userMailer->sendPasswordResetEmail($user);
116123
}
117124

@@ -196,14 +203,26 @@ private function expectFactoryCallAndSendMailerMethod(string $factoryClass, Abst
196203
$this->expectMailerSendMethod($templateEmail);
197204
}
198205

199-
private function getFactoryFromContainerMock(string $factory): MockObject
206+
private function getFactoryFromContainerMock(string $factory, array $additionalExpectations = []): MockObject
200207
{
208+
201209
$factoryMock = $this->createMock(AbstractUserEmailFactory::class);
210+
$expectations = [
211+
[$factory, $factoryMock],
212+
...$additionalExpectations,
213+
];
214+
215+
$invokedCount = self::exactly(count($expectations));
216+
202217
$this->containerMock
203-
->expects(self::once())
218+
->expects($invokedCount)
204219
->method('get')
205-
->with($factory)
206-
->willReturn($factoryMock);
220+
->willReturnCallback(function ($parameters) use ($invokedCount, $expectations) {
221+
$currentInvocationCount = $invokedCount->numberOfInvocations();
222+
$currentExpectation = $expectations[$currentInvocationCount - 1];
223+
$this->assertSame($currentExpectation[0], $parameters);
224+
return $currentExpectation[1];
225+
});
207226

208227
return $factoryMock;
209228
}

0 commit comments

Comments
 (0)