22
33namespace Tests \Unit \Commands ;
44
5- use Tests \TestCase ;
6- use App \Models \Article ;
7- use Illuminate \Support \Facades \Notification ;
85use App \Console \Commands \PostArticleToTwitter ;
9- use Illuminate \Notifications \AnonymousNotifiable ;
10- use Illuminate \Foundation \Testing \DatabaseMigrations ;
6+ use App \Models \Article ;
117use App \Notifications \PostArticleToTwitter as PostArticleToTwitterNotification ;
8+ use Illuminate \Foundation \Testing \DatabaseMigrations ;
9+ use Illuminate \Notifications \AnonymousNotifiable ;
10+ use Illuminate \Support \Facades \Notification ;
11+ use Illuminate \Support \Str ;
12+ use Tests \TestCase ;
1213
1314class PostArticleToTwitterTest extends TestCase
1415{
@@ -24,20 +25,74 @@ protected function setUp(): void
2425 public function published_articles_can_be_shared_on_twitter ()
2526 {
2627 $ article = factory (Article::class)->create ([
28+ 'title ' => 'My First Article ' ,
2729 'submitted_at ' => now (),
2830 'approved_at ' => now (),
2931 ]);
3032
3133 (new PostArticleToTwitter (new AnonymousNotifiable ()))->handle ();
3234
3335 Notification::assertSentTo (
34- new AnonymousNotifiable ,
35- PostArticleToTwitterNotification::class
36+ new AnonymousNotifiable (),
37+ PostArticleToTwitterNotification::class,
38+ function ($ notification , $ channels , $ notifiable ) use ($ article ) {
39+ $ tweet = $ notification ->generateTweet ();
40+ return Str::contains ($ tweet , 'My First Article ' ) &&
41+ Str::contains ($ tweet , route ('articles.show ' , $ article ->slug ()));
42+ }
3643 );
3744
3845 $ this ->assertTrue ($ article ->fresh ()->isShared ());
3946 }
4047
48+ /** @test */
49+ public function articles_are_shared_with_twitter_handle ()
50+ {
51+ $ user = $ this ->createUser ([
52+ 'twitter_handle ' => '_joedixon ' ,
53+ ]);
54+
55+ factory (Article::class)->create ([
56+ 'author_id ' => $ user ->id (),
57+ 'submitted_at ' => now (),
58+ 'approved_at ' => now (),
59+ ]);
60+
61+ (new PostArticleToTwitter (new AnonymousNotifiable ()))->handle ();
62+
63+ Notification::assertSentTo (
64+ new AnonymousNotifiable (),
65+ PostArticleToTwitterNotification::class,
66+ function ($ notification , $ channels , $ notifiable ) use ($ user ) {
67+ return Str::contains ($ notification ->generateTweet (), '@_joedixon ' );
68+ }
69+ );
70+ }
71+
72+ /** @test */
73+ public function articles_are_shared_with_name_when_no_twitter_handle ()
74+ {
75+ $ user = $ this ->createUser ([
76+ 'name ' => 'Joe Dixon ' ,
77+ ]);
78+
79+ factory (Article::class)->create ([
80+ 'author_id ' => $ user ->id (),
81+ 'submitted_at ' => now (),
82+ 'approved_at ' => now (),
83+ ]);
84+
85+ (new PostArticleToTwitter (new AnonymousNotifiable ()))->handle ();
86+
87+ Notification::assertSentTo (
88+ new AnonymousNotifiable (),
89+ PostArticleToTwitterNotification::class,
90+ function ($ notification , $ channels , $ notifiable ) use ($ user ) {
91+ return Str::contains ($ notification ->generateTweet (), 'Joe Dixon ' );
92+ }
93+ );
94+ }
95+
4196 /** @test */
4297 public function already_shared_articles_are_not_shared_again ()
4398 {
0 commit comments