From 3745dcc5674592bf9ff4e723bbf67f040811aa34 Mon Sep 17 00:00:00 2001 From: Stevy Endaman Date: Fri, 8 Nov 2024 21:56:29 +0100 Subject: [PATCH 1/4] fix: (LAR-21) fixing twitter sharing article problem --- .env.example | 5 ++ app/Console/Commands/PostArticleToTwitter.php | 12 ++--- composer.json | 2 +- composer.lock | 4 +- config/services.php | 4 +- .../Article/PostArticleToTwitterTest.php | 51 +++++++++++++++++++ 6 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 tests/Feature/Article/PostArticleToTwitterTest.php 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 e984ef41..086748f7 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 ebba4005..d2a42fd5 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": "676bfee6284c52a07b36c680e3d569d5", + "content-hash": "a896a05c8490dea4db57f2edcd24afe7", "packages": [ { "name": "abraham/twitteroauth", @@ -17915,5 +17915,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..b17b79e2 --- /dev/null +++ b/tests/Feature/Article/PostArticleToTwitterTest.php @@ -0,0 +1,51 @@ + +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 From 8e24209d4f66e48b8c545ec312464bc6c6e4406f Mon Sep 17 00:00:00 2001 From: Stevy Endaman Date: Fri, 8 Nov 2024 22:00:04 +0100 Subject: [PATCH 2/4] fix: (LAR-21) resolve rebase conflict --- .env.example | 5 ++ app/Console/Commands/PostArticleToTwitter.php | 12 ++--- composer.json | 2 +- composer.lock | 4 +- config/services.php | 4 +- .../Article/PostArticleToTwitterTest.php | 51 +++++++++++++++++++ 6 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 tests/Feature/Article/PostArticleToTwitterTest.php 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..913f298d 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": "a896a05c8490dea4db57f2edcd24afe7", "packages": [ { "name": "abraham/twitteroauth", @@ -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..b17b79e2 --- /dev/null +++ b/tests/Feature/Article/PostArticleToTwitterTest.php @@ -0,0 +1,51 @@ + +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 From 96297212434aac264a3de5c6d7698284f8ed582a Mon Sep 17 00:00:00 2001 From: Stevy Endaman Date: Fri, 8 Nov 2024 22:13:52 +0100 Subject: [PATCH 3/4] fix: (LAR-21) update composer lock file --- composer.lock | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/composer.lock b/composer.lock index 913f298d..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": "a896a05c8490dea4db57f2edcd24afe7", + "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", From 320aeb9ac35ea9658b11d5420d193c0de385544b Mon Sep 17 00:00:00 2001 From: Stevy Endaman Date: Fri, 8 Nov 2024 22:49:31 +0100 Subject: [PATCH 4/4] refactor(test): (LAR-21) remove unecessary coma --- tests/Feature/Article/PostArticleToTwitterTest.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/Feature/Article/PostArticleToTwitterTest.php b/tests/Feature/Article/PostArticleToTwitterTest.php index b17b79e2..e5ffa397 100644 --- a/tests/Feature/Article/PostArticleToTwitterTest.php +++ b/tests/Feature/Article/PostArticleToTwitterTest.php @@ -7,7 +7,6 @@ use Illuminate\Support\Facades\Notification; use App\Console\Commands\PostArticleToTwitter; - beforeEach(fn () => Notification::fake(), TestTime::freeze('Y-m-d H:i:s', '2021-05-01 00:00:01') @@ -16,10 +15,10 @@ 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(),], + ['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); @@ -38,8 +37,8 @@ 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(),], + ['submitted_at' => now(),'approved_at' => now(),'shared_at' => now()], + ['submitted_at' => now(),'approved_at' => now(),'shared_at' => now()], ]); $this->assertDatabaseCount('articles', 2);