Skip to content

Commit 85453da

Browse files
add tests and improve container binding
1 parent 1ef1fee commit 85453da

6 files changed

+85
-7
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
},
2828
"require-dev": {
2929
"orchestra/testbench": "^5.0 || ^6.0",
30-
"phpunit/phpunit": "^9.3"
30+
"phpunit/phpunit": "^9.3",
31+
"spatie/phpunit-snapshot-assertions": "^4.2"
3132
},
3233
"config": {
3334
"sort-packages": true

src/TweetComponentServiceProvider.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Larabelles\TweetComponent;
44

5+
use DG\Twitter\Twitter;
56
use Illuminate\Support\ServiceProvider;
67
use Illuminate\View\Compilers\BladeCompiler;
78
use Larabelles\TweetComponent\View\Components\Tweet;
@@ -10,7 +11,14 @@ class TweetComponentServiceProvider extends ServiceProvider
1011
{
1112
public function register() : void
1213
{
13-
14+
$this->app->singleton(Twitter::class, function (): Twitter {
15+
return new Twitter(
16+
config('services.twitter.consumer_key'),
17+
config('services.twitter.consumer_secret'),
18+
config('services.twitter.access_token'),
19+
config('services.twitter.access_token_secret')
20+
);
21+
});
1422
}
1523

1624
public function boot() : void

src/View/Components/Tweet.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
class Tweet extends Component
1010
{
1111
protected string $id;
12+
protected Twitter $twitter;
1213

13-
public function __construct(string $id)
14+
public function __construct(string $id, Twitter $twitter)
1415
{
1516
$this->id = $id;
17+
$this->twitter = $twitter;
1618
}
1719

1820
public function render() : View
@@ -22,8 +24,6 @@ public function render() : View
2224

2325
protected function retrieveData() : array
2426
{
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();
27+
return collect($this->twitter->request("statuses/show/{$this->id}", 'GET'))->toArray();
2828
}
2929
}

tests/TestCase.php

+11
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,15 @@ protected function blade(string $template, array $data = []): string
4141

4242
return view(Str::before(basename($tempFile), '.blade.php'), $data)->render();
4343
}
44+
45+
protected function setUp(): void
46+
{
47+
parent::setUp();
48+
config()->set('services.twitter', [
49+
'consumer_key' => env('TWITTER_CONSUMER_KEY'),
50+
'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
51+
'access_token' => env('TWITTER_ACCESS_TOKEN'),
52+
'access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'),
53+
]);
54+
}
4455
}

tests/TweetComponentTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
namespace Larabelles\TweetComponent\Tests;
44

5+
use Spatie\Snapshots\MatchesSnapshots;
6+
57
class TweetComponentTest extends TestCase
68
{
9+
use MatchesSnapshots;
10+
711
/** @test */
812
public function it_renders() : void
913
{
1014
$html = $this->blade('<x-tweet id="1372946230783934465" />');
1115

12-
file_put_contents(__DIR__.'/tweet.html', $html);
16+
$this->assertMatchesHtmlSnapshot($html);
17+
1318
}
1419
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<link href="https://unpkg.com/tailwindcss@%5E2/dist/tailwind.min.css" rel="stylesheet">
7+
</head>
8+
<body class="min-h-screen bg-gray-100 flex flex-col justify-center">
9+
10+
<section class="relative py-3 px-2">
11+
<article class="max-w-full sm:max-w-2xl sm:mx-auto">
12+
<div class="relative bg-white shadow-lg rounded sm:rounded-md p-4 sm:p-6 lg:p-8 space-y-4">
13+
<header class="flex flex-row items-center space-x-2">
14+
<div>
15+
<img src="https://pbs.twimg.com/profile_images/1353128720270290946/IG1iQCUQ_normal.jpg" class="h-12 sm:h-16 w-12 sm:w-16" alt=" avatar">
16+
</div>
17+
<div class="flex-grow">
18+
<div class="flex flex-row space-x-2">
19+
<div class="text-xl text-gray-600 font-bold tracking-wide">Zuzana</div>
20+
<div class="text-gray-400">@zuzana_kunckova</div>
21+
</div>
22+
<div class="text-sm text-gray-400">Fri, Mar 19, 2021 4:21 PM</div>
23+
</div>
24+
<div>
25+
<a href="https://twitter.com/zuzana_kunckova/status/1372946230783934465">
26+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="289.984" height="289.984" viewbox="0 0 289.984 289.984" xml:space="preserve" class="h-4 sm:h-6">
27+
<desc>Created with Fabric.js 1.7.22</desc>
28+
<defs>
29+
</defs>
30+
<g id="icon" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: none; fill-rule: nonzero; opacity: 1;" transform="translate(-1.611022222222232 -1.611022222222232) scale(3.22 3.22)">
31+
<path d="M 28.303 81.565 c 33.962 0 52.538 -28.138 52.538 -52.538 c 0 -0.799 0 -1.595 -0.054 -2.387 c 3.614 -2.614 6.733 -5.85 9.212 -9.558 c -3.37 1.493 -6.945 2.473 -10.606 2.905 c 3.855 -2.308 6.74 -5.937 8.118 -10.213 c -3.625 2.151 -7.59 3.667 -11.725 4.482 c -6.993 -7.436 -18.69 -7.795 -26.126 -0.802 c -4.796 4.51 -6.83 11.23 -5.342 17.643 C 29.473 30.352 15.64 23.34 6.264 11.804 c -4.901 8.437 -2.398 19.231 5.717 24.649 c -2.939 -0.087 -5.813 -0.88 -8.381 -2.311 c 0 0.076 0 0.155 0 0.234 c 0.002 8.79 6.198 16.36 14.814 18.101 c -2.718 0.741 -5.571 0.85 -8.338 0.317 c 2.419 7.522 9.351 12.675 17.251 12.823 c -6.539 5.139 -14.616 7.928 -22.932 7.92 C 2.926 73.534 1.459 73.445 0 73.27 c 8.444 5.419 18.27 8.293 28.303 8.28" style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: rgb(29,161,242); fill-rule: nonzero; opacity: 1;" transform=" matrix(1 0 0 1 0 0) " stroke-linecap="round"></path>
32+
</g>
33+
</svg>
34+
</a>
35+
</div>
36+
</header>
37+
38+
<main>
39+
<p class="overflow-clip overflow-hidden inline">
40+
I *think* I finally understood what Laravel's service container is all about&#129395;. Only took me ... a long time &#128517;.
41+
</p>
42+
</main>
43+
44+
<footer class="space-x-4">
45+
<span class="text-gray-400 text-sm">&#9825; 14</span>
46+
<span class="text-gray-400 text-sm"><a href="https://twitter.com/zuzana_kunckova">&#9733; See Zuzana's other Tweets</a></span>
47+
</footer>
48+
</div>
49+
</article>
50+
</section>
51+
52+
</body>
53+
</html>

0 commit comments

Comments
 (0)