Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 5 additions & 7 deletions app/Console/Commands/PostArticleToTwitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 14 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand Down
51 changes: 51 additions & 0 deletions tests/Feature/Article/PostArticleToTwitterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

use App\Models\Article;
use Spatie\TestTime\TestTime;
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')
);

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);
});
});
Loading