Skip to content

Commit 1ef1fee

Browse files
fetch real data
1 parent ee1b54b commit 1ef1fee

File tree

6 files changed

+39
-22
lines changed

6 files changed

+39
-22
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/composer.lock
33
/phpunit.xml
44
.phpunit.result.cache
5-
/tests/tweet.html
5+
/tests/tweet.html
6+
.env

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
],
2222
"require": {
2323
"php": "^7.4 || ^8.0",
24+
"dg/twitter-php": "^4.1",
2425
"illuminate/support": "^7.0 || ^8.0",
2526
"illuminate/view": "^7.0 || ^8.0"
2627
},

resources/views/components/tweet.blade.php

+8-19
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,17 @@
1212
<div class="relative bg-white shadow-lg rounded sm:rounded-md p-4 sm:p-6 lg:p-8 space-y-4">
1313
<header class="flex flex-row items-center space-x-2">
1414
<div>
15-
<!-- twitter.user.avatar -->
16-
<img src="/assets/logo.png" class="h-12 sm:h-16 w-12 sm:w-16" alt=" avatar">
15+
<img src="{{ $user->profile_image_url_https }}" class="h-12 sm:h-16 w-12 sm:w-16" alt=" avatar">
1716
</div>
1817
<div class="flex-grow">
1918
<div class="flex flex-row space-x-2">
20-
<!-- twitter.user.name -->
21-
<div class="text-xl text-gray-600 font-bold tracking-wide">Larabelles</div>
22-
<!-- twitter.user.handle -->
23-
<div class="text-gray-400">@LarabellesPHP</div>
19+
<div class="text-xl text-gray-600 font-bold tracking-wide">{{ $user->name }}</div>
20+
<div class="text-gray-400">{{ '@'.$user->screen_name }}</div>
2421
</div>
25-
<!-- tweet.date -->
26-
<div class="text-sm text-gray-400">8:39 AM - Feb 12, 2021</div>
22+
<div class="text-sm text-gray-400">{{ \Carbon\Carbon::parse($created_at)->toDayDateTimeString() }}</div>
2723
</div>
2824
<div>
29-
<!-- tweet.url -->
30-
<a href="#">
25+
<a href="https://twitter.com/{{ $user->screen_name }}/status/{{ $id }}">
3126
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"
3227
width="289.984" height="289.984" viewBox="0 0 289.984 289.984" xml:space="preserve" class="h-4 sm:h-6">
3328
<desc>Created with Fabric.js 1.7.22</desc>
@@ -47,19 +42,13 @@
4742

4843
<main>
4944
<p class="overflow-clip overflow-hidden inline">
50-
<!-- tweet.body -->
51-
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque convallis, turpis et
52-
hendrerit viverra, tortor mi imperdiet eros, sit amet tristique nisl ligula vitae magna.
45+
{{ $text }}
5346
</p>
54-
<!-- tweet.url -->
55-
<a class="text-blue-400" href="#">tweet.url</a>
5647
</main>
5748

5849
<footer class="space-x-4">
59-
<!-- tweet.like -->
60-
<span class="text-gray-400 text-sm">&#9825; 35</span>
61-
<!-- twitter.user.handle -->
62-
<span class="text-gray-400 text-sm"><a href="#">&#9733; See LarabellesPHPs other Tweets</a></span>
50+
<span class="text-gray-400 text-sm">&#9825; {{ $favorite_count }}</span>
51+
<span class="text-gray-400 text-sm"><a href="https://twitter.com/{{ $user->screen_name }}">&#9733; See {{ $user->name }}'s other Tweets</a></span>
6352
</footer>
6453
</div>
6554
</article>

src/View/Components/Tweet.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22

33
namespace Larabelles\TweetComponent\View\Components;
44

5+
use DG\Twitter\Twitter;
56
use Illuminate\Contracts\View\View;
67
use Illuminate\View\Component;
78

89
class Tweet extends Component
910
{
11+
protected string $id;
12+
13+
public function __construct(string $id)
14+
{
15+
$this->id = $id;
16+
}
17+
1018
public function render() : View
1119
{
12-
return view('tweet-component::components.tweet');
20+
return view('tweet-component::components.tweet', $this->retrieveData());
21+
}
22+
23+
protected function retrieveData() : array
24+
{
25+
$twitter = new Twitter(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'), env('TWITTER_ACCESS_TOKEN'), env('TWITTER_ACCESS_TOKEN_SECRET'));
26+
27+
return collect($twitter->request("statuses/show/{$this->id}", 'GET'))->dump()->toArray();
1328
}
1429
}

tests/TestCase.php

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99

1010
abstract class TestCase extends OrchestraTestCase
1111
{
12+
protected $loadEnvironmentVariables = true;
13+
14+
protected function resolveApplication()
15+
{
16+
$app = parent::resolveApplication();
17+
18+
$app->useEnvironmentPath(__DIR__.'/..');
19+
20+
return $app;
21+
}
22+
1223
protected function getPackageProviders($app) : array
1324
{
1425
return [

tests/TweetComponentTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class TweetComponentTest extends TestCase
77
/** @test */
88
public function it_renders() : void
99
{
10-
$html = $this->blade('<x-tweet />');
10+
$html = $this->blade('<x-tweet id="1372946230783934465" />');
1111

1212
file_put_contents(__DIR__.'/tweet.html', $html);
1313
}

0 commit comments

Comments
 (0)