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

Commit

Permalink
Merge branch 'feature/dm-limit' into develop
Browse files Browse the repository at this point in the history
Close #40
  • Loading branch information
weierophinney committed Aug 15, 2017
2 parents 934ab32 + 60cd1b1 commit 56ab734
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ All notable changes to this project will be documented in this file, in reverse

### Changed

- [#34](https://github.com/zendframework/ZendService_Twitter/pull/34) updates
direct message support to remove the 140 character limit.
- [#40](https://github.com/zendframework/ZendService_Twitter/pull/40) updates
direct message support to set the character limit to 10k, as documented
currently for the Twitter API.

- [#34](https://github.com/zendframework/ZendService_Twitter/pull/34) updates
the `Twitter` class to return a `ZendService\Twitter\Response` instance
Expand Down
3 changes: 0 additions & 3 deletions library/ZendService/Twitter/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
*/
class Image extends Media
{
/**
* @param $imageUrl
*/
public function __construct(string $imageUrl, string $mediaType = 'image/jpeg')
{
parent::__construct($imageUrl, $mediaType);
Expand Down
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 56ab734

Please sign in to comment.