Skip to content

Commit 3e0fd2a

Browse files
authored
bug #118 [Sender] Assert recipient emails are always not empty strings (lchrusciel)
This PR was merged into the 1.7 branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT In Sylius our "getEmail" contract on "Customer" is saying that email can be null. Of course, we should never pass null to the sender, however, the psalm is complaining that we've changed rules. with the #113. I'm in favour of enforcing this rule, but rather in runtime to keep all the builds green. We can change it with SyliusMailer 2.0 Related psalm test: https://psalm.dev/r/18ab1bda24 Commits ------- 7c8c2f7 [Sender] Assert recipient emails are always not empty strings
2 parents d5eb632 + 7c8c2f7 commit 3e0fd2a

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/Component/Sender/Sender.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Sylius\Component\Mailer\Provider\EmailProviderInterface;
1818
use Sylius\Component\Mailer\Renderer\Adapter\AdapterInterface as RendererAdapterInterface;
1919
use Sylius\Component\Mailer\Sender\Adapter\AdapterInterface as SenderAdapterInterface;
20+
use Webmozart\Assert\Assert;
2021

2122
final class Sender implements SenderInterface
2223
{
@@ -45,6 +46,8 @@ public function __construct(
4546
*/
4647
public function send(string $code, array $recipients, array $data = [], array $attachments = [], array $replyTo = []): void
4748
{
49+
Assert::allStringNotEmpty($recipients);
50+
4851
$email = $this->provider->getEmail($code);
4952

5053
if (!$email->isEnabled()) {

src/Component/Sender/SenderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
interface SenderInterface
1717
{
1818
/**
19-
* @param string[] $recipients A list of email addresses to receive the message.
19+
* @param string[]|null[] $recipients A list of email addresses to receive the message. Providing null or empty string in the list of recipients is deprecated and will be removed in SyliusMailerBundle 2.0
2020
* @param string[] $attachments A list of file paths to attach to the message.
2121
* @param string[] $replyTo A list of email addresses to set as the Reply-To address for the message.
2222
*/

src/Component/spec/Sender/SenderSpec.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace spec\Sylius\Component\Mailer\Sender;
1515

1616
use PhpSpec\ObjectBehavior;
17+
use Prophecy\Argument;
1718
use Sylius\Component\Mailer\Model\EmailInterface;
1819
use Sylius\Component\Mailer\Provider\DefaultSettingsProviderInterface;
1920
use Sylius\Component\Mailer\Provider\EmailProviderInterface;
@@ -66,4 +67,27 @@ function it_does_not_send_disabled_emails(
6667

6768
$this->send('bar', ['john@example.com'], ['foo' => 2], []);
6869
}
70+
71+
function it_throws_an_exception_if_wrong_value_is_provided_as_recipient_email(
72+
RendererAdapterInterface $rendererAdapter,
73+
SenderAdapterInterface $senderAdapter
74+
): void {
75+
$rendererAdapter->render(Argument::any())->shouldNotBeCalled();
76+
$senderAdapter->send(Argument::any())->shouldNotBeCalled();
77+
78+
$this->shouldThrow(\InvalidArgumentException::class)->during(
79+
'send',
80+
['bar', ['john@example.com', null], ['foo' => 2], []],
81+
);
82+
83+
$this->shouldThrow(\InvalidArgumentException::class)->during(
84+
'send',
85+
['bar', [5], ['foo' => 2], []],
86+
);
87+
88+
$this->shouldThrow(\InvalidArgumentException::class)->during(
89+
'send',
90+
['bar', [''], ['foo' => 2], []],
91+
);
92+
}
6993
}

0 commit comments

Comments
 (0)