diff --git a/.env.example b/.env.example index 1bec7794..fd6377df 100644 --- a/.env.example +++ b/.env.example @@ -76,3 +76,8 @@ SENTRY_LARAVEL_DSN= SENTRY_TRACES_SAMPLE_RATE= NOTCHPAY_PUBLIC_KEY= + +TWITTER_CONSUMER_KEY=your-consumer-key +TWITTER_CONSUMER_SECRET=your-consumer-secret +TWITTER_ACCESS_TOKEN=your-accesss_token +TWITTER_ACCESS_SECRET=your-access-token-secret diff --git a/app/Console/Commands/PostArticleToTwitter.php b/app/Console/Commands/PostArticleToTwitter.php index 32110fee..ba59b28a 100644 --- a/app/Console/Commands/PostArticleToTwitter.php +++ b/app/Console/Commands/PostArticleToTwitter.php @@ -17,12 +17,10 @@ final class PostArticleToTwitter extends Command public function handle(AnonymousNotifiable $notifiable): void { - if (app()->environment('production')) { - if ($article = Article::nextForSharing()) { - $notifiable->notify(new PostArticleToTwitterNotification($article)); - - $article->markAsShared(); - } + if ($article = Article::nextForSharing()) { + $notifiable->notify(new PostArticleToTwitterNotification($article)); + + $article->markAsShared(); } } -} +} \ No newline at end of file diff --git a/composer.json b/composer.json index 57aa66de..8e0c3e12 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "guzzlehttp/guzzle": "^7.7.0", "jenssegers/agent": "^2.6.4", "laravel-notification-channels/telegram": "^4.0", - "laravel-notification-channels/twitter": "^8.0", + "laravel-notification-channels/twitter": "^8.1", "laravel/framework": "^10.0", "laravel/sanctum": "^3.2.5", "laravel/slack-notification-channel": "^2.5", diff --git a/composer.lock b/composer.lock index 91de0c3f..05b893c8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8d3e5c2bae9f6e791b1fa5293cbc708a", + "content-hash": "702948a16302c8d21412e535d9aa857b", "packages": [ { "name": "abraham/twitteroauth", @@ -10575,16 +10575,16 @@ }, { "name": "spatie/laravel-permission", - "version": "6.10.0", + "version": "6.10.1", "source": { "type": "git", "url": "https://github.com/spatie/laravel-permission.git", - "reference": "2444bb914a52c570c00ae8c94e096a58e01b2317" + "reference": "8bb69d6d67387f7a00d93a2f5fab98860f06e704" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/spatie/laravel-permission/zipball/2444bb914a52c570c00ae8c94e096a58e01b2317", - "reference": "2444bb914a52c570c00ae8c94e096a58e01b2317", + "url": "https://api.github.com/repos/spatie/laravel-permission/zipball/8bb69d6d67387f7a00d93a2f5fab98860f06e704", + "reference": "8bb69d6d67387f7a00d93a2f5fab98860f06e704", "shasum": "" }, "require": { @@ -10646,7 +10646,7 @@ ], "support": { "issues": "https://github.com/spatie/laravel-permission/issues", - "source": "https://github.com/spatie/laravel-permission/tree/6.10.0" + "source": "https://github.com/spatie/laravel-permission/tree/6.10.1" }, "funding": [ { @@ -10654,7 +10654,7 @@ "type": "github" } ], - "time": "2024-11-05T17:30:49+00:00" + "time": "2024-11-08T18:45:41+00:00" }, { "name": "spatie/laravel-sitemap", @@ -15130,16 +15130,16 @@ }, { "name": "myclabs/deep-copy", - "version": "1.12.0", + "version": "1.12.1", "source": { "type": "git", "url": "https://github.com/myclabs/DeepCopy.git", - "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c" + "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", - "reference": "3a6b9a42cd8f8771bd4295d13e1423fa7f3d942c", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/123267b2c49fbf30d78a7b2d333f6be754b94845", + "reference": "123267b2c49fbf30d78a7b2d333f6be754b94845", "shasum": "" }, "require": { @@ -15178,7 +15178,7 @@ ], "support": { "issues": "https://github.com/myclabs/DeepCopy/issues", - "source": "https://github.com/myclabs/DeepCopy/tree/1.12.0" + "source": "https://github.com/myclabs/DeepCopy/tree/1.12.1" }, "funding": [ { @@ -15186,7 +15186,7 @@ "type": "tidelift" } ], - "time": "2024-06-12T14:39:25+00:00" + "time": "2024-11-08T17:47:46+00:00" }, { "name": "nunomaduro/collision", @@ -17916,5 +17916,5 @@ "ext-json": "*" }, "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/config/services.php b/config/services.php index b2faa32f..032a45b8 100644 --- a/config/services.php +++ b/config/services.php @@ -44,8 +44,8 @@ 'client_id' => env('TWITTER_CLIENT_ID'), 'client_secret' => env('TWITTER_CLIENT_SECRET'), 'redirect' => env('TWITTER_REDIRECT'), - 'consumer_key' => env('TWITTER_CLIENT_ID'), - 'consumer_secret' => env('TWITTER_CLIENT_SECRET'), + 'consumer_key' => env('TWITTER_CONSUMER_KEY'), + 'consumer_secret' => env('TWITTER_CONSUMER_SECRET'), 'access_token' => env('TWITTER_ACCESS_TOKEN'), 'access_secret' => env('TWITTER_ACCESS_SECRET'), 'scopes' => [], diff --git a/tests/Feature/Article/PostArticleToTwitterTest.php b/tests/Feature/Article/PostArticleToTwitterTest.php new file mode 100644 index 00000000..e5ffa397 --- /dev/null +++ b/tests/Feature/Article/PostArticleToTwitterTest.php @@ -0,0 +1,50 @@ + +Notification::fake(), +TestTime::freeze('Y-m-d H:i:s', '2021-05-01 00:00:01') +); + +describe(PostArticleToTwitter::class, function() { + it('shares one article on Twitter every 4 hours when the artisan command runs', function (): void { + Article::factory()->createMany([ + ['submitted_at' => now()], + ['submitted_at' => now(), 'approved_at' => now(), 'published_at' => now()], + ['submitted_at' => now(), 'approved_at' => now(), 'published_at' => now()->addHours(1)], + ['submitted_at' => now(), 'declined_at' => now()], + ]); + + $this->assertDatabaseCount('articles', 4); + + $this->artisan(PostArticleToTwitter::class)->assertExitCode(0); + Notification::assertCount(1); + + TestTime::addHours(4); + $this->artisan(PostArticleToTwitter::class)->assertExitCode(0); + Notification::assertCount(2); + + TestTime::addHours(4); + $this->artisan(PostArticleToTwitter::class)->assertExitCode(0); + Notification::assertCount(2); + }); + + it('will not send article when there are not articles to share', function (): void { + Article::factory()->createMany([ + ['submitted_at' => now(),'approved_at' => now(),'shared_at' => now()], + ['submitted_at' => now(),'approved_at' => now(),'shared_at' => now()], + ]); + + $this->assertDatabaseCount('articles', 2); + $this->artisan(PostArticleToTwitter::class)->assertExitCode(0); + + Notification::assertNothingSent(); + Notification::assertCount(0); + }); +}); \ No newline at end of file