Skip to content

Commit

Permalink
fix: handle array in formatting helpers + tests (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
atymic authored Apr 14, 2021
1 parent 6761cda commit 7f43558
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 5 deletions.
65 changes: 60 additions & 5 deletions src/ApiV1/Traits/FormattingHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,83 @@ public function ago($timestamp): string
}

// todo redo these helpers

/**
* @param object|array|string $user
*
* @return string
*/
public function linkUser($user): string
{
return 'https://twitter.com/' . (is_object($user) ? $user->screen_name : $user);
$screenName = is_string($user) ? $user : $this->objectToArray($user)['screen_name'];

return 'https://twitter.com/' . $screenName;
}

/**
* @param object|array $tweet
*
* @return string
*/
public function linkTweet($tweet): string
{
return $this->linkUser($tweet->user) . '/status/' . $tweet->id_str;
$tweet = $this->objectToArray($tweet);

return $this->linkUser($tweet['user']) . '/status/' . $tweet['id_str'];
}

/**
* @param object|array $tweet
*
* @return string
*/
public function linkRetweet($tweet): string
{
return 'https://twitter.com/intent/retweet?tweet_id=' . $tweet->id_str;
$tweet = $this->objectToArray($tweet);

return 'https://twitter.com/intent/retweet?tweet_id=' . $tweet['id_str'];
}

/**
* @param object|array $tweet
*
* @return string
*/
public function linkAddTweetToFavorites($tweet): string
{
return 'https://twitter.com/intent/favorite?tweet_id=' . $tweet->id_str;
$tweet = $this->objectToArray($tweet);

return 'https://twitter.com/intent/favorite?tweet_id=' . $tweet['id_str'];
}

/**
* @param object|array $tweet
*
* @return string
*/
public function linkReply($tweet): string
{
return 'https://twitter.com/intent/tweet?in_reply_to=' . $tweet->id_str;
$tweet = $this->objectToArray($tweet);

return 'https://twitter.com/intent/tweet?in_reply_to=' . $tweet['id_str'];
}

/**
* @param $data
*
* @return array|mixed
*/
protected function objectToArray($data)
{
if (is_array($data)) {
return $data;
}

if (is_object($data)) {
return json_decode(json_encode($data), true);
}

// Fallback for non objects
return $data;
}
}
26 changes: 26 additions & 0 deletions tests/Unit/ApiV1/Traits/ConcernTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Atymic\Twitter\Tests\Unit\ApiV1\Traits;

use Atymic\Twitter\Tests\Unit\AccessorTestCase;
use Exception;
use PHPUnit\Framework\MockObject\MockObject;

abstract class ConcernTestCase extends AccessorTestCase
{
protected MockObject $subject;

/**
* @throws Exception
*/
protected function setUp(): void
{
parent::setUp();

$this->subject = $this->getMockForTrait($this->getTraitName());
}

abstract protected function getTraitName(): string;
}
57 changes: 57 additions & 0 deletions tests/Unit/ApiV1/Traits/FormattingHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* @noinspection PhpParamsInspection
*/

declare(strict_types=1);

namespace Atymic\Twitter\Tests\Unit\ApiV1\Traits;

use Atymic\Twitter\ApiV1\Traits\FormattingHelpers;

final class FormattingHelpersTest extends ConcernTestCase
{
/**
* @dataProvider dataGetUserLink
*/
public function testGetUserLink($user): void
{
$this->assertSame(
'https://twitter.com/atymic',
$this->subject->linkUser($user)
);
}

public function dataGetUserLink(): array
{
return [
'string' => ['atymic'],
'object' => [(object) ['screen_name' => 'atymic']],
'array' => [['screen_name' => 'atymic']],
];
}

/**
* @dataProvider dataLinkAddTweetToFavorites
*/
public function testLinkAddTweetToFavorites($tweet): void
{
$this->assertSame(
'https://twitter.com/intent/favorite?tweet_id=1381031025053155332',
$this->subject->linkAddTweetToFavorites($tweet)
);
}

public function dataLinkAddTweetToFavorites(): array
{
return [
'object' => [(object) ['id_str' => '1381031025053155332']],
'array' => [['id_str' => '1381031025053155332']],
];
}

protected function getTraitName(): string
{
return FormattingHelpers::class;
}
}

0 comments on commit 7f43558

Please sign in to comment.