Skip to content

Commit d47262d

Browse files
authored
Merge pull request phpbb#6796 from marc1706/ticket/17490
[ticket/17490] Add unit tests for symfony mailer classes
2 parents 6ecb41f + bf2dd2c commit d47262d

File tree

6 files changed

+1760
-29
lines changed

6 files changed

+1760
-29
lines changed

phpBB/phpbb/messenger/method/base.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ abstract public function process_queue(array &$queue_data): void;
216216
* @param string $template_path Email template path
217217
* @param string $template_dir_prefix Email template directory prefix
218218
*
219-
* @return bool
219+
* @return void
220220
*/
221-
public function template(string $template_file, string $template_lang = '', string $template_path = '', string $template_dir_prefix = ''): bool
221+
public function template(string $template_file, string $template_lang = '', string $template_path = '', string $template_dir_prefix = ''): void
222222
{
223223
$template_dir_prefix = (!$template_dir_prefix || $template_dir_prefix[0] === '/') ? $template_dir_prefix : '/' . $template_dir_prefix;
224224

@@ -290,8 +290,6 @@ public function template(string $template_file, string $template_lang = '', stri
290290
$this->template->set_filenames([
291291
'body' => $template_file . '.txt',
292292
]);
293-
294-
return true;
295293
}
296294

297295
/**
@@ -425,7 +423,7 @@ public function error(string $msg): void
425423
*/
426424
public function save_queue(): void
427425
{
428-
if ($this->use_queue && !empty($this->queue))
426+
if ($this->use_queue)
429427
{
430428
$this->queue->save();
431429
}
@@ -487,6 +485,5 @@ protected function set_template_paths(string|array $path_name, string|array $pat
487485
*/
488486
public function header(string $header_name, mixed $header_value): void
489487
{
490-
return;
491488
}
492489
}

phpBB/phpbb/messenger/method/email.php

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
namespace phpbb\messenger\method;
1515

16+
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
17+
use Symfony\Component\Mailer\MailerInterface;
1618
use Symfony\Component\Mailer\Transport;
1719
use Symfony\Component\Mailer\Mailer;
20+
use Symfony\Component\Mailer\Transport\AbstractTransport;
21+
use Symfony\Component\Mailer\Transport\TransportInterface;
1822
use Symfony\Component\Mime\Address;
1923
use Symfony\Component\Mime\Email as symfony_email;
2024
use Symfony\Component\Mime\Header\Headers;
@@ -38,16 +42,16 @@ class email extends base
3842
*
3943
* Symfony Mailer transport DSN
4044
*/
41-
protected $dsn = '';
45+
protected string $dsn = '';
4246

4347
/** @var symfony_email */
44-
protected $email;
48+
protected symfony_email $email;
4549

46-
/** @var Address */
47-
protected $from;
50+
/** @var Address From address */
51+
protected Address $from;
4852

49-
/** @var Headers */
50-
protected $headers;
53+
/** @var Headers Email headers */
54+
protected Headers $headers;
5155

5256
/**
5357
* @var int
@@ -59,16 +63,16 @@ class email extends base
5963
* symfony_email::PRIORITY_LOW
6064
* symfony_email::PRIORITY_LOWEST
6165
*/
62-
protected $mail_priority = symfony_email::PRIORITY_NORMAL;
66+
protected int $mail_priority = symfony_email::PRIORITY_NORMAL;
6367

6468
/** @var \phpbb\messenger\queue */
6569
protected $queue;
6670

6771
/** @var Address */
68-
protected $reply_to;
72+
protected Address $reply_to;
6973

70-
/** @var \Symfony\Component\Mailer\Transport\AbstractTransport */
71-
protected $transport;
74+
/** @var AbstractTransport */
75+
protected AbstractTransport $transport;
7276

7377
/**
7478
* {@inheritDoc}
@@ -124,7 +128,7 @@ public function set_addresses(array $user_row): void
124128
{
125129
if (!empty($user_row['user_email']))
126130
{
127-
$this->to($user_row['user_email'], $user_row['username'] ?: '');
131+
$this->to($user_row['user_email'], $user_row['username'] ?? '');
128132
}
129133
}
130134

@@ -142,10 +146,7 @@ public function to(string $address, string $realname = ''): void
142146
return;
143147
}
144148

145-
// If empty sendmail_path on windows, PHP changes the to line
146-
$windows_empty_sendmail_path = !$this->config['smtp_delivery'] && DIRECTORY_SEPARATOR == '\\';
147-
148-
$to = new Address($address, $windows_empty_sendmail_path ? '' : trim($realname));
149+
$to = new Address($address, trim($realname));
149150
$this->email->getTo() ? $this->email->addTo($to) : $this->email->to($to);
150151
}
151152

@@ -273,7 +274,6 @@ public function set_mail_priority(int $priority = symfony_email::PRIORITY_NORMAL
273274
*/
274275
protected function build_headers(): void
275276
{
276-
277277
$board_contact = trim($this->config['board_contact']);
278278
$contact_name = html_entity_decode($this->config['board_contact_name'], ENT_COMPAT);
279279

@@ -315,7 +315,6 @@ protected function build_headers(): void
315315
{
316316
$this->header($header, $value);
317317
}
318-
319318
}
320319

321320
/**
@@ -406,7 +405,7 @@ public function process_queue(array &$queue_data): void
406405

407406
$package_size = $queue_data[$queue_object_name]['package_size'] ?? 0;
408407
$num_items = (!$package_size || $messages_count < $package_size) ? $messages_count : $package_size;
409-
$mailer = new Mailer($this->transport);
408+
$mailer = $this->get_mailer();
410409

411410
for ($i = 0; $i < $num_items; $i++)
412411
{
@@ -435,7 +434,7 @@ public function process_queue(array &$queue_data): void
435434
{
436435
$mailer->send($email);
437436
}
438-
catch (\Symfony\Component\Mailer\Exception\TransportExceptionInterface $e)
437+
catch (TransportExceptionInterface $e)
439438
{
440439
$this->error($e->getDebug());
441440
continue;
@@ -450,12 +449,22 @@ public function process_queue(array &$queue_data): void
450449
}
451450
}
452451

452+
/**
453+
* Get mailer object
454+
*
455+
* @return MailerInterface Symfony Mailer object
456+
*/
457+
protected function get_mailer(): MailerInterface
458+
{
459+
return new Mailer($this->transport);
460+
}
461+
453462
/**
454463
* Get mailer transport object
455464
*
456-
* @return \Symfony\Component\Mailer\Transport\TransportInterface Symfony Mailer transport object
465+
* @return TransportInterface Symfony Mailer transport object
457466
*/
458-
public function get_transport(): \Symfony\Component\Mailer\Transport\TransportInterface
467+
public function get_transport(): TransportInterface
459468
{
460469
return $this->transport;
461470
}
@@ -504,7 +513,7 @@ public function send(): bool
504513
// Send message ...
505514
if (!$this->use_queue)
506515
{
507-
$mailer = new Mailer($this->transport);
516+
$mailer = $this->get_mailer();
508517

509518
$subject = $this->subject;
510519
$msg = $this->msg;
@@ -538,7 +547,7 @@ public function send(): bool
538547
{
539548
$mailer->send($this->email);
540549
}
541-
catch (\Symfony\Component\Mailer\Exception\TransportExceptionInterface $e)
550+
catch (TransportExceptionInterface $e)
542551
{
543552
$this->error($e->getDebug());
544553
return false;

0 commit comments

Comments
 (0)