Skip to content

Commit d3c9c98

Browse files
committed
Sender not sent if not set and specific tests
1 parent 8319b7d commit d3c9c98

File tree

4 files changed

+82
-57
lines changed

4 files changed

+82
-57
lines changed

src/Sns.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,26 @@ public function __construct(SnsService $snsService)
2525
*/
2626
public function send(SnsMessage $message, $destination)
2727
{
28-
$parameters = [
29-
'Message' => $message->getBody(),
30-
'PhoneNumber' => $destination,
31-
'MessageAttributes' => [
28+
$attributes = [
29+
'AWS.SNS.SMS.SMSType' => [
30+
'DataType' => 'String',
31+
'StringValue' => $message->getDeliveryType(),
32+
],
33+
];
34+
35+
if (! empty($message->getSender())) {
36+
$attributes += [
3237
'AWS.SNS.SMS.SenderID' => [
3338
'DataType' => 'String',
34-
'StringValue' => $message->getSenderID(),
39+
'StringValue' => $message->getSender(),
3540
],
36-
'AWS.SNS.SMS.SMSType' => [
37-
'DataType' => 'String',
38-
'StringValue' => $message->getDeliveryType(),
39-
],
40-
],
41+
];
42+
}
43+
44+
$parameters = [
45+
'Message' => $message->getBody(),
46+
'PhoneNumber' => $destination,
47+
'MessageAttributes' => $attributes,
4148
];
4249

4350
return $this->snsService->publish($parameters);

src/SnsMessage.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ class SnsMessage
99
const TRANSACTIONAL_SMS_TYPE = 'Transactional';
1010

1111
/**
12-
* The default delivery type for the SMS message.
12+
* The body of the message.
1313
*
14-
* @var bool
14+
* @var string
1515
*/
16-
protected $promotional = true;
16+
protected $body = '';
1717

1818
/**
19-
* The default Sender ID for the SMS message.
19+
* The delivery type of the message.
2020
*
21-
* @var string
21+
* @var bool
2222
*/
23-
protected $senderID = 'NOTICE';
23+
protected $promotional = true;
2424

2525
/**
26-
* The body of the message.
26+
* The sender identification of the message.
2727
*
2828
* @var string
2929
*/
30-
protected $body = '';
30+
protected $sender = '';
3131

3232
public function __construct($content)
3333
{
@@ -79,27 +79,7 @@ public function getBody()
7979
}
8080

8181
/**
82-
* Get the SMS delivery type.
83-
*
84-
* @return string
85-
*/
86-
public function getDeliveryType()
87-
{
88-
return $this->promotional ? self::PROMOTIONAL_SMS_TYPE : self::TRANSACTIONAL_SMS_TYPE;
89-
}
90-
91-
/**
92-
* Get the SMS Sender ID.
93-
*
94-
* @return string
95-
*/
96-
public function getSenderID()
97-
{
98-
return $this->senderID;
99-
}
100-
101-
/**
102-
* Sets the SMS delivery type as promotional.
82+
* Sets the message delivery type as promotional.
10383
*
10484
* @param bool $active
10585
* @return $this
@@ -112,7 +92,7 @@ public function promotional(bool $active = true)
11292
}
11393

11494
/**
115-
* Sets the SMS delivery type as transactional.
95+
* Sets the message delivery type as transactional.
11696
*
11797
* @param bool $active
11898
* @return $this
@@ -125,15 +105,35 @@ public function transactional(bool $active = true)
125105
}
126106

127107
/**
128-
* Sets the SMS Sender ID.
108+
* Get the message delivery type.
109+
*
110+
* @return string
111+
*/
112+
public function getDeliveryType()
113+
{
114+
return $this->promotional ? self::PROMOTIONAL_SMS_TYPE : self::TRANSACTIONAL_SMS_TYPE;
115+
}
116+
117+
/**
118+
* Sets the message sender identification.
129119
*
130120
* @param string $sender
131121
* @return $this
132122
*/
133-
public function sender(string $sender = 'NOTICE')
123+
public function sender(string $sender)
134124
{
135-
$this->senderID = $sender;
125+
$this->sender = $sender;
136126

137127
return $this;
138128
}
129+
130+
/**
131+
* Get the message sender identification.
132+
*
133+
* @return string
134+
*/
135+
public function getSender()
136+
{
137+
return $this->sender;
138+
}
139139
}

tests/SnsMessageTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ public function the_sms_delivery_type_can_be_explicitly_as_promotional()
6262
}
6363

6464
/** @test */
65-
public function the_default_sms_sender_id_is_notice()
65+
public function the_default_sms_sender_id_is_empty()
6666
{
6767
$message = SnsMessage::create();
68-
$this->assertEquals('NOTICE', $message->getSenderID());
68+
$this->assertEmpty($message->getSender());
6969
}
7070

7171
/** @test */
7272
public function the_sms_sender_id_can_be_changed_using_a_proper_method()
7373
{
7474
$message = SnsMessage::create()->sender('Test');
75-
$this->assertEquals('Test', $message->getSenderID());
75+
$this->assertEquals('Test', $message->getSender());
7676
}
7777

7878
/** @test */
@@ -85,6 +85,6 @@ public function it_can_accept_all_the_contents_when_constructing_a_message()
8585
]);
8686
$this->assertEquals('My mass body', $message->getBody());
8787
$this->assertEquals('Transactional', $message->getDeliveryType());
88-
$this->assertEquals('Test', $message->getSenderID());
88+
$this->assertEquals('Test', $message->getSender());
8989
}
9090
}

tests/SnsTest.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,14 @@ protected function setUp(): void
4141
/** @test */
4242
public function it_can_send_a_promotional_sms_message_to_sns()
4343
{
44-
$message = new SnsMessage(['body' => 'Message text', 'sender' => 'Test']);
44+
$message = new SnsMessage('Message text');
4545

4646
$this->snsService->shouldReceive('publish')
4747
->atLeast()->once()
4848
->with([
4949
'Message' => 'Message text',
5050
'PhoneNumber' => '+1111111111',
5151
'MessageAttributes' => [
52-
'AWS.SNS.SMS.SenderID' => [
53-
'DataType' => 'String',
54-
'StringValue' => 'Test',
55-
],
5652
'AWS.SNS.SMS.SMSType' => [
5753
'DataType' => 'String',
5854
'StringValue' => 'Promotional',
@@ -67,18 +63,14 @@ public function it_can_send_a_promotional_sms_message_to_sns()
6763
/** @test */
6864
public function it_can_send_a_transactional_sms_message_to_sns()
6965
{
70-
$message = new SnsMessage(['body' => 'Message text', 'sender' => 'Test', 'transactional' => true]);
66+
$message = new SnsMessage(['body' => 'Message text', 'transactional' => true]);
7167

7268
$this->snsService->shouldReceive('publish')
7369
->atLeast()->once()
7470
->with([
7571
'Message' => 'Message text',
7672
'PhoneNumber' => '+22222222222',
7773
'MessageAttributes' => [
78-
'AWS.SNS.SMS.SenderID' => [
79-
'DataType' => 'String',
80-
'StringValue' => 'Test',
81-
],
8274
'AWS.SNS.SMS.SMSType' => [
8375
'DataType' => 'String',
8476
'StringValue' => 'Transactional',
@@ -89,4 +81,30 @@ public function it_can_send_a_transactional_sms_message_to_sns()
8981

9082
$this->sns->send($message, '+22222222222');
9183
}
84+
85+
/** @test */
86+
public function it_can_send_a_sms_message_with_sender_id()
87+
{
88+
$message = new SnsMessage(['body' => 'Message text', 'sender' => 'CompanyInc']);
89+
90+
$this->snsService->shouldReceive('publish')
91+
->atLeast()->once()
92+
->with([
93+
'Message' => 'Message text',
94+
'PhoneNumber' => '+33333333333',
95+
'MessageAttributes' => [
96+
'AWS.SNS.SMS.SMSType' => [
97+
'DataType' => 'String',
98+
'StringValue' => 'Promotional',
99+
],
100+
'AWS.SNS.SMS.SenderID' => [
101+
'DataType' => 'String',
102+
'StringValue' => 'CompanyInc',
103+
],
104+
],
105+
])
106+
->andReturn(true);
107+
108+
$this->sns->send($message, '+33333333333');
109+
}
92110
}

0 commit comments

Comments
 (0)