Skip to content

Commit c1dcfea

Browse files
Merge pull request #97 from Nelwhix/master
migrating to V2 of twitter api
2 parents 12ce3fc + dd16c40 commit c1dcfea

8 files changed

+56
-47
lines changed

src/Exceptions/CouldNotSendNotification.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public static function serviceRespondsNotSuccessful(mixed $response): CouldNotSe
1111
if (isset($response->error)) {
1212
return new static("Couldn't post notification. Response: ".$response->error);
1313
}
14-
15-
$responseBody = print_r($response->errors[0]->message, true);
14+
15+
$responseBody = print_r($response->detail, true);
1616

1717
return new static("Couldn't post notification. Response: ".$responseBody);
1818
}
@@ -27,7 +27,7 @@ public static function userWasNotFound(mixed $response): CouldNotSendNotificatio
2727
public static function statusUpdateTooLong(int $exceededLength): CouldNotSendNotification
2828
{
2929
return new static(
30-
"Couldn't post notification, because the status message was too long by ${exceededLength} character(s)."
30+
"Couldn't post notification, because the status message was too long by $exceededLength character(s)."
3131
);
3232
}
3333

src/TwitterChannel.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ public function send($notifiable, Notification $notification): array|object
3232
} else {
3333
$requestBody = $twitterMessage->getRequestBody();
3434
}
35+
36+
// api V2 does not support sending media yet, so I wait till after sending media to switch api version
37+
$this->twitter->setApiVersion('2');
3538
$twitterApiResponse = $this->twitter->post(
3639
$twitterMessage->getApiEndpoint(),
3740
$requestBody,
3841
$twitterMessage->isJsonRequest,
3942
);
40-
41-
if ($this->twitter->getLastHttpCode() !== 200) {
43+
44+
if ($this->twitter->getLastHttpCode() !== 201) {
4245
throw CouldNotSendNotification::serviceRespondsNotSuccessful($this->twitter->getLastBody());
4346
}
4447

src/TwitterMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
abstract class TwitterMessage
66
{
7-
public bool $isJsonRequest = false;
7+
public bool $isJsonRequest = true;
88

99
public function __construct(protected string $content)
1010
{

src/TwitterServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ public function boot()
1515
$this->app->when([TwitterChannel::class, TwitterDirectMessage::class])
1616
->needs(TwitterOAuth::class)
1717
->give(function () {
18-
return new TwitterOAuth(
18+
$connection = new TwitterOAuth(
1919
config('services.twitter.consumer_key'),
2020
config('services.twitter.consumer_secret'),
2121
config('services.twitter.access_token'),
2222
config('services.twitter.access_secret')
2323
);
24+
25+
return $connection;
2426
});
2527
}
2628
}

src/TwitterStatusUpdate.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TwitterStatusUpdate extends TwitterMessage
1212
public ?Collection $videoIds = null;
1313
private ?array $images = null;
1414
private ?array $videos = null;
15-
private ?int $inReplyToStatusId = null;
15+
private ?int $inReplyToTweetId = null;
1616

1717
/**
1818
* @throws CouldNotSendNotification
@@ -28,7 +28,7 @@ public function __construct(string $content)
2828

2929
public function getApiEndpoint(): string
3030
{
31-
return 'statuses/update';
31+
return 'tweets';
3232
}
3333

3434
/**
@@ -83,40 +83,41 @@ public function getVideos(): ?array
8383
* @param int $statusId
8484
* @return $this
8585
*/
86-
public function inReplyTo(int $statusId): self
86+
public function inReplyTo(int $tweetId): self
8787
{
88-
$this->inReplyToStatusId = $statusId;
88+
$this->inReplyToTweetId = $tweetId;
8989

9090
return $this;
9191
}
9292

9393
/**
9494
* @return int|null
9595
*/
96-
public function getInReplyToStatusId(): ?int
96+
public function getInReplyToTweetId(): ?int
9797
{
98-
return $this->inReplyToStatusId;
98+
return $this->inReplyToTweetId;
9999
}
100100

101101
/**
102102
* Build Twitter request body.
103103
*/
104104
public function getRequestBody(): array
105105
{
106-
$body = ['status' => $this->getContent()];
106+
$body = ['text' => $this->getContent()];
107107

108108
$mediaIds = collect()
109109
->merge($this->imageIds instanceof Collection ? $this->imageIds : [])
110110
->merge($this->videoIds instanceof Collection ? $this->videoIds : [])
111-
->filter()
112111
->values();
113112

114113
if ($mediaIds->count() > 0) {
115-
$body['media_ids'] = $mediaIds->implode(',');
114+
$body['media'] = [
115+
'media_ids' => $mediaIds->toArray()
116+
];
116117
}
117118

118-
if ($this->inReplyToStatusId) {
119-
$body['in_reply_to_status_id'] = $this->inReplyToStatusId;
119+
if ($this->inReplyToTweetId) {
120+
$body['in_reply_to_tweet_id'] = $this->inReplyToTweetId;
120121
}
121122

122123
return $body;

tests/TwitterChannelTest.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ class TwitterChannelTest extends TestCase
2424
public function setUp(): void
2525
{
2626
parent::setUp();
27-
$this->twitter = m::mock(TwitterOAuth::class);
27+
$this->twitter = m::mock(TwitterOAuth::class, function ($mock) {
28+
$mock->shouldReceive('setApiVersion')
29+
->with('2');
30+
});
2831
$this->channel = new TwitterChannel($this->twitter);
2932
}
3033

@@ -33,13 +36,13 @@ public function it_can_send_a_status_update_notification()
3336
{
3437
$this->twitter->shouldReceive('post')
3538
->once()
36-
->with('statuses/update', ['status' => 'Laravel Notification Channels are awesome!'], false)
39+
->with('tweets', ['text' => 'Laravel Notification Channels are awesome!'], true)
3740
->andReturn([]);
3841

3942
$this->twitter->shouldReceive('getLastHttpCode')
4043
->once()
41-
->andReturn(200);
42-
44+
->andReturn(201);
45+
4346
$this->channel->send(new TestNotifiable(), new TestNotification());
4447
}
4548

@@ -56,9 +59,9 @@ public function it_can_send_a_status_update_notification_with_images()
5659
$this->twitter->shouldReceive('post')
5760
->once()
5861
->with(
59-
'statuses/update',
60-
['status' => 'Laravel Notification Channels are awesome!', 'media_ids' => '2'],
61-
false
62+
'tweets',
63+
['text' => 'Laravel Notification Channels are awesome!', 'media' => [ 'media_ids' => [2]]],
64+
true
6265
)
6366
->andReturn([]);
6467

@@ -69,7 +72,7 @@ public function it_can_send_a_status_update_notification_with_images()
6972

7073
$this->twitter->shouldReceive('getLastHttpCode')
7174
->once()
72-
->andReturn(200);
75+
->andReturn(201);
7376

7477
$this->channel->send(new TestNotifiable(), new TestNotificationWithImage());
7578
}
@@ -92,9 +95,9 @@ public function it_can_send_a_status_update_notification_with_videos()
9295
$this->twitter->shouldReceive('post')
9396
->once()
9497
->with(
95-
'statuses/update',
96-
['status' => 'Laravel Notification Channels are awesome!', 'media_ids' => '2'],
97-
false
98+
'tweets',
99+
['text' => 'Laravel Notification Channels are awesome!', 'media' => [ 'media_ids' => [2]]],
100+
true
98101
)
99102
->andReturn([]);
100103

@@ -114,42 +117,40 @@ public function it_can_send_a_status_update_notification_with_videos()
114117

115118
$this->twitter->shouldReceive('getLastHttpCode')
116119
->once()
117-
->andReturn(200);
120+
->andReturn(201);
118121

119122
$this->channel->send(new TestNotifiable(), new TestNotificationWithVideo());
120123
}
121124

122125
/** @test */
123-
public function it_can_send_a_status_update_notification_with_reply_to_status_id(): void
126+
public function it_can_send_a_status_update_notification_with_reply_to_tweet_id(): void
124127
{
125128
$postParams = [
126-
'status' => 'Laravel Notification Channels are awesome!',
127-
'in_reply_to_status_id' => $replyToStatusId = 123,
129+
'text' => 'Laravel Notification Channels are awesome!',
130+
'in_reply_to_tweet_id' => $replyToStatusId = 123,
128131
];
129132

130133
$this->twitter->shouldReceive('post')
131134
->once()
132-
->with('statuses/update', $postParams, false)
135+
->with('tweets', $postParams, true)
133136
->andReturn([]);
134137

135138
$this->twitter->shouldReceive('getLastHttpCode')
136139
->once()
137-
->andReturn(200);
140+
->andReturn(201);
138141

139142
$this->channel->send(new TestNotifiable(), new TestNotificationWithReplyToStatusId($replyToStatusId));
140143
}
141144

142145
/** @test */
143146
public function it_throws_an_exception_when_it_could_not_send_the_notification()
144147
{
145-
$messageObject = new stdClass;
146-
$messageObject->message = 'Error message';
147148
$twitterResponse = new stdClass;
148-
$twitterResponse->errors[] = $messageObject;
149+
$twitterResponse->detail = 'Error Message';
149150

150151
$this->twitter->shouldReceive('post')
151152
->once()
152-
->with('statuses/update', ['status' => 'Laravel Notification Channels are awesome!'], false);
153+
->with('tweets', ['text' => 'Laravel Notification Channels are awesome!'], true);
153154

154155
$this->twitter->shouldReceive('getLastHttpCode')
155156
->once()
@@ -194,7 +195,7 @@ public function it_throws_an_exception_when_it_could_not_send_the_notification_w
194195
->once()
195196
->with($media->media_id_string)
196197
->andReturn($status);
197-
198+
198199
$this->expectException(CouldNotSendNotification::class);
199200

200201
$this->channel->send(new TestNotifiable(), new TestNotificationWithVideo());

tests/TwitterMessageTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ public function getApiEndpoint(): string
3636
}
3737

3838
/** @test */
39-
public function it_has_an_is_json_request_property_with_default_value_false()
39+
public function it_has_an_is_json_request_property_with_default_value_true()
4040
{
4141
$message = new class('Foo content') extends TwitterMessage
4242
{
4343
public function getApiEndpoint(): string
4444
{
45-
return 'status/update';
45+
return 'tweets';
4646
}
4747
};
4848

4949
$this->assertObjectHasAttribute('isJsonRequest', $message);
50-
$this->assertFalse($message->isJsonRequest);
50+
$this->assertTrue($message->isJsonRequest);
5151
}
5252
}

tests/TwitterStatusUpdateTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ public function it_constructs_a_request_body(): void
8787
$message->videoIds = collect([534, 535, 536]);
8888

8989
$this->assertEquals([
90-
'status' => 'myMessage',
91-
'media_ids' => '434,435,436,534,535,536',
90+
'text' => 'myMessage',
91+
'media' => [
92+
'media_ids' => [434,435,436,534,535,536]
93+
]
9294
], $message->getRequestBody());
9395
}
9496

@@ -131,7 +133,7 @@ public function it_has_in_reply_to_status_id_as_optional_parameter(): void
131133
{
132134
$message = new TwitterStatusUpdate('Hello world!');
133135

134-
$this->assertEquals(null, $message->getInReplyToStatusId());
136+
$this->assertEquals(null, $message->getInReplyToTweetId());
135137
}
136138

137139
/** @test */
@@ -141,6 +143,6 @@ public function it_accepts_in_reply_to_status_id(): void
141143
->inReplyTo($inReplyToStatusId = 12345);
142144

143145
$this->assertEquals($content, $message->getContent());
144-
$this->assertEquals($inReplyToStatusId, $message->getInReplyToStatusId());
146+
$this->assertEquals($inReplyToStatusId, $message->getInReplyToTweetId());
145147
}
146148
}

0 commit comments

Comments
 (0)