Skip to content

Commit d11567f

Browse files
committed
Ensure non-empty phone and email in *Message classes
1 parent 1dab3f5 commit d11567f

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

src/Symfony/Component/Notifier/Message/EmailMessage.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Mailer\Envelope;
1616
use Symfony\Component\Mime\Email;
1717
use Symfony\Component\Mime\RawMessage;
18+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1819
use Symfony\Component\Notifier\Exception\LogicException;
1920
use Symfony\Component\Notifier\Notification\Notification;
2021
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
@@ -37,6 +38,10 @@ public function __construct(RawMessage $message, Envelope $envelope = null)
3738

3839
public static function fromNotification(Notification $notification, EmailRecipientInterface $recipient): self
3940
{
41+
if ('' === $recipient->getEmail()) {
42+
throw new InvalidArgumentException(sprintf('"%s" needs an email, it cannot be empty.', static::class));
43+
}
44+
4045
if (!class_exists(NotificationEmail::class)) {
4146
$email = (new Email())
4247
->to($recipient->getEmail())

src/Symfony/Component/Notifier/Message/SmsMessage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Notifier\Message;
1313

14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1415
use Symfony\Component\Notifier\Notification\Notification;
1516
use Symfony\Component\Notifier\Recipient\SmsRecipientInterface;
1617

@@ -27,6 +28,10 @@ final class SmsMessage implements MessageInterface
2728

2829
public function __construct(string $phone, string $subject)
2930
{
31+
if ('' === $phone) {
32+
throw new InvalidArgumentException(sprintf('"%s" needs a phone number, it cannot be empty.', static::class));
33+
}
34+
3035
$this->subject = $subject;
3136
$this->phone = $phone;
3237
}
@@ -41,6 +46,10 @@ public static function fromNotification(Notification $notification, SmsRecipient
4146
*/
4247
public function phone(string $phone): self
4348
{
49+
if ('' === $phone) {
50+
throw new InvalidArgumentException(sprintf('"%s" needs a phone number, it cannot be empty.', static::class));
51+
}
52+
4453
$this->phone = $phone;
4554

4655
return $this;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Notifier\Tests\Message;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Notifier\Message\EmailMessage;
9+
use Symfony\Component\Notifier\Notification\Notification;
10+
use Symfony\Component\Notifier\Recipient\Recipient;
11+
12+
/**
13+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
14+
*/
15+
class EmailMessageTest extends TestCase
16+
{
17+
public function testEnsureNonEmptyEmailOnCreationFromNotification()
18+
{
19+
$this->expectException(\InvalidArgumentException::class);
20+
$this->expectExceptionMessage('"Symfony\Component\Notifier\Message\EmailMessage" needs an email, it cannot be empty.');
21+
22+
EmailMessage::fromNotification(new Notification(), new Recipient('', '+3312345678'));
23+
}
24+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Notifier\Tests\Message;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\Notifier\Message\SmsMessage;
9+
10+
/**
11+
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
12+
*/
13+
class SmsMessageTest extends TestCase
14+
{
15+
public function testCanBeConstructed()
16+
{
17+
$message = new SmsMessage('+3312345678', 'subject');
18+
19+
$this->assertSame('subject', $message->getSubject());
20+
$this->assertSame('+3312345678', $message->getPhone());
21+
}
22+
23+
public function testEnsureNonEmptyPhoneOnConstruction()
24+
{
25+
$this->expectException(\InvalidArgumentException::class);
26+
$this->expectExceptionMessage('"Symfony\Component\Notifier\Message\SmsMessage" needs a phone number, it cannot be empty.');
27+
28+
new SmsMessage('', 'subject');
29+
}
30+
31+
public function testSetPhone()
32+
{
33+
$message = new SmsMessage('+3312345678', 'subject');
34+
35+
$this->assertSame('+3312345678', $message->getPhone());
36+
37+
$message->phone('+4912345678');
38+
39+
$this->assertSame('+4912345678', $message->getPhone());
40+
}
41+
42+
public function testEnsureNonEmptyPhoneOnSet()
43+
{
44+
$this->expectException(\InvalidArgumentException::class);
45+
$this->expectExceptionMessage('"Symfony\Component\Notifier\Message\SmsMessage" needs a phone number, it cannot be empty.');
46+
47+
$message = new SmsMessage('+3312345678', 'subject');
48+
49+
$this->assertSame('+3312345678', $message->getPhone());
50+
51+
$message->phone('');
52+
}
53+
}

src/Symfony/Component/Notifier/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
],
1818
"require": {
1919
"php": ">=7.2.5",
20-
"symfony/polyfill-php80": "^1.15"
20+
"symfony/polyfill-php80": "^1.15",
21+
"psr/log": "~1.0"
2122
},
2223
"conflict": {
2324
"symfony/http-kernel": "<4.4",

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy