Skip to content

Commit

Permalink
add tests and improve container binding
Browse files Browse the repository at this point in the history
  • Loading branch information
zuzana-kunckova committed Mar 19, 2021
1 parent 1ef1fee commit 85453da
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
},
"require-dev": {
"orchestra/testbench": "^5.0 || ^6.0",
"phpunit/phpunit": "^9.3"
"phpunit/phpunit": "^9.3",
"spatie/phpunit-snapshot-assertions": "^4.2"
},
"config": {
"sort-packages": true
Expand Down
10 changes: 9 additions & 1 deletion src/TweetComponentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Larabelles\TweetComponent;

use DG\Twitter\Twitter;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;
use Larabelles\TweetComponent\View\Components\Tweet;
Expand All @@ -10,7 +11,14 @@ class TweetComponentServiceProvider extends ServiceProvider
{
public function register() : void
{

$this->app->singleton(Twitter::class, function (): Twitter {
return new Twitter(
config('services.twitter.consumer_key'),
config('services.twitter.consumer_secret'),
config('services.twitter.access_token'),
config('services.twitter.access_token_secret')
);
});
}

public function boot() : void
Expand Down
8 changes: 4 additions & 4 deletions src/View/Components/Tweet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
class Tweet extends Component
{
protected string $id;
protected Twitter $twitter;

public function __construct(string $id)
public function __construct(string $id, Twitter $twitter)
{
$this->id = $id;
$this->twitter = $twitter;
}

public function render() : View
Expand All @@ -22,8 +24,6 @@ public function render() : View

protected function retrieveData() : array
{
$twitter = new Twitter(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'), env('TWITTER_ACCESS_TOKEN'), env('TWITTER_ACCESS_TOKEN_SECRET'));

return collect($twitter->request("statuses/show/{$this->id}", 'GET'))->dump()->toArray();
return collect($this->twitter->request("statuses/show/{$this->id}", 'GET'))->toArray();
}
}
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@ protected function blade(string $template, array $data = []): string

return view(Str::before(basename($tempFile), '.blade.php'), $data)->render();
}

protected function setUp(): void
{
parent::setUp();
config()->set('services.twitter', [
'consumer_key' => env('TWITTER_CONSUMER_KEY'),
'consumer_secret' => env('TWITTER_CONSUMER_SECRET'),
'access_token' => env('TWITTER_ACCESS_TOKEN'),
'access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'),
]);
}
}
7 changes: 6 additions & 1 deletion tests/TweetComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

namespace Larabelles\TweetComponent\Tests;

use Spatie\Snapshots\MatchesSnapshots;

class TweetComponentTest extends TestCase
{
use MatchesSnapshots;

/** @test */
public function it_renders() : void
{
$html = $this->blade('<x-tweet id="1372946230783934465" />');

file_put_contents(__DIR__.'/tweet.html', $html);
$this->assertMatchesHtmlSnapshot($html);

}
}
53 changes: 53 additions & 0 deletions tests/__snapshots__/TweetComponentTest__it_renders__1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://unpkg.com/tailwindcss@%5E2/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="min-h-screen bg-gray-100 flex flex-col justify-center">

<section class="relative py-3 px-2">
<article class="max-w-full sm:max-w-2xl sm:mx-auto">
<div class="relative bg-white shadow-lg rounded sm:rounded-md p-4 sm:p-6 lg:p-8 space-y-4">
<header class="flex flex-row items-center space-x-2">
<div>
<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">
</div>
<div class="flex-grow">
<div class="flex flex-row space-x-2">
<div class="text-xl text-gray-600 font-bold tracking-wide">Zuzana</div>
<div class="text-gray-400">@zuzana_kunckova</div>
</div>
<div class="text-sm text-gray-400">Fri, Mar 19, 2021 4:21 PM</div>
</div>
<div>
<a href="https://twitter.com/zuzana_kunckova/status/1372946230783934465">
<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">
<desc>Created with Fabric.js 1.7.22</desc>
<defs>
</defs>
<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)">
<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>
</g>
</svg>
</a>
</div>
</header>

<main>
<p class="overflow-clip overflow-hidden inline">
I *think* I finally understood what Laravel's service container is all about&#129395;. Only took me ... a long time &#128517;.
</p>
</main>

<footer class="space-x-4">
<span class="text-gray-400 text-sm">&#9825; 14</span>
<span class="text-gray-400 text-sm"><a href="https://twitter.com/zuzana_kunckova">&#9733; See Zuzana's other Tweets</a></span>
</footer>
</div>
</article>
</section>

</body>
</html>

0 comments on commit 85453da

Please sign in to comment.