Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Updates direct message creation to impose a limit
Browse files Browse the repository at this point in the history
Per https://dev.twitter.com/rest/reference/post/direct_messages/events/new,
DM text can be no more than 10k characters in length.
  • Loading branch information
weierophinney committed Aug 15, 2017
1 parent 2e29a15 commit 321f56b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
8 changes: 7 additions & 1 deletion library/ZendService/Twitter/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,18 @@ public function directMessagesNew($user, $text)
$path = 'direct_messages/new';

$len = iconv_strlen($text, 'UTF-8');
if (0 == $len) {
if (0 === $len) {
throw new Exception\InvalidArgumentException(
'Direct message must contain at least one character'
);
}

if (10000 < $len) {
throw new Exception\InvalidArgumentException(
'Direct message must be no more than 10000 characters'
);
}

$params = $this->createUserParameter($user, []);
$params['text'] = $text;
$response = $this->post($path, $params);
Expand Down
29 changes: 29 additions & 0 deletions tests/ZendService/Twitter/TwitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ protected function stubOAuthClient(
return $client->reveal();
}

public function stubHttpClientInitialization()
{
$client = $this->prophesize(OAuthClient::class);
$client->setHeaders(['Accept-Charset' => 'ISO-8859-1,utf-8'])->will([$client, 'reveal']);
$client->resetParameters()->will([$client, 'reveal']);
$client->clearCookies()->will([$client, 'reveal']);
$client->getCookies()->willReturn([]);
return $client->reveal();
}

public function testRateLimitHeaders()
{
$rateLimits = [
Expand Down Expand Up @@ -794,4 +804,23 @@ public function testAdapterAlwaysReachableIfSpecified($config, $adapter)
$twitter = new Twitter\Twitter($config);
$this->assertSame($adapter, $twitter->getHttpClient()->getAdapter());
}

public function testDirectMessagesNewRaisesExceptionForEmptyMessage()
{
$twitter = new Twitter\Twitter();
$twitter->setHttpClient($this->stubHttpClientInitialization());
$this->expectException(Twitter\Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('at least one char');
$twitter->directMessagesNew('twitter', '');
}

public function testDirectMessagesNewRaisesExceptionForTooLongOfMessage()
{
$twitter = new Twitter\Twitter();
$twitter->setHttpClient($this->stubHttpClientInitialization());
$text = str_pad('', 10001, 'X');
$this->expectException(Twitter\Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('no more than 10000 char');
$twitter->directMessagesNew('twitter', $text);
}
}

0 comments on commit 321f56b

Please sign in to comment.