Skip to content

Commit 16d2fd0

Browse files
author
Christoph Rumpel
authored
Merge pull request #24 from christophrumpel/feature/addMessageLimitHandling
Throw exception if status message character count is exceeded
2 parents b1c59ca + f5e3d2d commit 16d2fd0

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"php": ">=5.6.4",
1616
"abraham/twitteroauth": "^0.6.4",
1717
"illuminate/notifications": "5.3.*",
18-
"illuminate/support": "5.1.*|5.2.*|5.3.*"
18+
"illuminate/support": "5.1.*|5.2.*|5.3.*",
19+
"kylewm/brevity": "^0.2.9"
1920
},
2021
"require-dev": {
2122
"mockery/mockery": "^0.9.5",

src/Exceptions/CouldNotSendNotification.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ public static function serviceRespondedWithAnError($response)
1010

1111
return new static("Couldn't post Notification. Response: ".$responseBody);
1212
}
13+
14+
public static function statusUpdateTooLong($exceededLength)
15+
{
16+
return new static("Couldn't post Notification, because the status message was too long by " .
17+
$exceededLength . " character(s).");
18+
}
1319
}

src/TwitterStatusUpdate.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace NotificationChannels\Twitter;
44

5+
use Kylewm\Brevity\Brevity;
6+
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification;
57
use NotificationChannels\Twitter\TwitterImage;
68

79
class TwitterStatusUpdate
810
{
11+
912
/** @var string */
1013
protected $content;
1114

@@ -29,6 +32,9 @@ class TwitterStatusUpdate
2932
*/
3033
public function __construct($content)
3134
{
35+
if ($exceededLength = $this->messageIsTooLong($content, new Brevity())) {
36+
throw CouldNotSendNotification::statusUpdateTooLong($exceededLength);
37+
}
3238
$this->content = $content;
3339
}
3440

@@ -38,10 +44,12 @@ public function __construct($content)
3844
* @param array|string $images
3945
* @return $this
4046
*/
41-
public function withImage($images){
42-
collect($images)->each(function($image){
47+
public function withImage($images)
48+
{
49+
collect($images)->each(function ($image) {
4350
$this->images[] = new TwitterImage($image);
4451
});
52+
4553
return $this;
4654
}
4755

@@ -90,4 +98,18 @@ public function getRequestBody()
9098

9199
return $body;
92100
}
101+
102+
/**
103+
* Check if the message length is too long
104+
* @param $content
105+
* @param $brevity
106+
* @return int
107+
*/
108+
private function messageIsTooLong($content, $brevity)
109+
{
110+
$tweetLength = $brevity->tweetLength($content);
111+
$exceededLength = $tweetLength - 140;
112+
113+
return $exceededLength > 0 ? $exceededLength : 0;
114+
}
93115
}

tests/TwitterChannelTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification()
8585

8686
$this->channel->send(new TestNotifiable(), new TestNotification());
8787
}
88-
8988
}
9089

9190

tests/TwitterStatusUpdateTest.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace NotificationChannels\Twitter\Test;
44

5+
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification;
56
use NotificationChannels\Twitter\TwitterImage;
67
use NotificationChannels\Twitter\TwitterStatusUpdate;
78

89
class TwitterStatusUpdateTest extends \PHPUnit_Framework_TestCase
910
{
11+
1012
/** @var TwitterStatusUpdate */
1113
protected $message;
1214

@@ -32,15 +34,17 @@ public function it_accepts_one_image_path()
3234
$message = (new TwitterStatusUpdate('myMessage'))->withImage('image1.png');
3335

3436
$this->assertEquals('myMessage', $message->getContent());
35-
$this->assertEquals([ new TwitterImage('image1.png') ], $message->getImages());
37+
$this->assertEquals([new TwitterImage('image1.png')], $message->getImages());
3638
}
3739

3840
/** @test */
3941
public function it_accepts_array_of_image_paths()
4042
{
4143
$imagePaths = ['path1', 'path2'];
4244
$message = (new TwitterStatusUpdate('myMessage'))->withImage($imagePaths);
43-
$imagePathsObjects = collect($imagePaths)->map(function($image){ return new TwitterImage($image); })->toArray();
45+
$imagePathsObjects = collect($imagePaths)->map(function ($image) {
46+
return new TwitterImage($image);
47+
})->toArray();
4448

4549
$this->assertEquals('myMessage', $message->getContent());
4650
$this->assertEquals($imagePathsObjects, $message->getImages());
@@ -57,4 +61,41 @@ public function it_constructs_a_request_body()
5761
'media_ids' => '434,435,436',
5862
]);
5963
}
64+
65+
/** @test */
66+
public function it_throws_an_exception_when_the_status_update_is_too_long()
67+
{
68+
$tooLongMessage = 'Laravel Notification Channels are awesome and this message is far too long for a Twitter
69+
status update and this is why an exception is thrown!';
70+
71+
try {
72+
$statusUpdate = new TwitterStatusUpdate($tooLongMessage);
73+
} catch (CouldNotSendNotification $e) {
74+
$this->assertEquals(CouldNotSendNotification::class, get_class($e));
75+
}
76+
}
77+
78+
/** @test */
79+
public function it_provides_exceeded_message_count_when_the_status_update_is_too_long()
80+
{
81+
$tooLongMessage = 'Laravel Notification Channels are awesome and this message is far too long for a Twitter status update because of this URL https://github.com/laravel-notification-channels';
82+
83+
try {
84+
$statusUpdate = new TwitterStatusUpdate($tooLongMessage);
85+
} catch (CouldNotSendNotification $e) {
86+
$this->assertEquals("Couldn't post Notification, because the status message was too long by 6 character(s).",
87+
$e->getMessage());
88+
}
89+
90+
$anotherTooLongMessage = 'Laravel Notification Channels are awesome and this message is just in length so that Twitter does not complain!!!!!!! https://github.com/laravel-notification-channels';
91+
92+
try {
93+
$statusUpdate = new TwitterStatusUpdate($anotherTooLongMessage);
94+
} catch (CouldNotSendNotification $e) {
95+
$this->assertEquals("Couldn't post Notification, because the status message was too long by 1 character(s).",
96+
$e->getMessage());
97+
}
98+
99+
}
100+
60101
}

0 commit comments

Comments
 (0)