Skip to content

Commit

Permalink
Merge pull request #3 from martin-georgiev/feature/append-link-to-tweet
Browse files Browse the repository at this point in the history
Embed new link to the tweet
  • Loading branch information
martin-georgiev committed Mar 22, 2017
2 parents f1d4ab5 + 2158fdf commit 4ae8024
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Expand Up @@ -45,11 +45,33 @@ public function publish(
string $description = ''
): bool {
try {
$post = $this->twitter->post('statuses/update', ['status' => $message, 'trim_user' => true]);
$status = $this->prepareStatus($message, $link);
$post = $this->twitter->post('statuses/update', ['status' => $status, 'trim_user' => true]);

return !empty($post->id_str);
} catch (Throwable $t) {
throw new FailureWhenPublishingSocialPost($t);
}
}

/**
* @param string $message
* @param string $link
* @return string
*/
protected function prepareStatus(
string $message,
string $link
): string {
$status = $message;

if (filter_var($link, FILTER_VALIDATE_URL) !== false) {
$linkIsNotPartOfTheMessage = mb_strpos($message, $link) === false;
if ($linkIsNotPartOfTheMessage) {
$status .= ' ' . $link;
}
}

return $status;
}
}
Expand Up @@ -40,6 +40,30 @@ public function test_can_successfully_publish_a_tweet()
$this->assertTrue($twitterProvider->publish($message));
}

public function test_can_successfully_publish_a_tweet_with_a_link()
{
$twitterOAuth = $this
->getMockBuilder(TwitterOAuth::class)
->disableOriginalConstructor()
->setMethods(['post'])
->getMock();

$message = 'test tweet';
$link = 'https://www.example.com';
$endpoint = 'statuses/update';
$status = $message . ' ' . $link;
$data = ['status' => $status, 'trim_user' => true];
$tweet = (object)['id_str' => '2007'];
$twitterOAuth
->expects($this->once())
->method('post')
->with($endpoint, $data)
->willReturn($tweet);

$twitterProvider = new TwitterOAuth07($twitterOAuth);
$this->assertTrue($twitterProvider->publish($message, $link));
}

public function test_will_fail_if_cannot_find_the_id_of_the_new_tweet()
{
$twitterOAuth = $this
Expand Down

0 comments on commit 4ae8024

Please sign in to comment.