@@ -108,59 +108,108 @@ public function testSendsToListOwnersWhenFlagEnabled(): void
108108 public function testFallsBackToAdminAddressesWhenNoOwnersOrFlagFalse (): void
109109 {
110110 $ configProvider = $ this ->createMock (ConfigProvider::class);
111+
111112 $ configProvider ->method ('isEnabled ' )
112113 ->with (ConfigOption::SendAdminCopies)
113114 ->willReturn (true );
114115
116+ $ getValueCalls = [];
117+
115118 $ configProvider ->expects (self ::exactly (2 ))
116119 ->method ('getValue ' )
117- ->withConsecutive ([ConfigOption::AdminAddress], [ConfigOption::AdminAddresses])
118- ->willReturnOnConsecutiveCalls (
119- 'single@example.com ' ,
120- ' admin1@example.com, , admin2@example.com ,admin1@example.com '
120+ ->willReturnCallback (
121+ function (ConfigOption $ key ) use (&$ getValueCalls ): string {
122+ $ getValueCalls [] = $ key ;
123+
124+ return match ($ key ) {
125+ ConfigOption::AdminAddress => 'single@example.com ' ,
126+ ConfigOption::AdminAddresses =>
127+ ' admin1@example.com, , admin2@example.com ,admin1@example.com ' ,
128+ default => '' ,
129+ };
130+ }
121131 );
122132
123- $ expectedRecipients = ['admin1@example.com ' , 'admin2@example.com ' , 'single@example.com ' ];
133+ $ expectedRecipients = [
134+ 'admin1@example.com ' ,
135+ 'admin2@example.com ' ,
136+ 'single@example.com ' ,
137+ ];
124138
125139 $ systemEmailBuilder = $ this ->createMock (SystemEmailBuilder::class);
126- $ systemEmailBuilder ->expects (self ::exactly (count ($ expectedRecipients )))
140+
141+ $ buildCalls = [];
142+
143+ $ systemEmailBuilder
144+ ->expects (self ::exactly (count ($ expectedRecipients )))
127145 ->method ('buildSystemEmail ' )
128- ->with (self ::callback (function (MessagePrecacheDto $ data ): bool {
129- return str_starts_with ($ data ->subject , 'phpList ' );
130- }))
131- ->willReturn (new Email ());
146+ ->willReturnCallback (
147+ function (MessagePrecacheDto $ data ) use (&$ buildCalls ): Email {
148+ $ buildCalls [] = $ data ;
149+
150+ return new Email ();
151+ }
152+ );
132153
133154 $ mailer = $ this ->createMock (MailerInterface::class);
155+
156+ $ sendCalls = [];
157+
134158 $ bounce = 'bounce@domain.test ' ;
135- $ i = 0 ;
159+
136160 $ mailer ->expects (self ::exactly (count ($ expectedRecipients )))
137161 ->method ('send ' )
138- ->with (
139- self ::isInstanceOf (Email::class),
140- self ::callback (function (Envelope $ envelope ) use ($ expectedRecipients , &$ i , $ bounce ): bool {
141- $ sender = $ envelope ->getSender ();
142- $ recipient = $ envelope ->getRecipients ()[0 ] ?? null ;
143- $ expected = $ expectedRecipients [$ i ++] ?? null ;
144- return $ sender !== null
145- && $ sender ->getAddress () === $ bounce
146- && $ recipient !== null
147- && $ recipient ->getAddress () === $ expected ;
148- })
162+ ->willReturnCallback (
163+ function (Email $ email , Envelope $ envelope ) use (&$ sendCalls ): void {
164+ $ sendCalls [] = [$ email , $ envelope ];
165+ }
149166 );
150167
151168 $ sender = new AdminCopyEmailSender (
152169 configProvider: $ configProvider ,
153170 systemEmailBuilder: $ systemEmailBuilder ,
154171 mailer: $ mailer ,
155172 logger: $ this ->createMock (LoggerInterface::class),
156- // ensure fallback path regardless of list owners
157173 sendListAdminCopy: false ,
158174 bounceEmail: $ bounce ,
159175 );
160176
161- // Even if lists have owners, flag=false should ignore them and use AdminAddress(es)
162177 $ listWithOwner = $ this ->createListWithOwner ('ignored@example.com ' );
178+
163179 $ sender ->__invoke ('System Update ' , 'Body ' , [$ listWithOwner ]);
180+
181+ $ this ->assertSame (
182+ [
183+ ConfigOption::AdminAddress,
184+ ConfigOption::AdminAddresses,
185+ ],
186+ $ getValueCalls ,
187+ );
188+
189+ $ this ->assertCount (count ($ expectedRecipients ), $ buildCalls );
190+
191+ foreach ($ buildCalls as $ call ) {
192+ $ this ->assertInstanceOf (MessagePrecacheDto::class, $ call );
193+ $ this ->assertStringStartsWith ('phpList ' , $ call ->subject );
194+ }
195+
196+ $ this ->assertCount (count ($ expectedRecipients ), $ sendCalls );
197+
198+ foreach ($ sendCalls as $ index => [$ email , $ envelope ]) {
199+ $ this ->assertInstanceOf (Email::class, $ email );
200+
201+ $ senderAddress = $ envelope ->getSender ();
202+ $ recipient = $ envelope ->getRecipients ()[0 ] ?? null ;
203+
204+ $ this ->assertNotNull ($ senderAddress );
205+ $ this ->assertSame ($ bounce , $ senderAddress ->getAddress ());
206+
207+ $ this ->assertNotNull ($ recipient );
208+ $ this ->assertSame (
209+ $ expectedRecipients [$ index ],
210+ $ recipient ->getAddress ()
211+ );
212+ }
164213 }
165214
166215 private function createListWithOwner (string $ email ): SubscriberList
0 commit comments