1313
1414namespace Silverback \ApiComponentsBundle \Tests \Helper \User ;
1515
16+ use Monolog \Logger ;
1617use PHPUnit \Framework \MockObject \MockObject ;
1718use PHPUnit \Framework \TestCase ;
1819use Psr \Container \ContainerInterface ;
1920use Silverback \ApiComponentsBundle \Entity \User \AbstractUser ;
20- use Silverback \ApiComponentsBundle \Exception \MailerTransportException ;
2121use Silverback \ApiComponentsBundle \Factory \User \Mailer \AbstractUserEmailFactory ;
2222use Silverback \ApiComponentsBundle \Factory \User \Mailer \ChangeEmailConfirmationEmailFactory ;
2323use 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