1313
1414namespace Silverback \ApiComponentsBundle \Tests \Helper \User ;
1515
16+ use Doctrine \ORM \EntityManagerInterface ;
1617use Monolog \Logger ;
1718use PHPUnit \Framework \MockObject \MockObject ;
1819use 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