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 ;
@@ -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