user = User::factory()->create(); $this->workspace = Workspace::factory()->create(['user_id' => $this->user->id]); }); test('process scheduled posts dispatches publish job for due posts', function () { Queue::fake(); $socialAccount = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]); $duePost = Post::factory()->create([ 'workspace_id' => $this->workspace->id, 'user_id' => $this->user->id, 'status' => PostStatus::Scheduled, 'scheduled_at' => now()->subMinute(), ]); PostPlatform::factory()->create([ 'post_id' => $duePost->id, 'social_account_id' => $socialAccount->id, ]); $this->artisan(ProcessScheduledPosts::class)->assertSuccessful(); Queue::assertPushed(PublishPost::class, function ($job) use ($duePost) { return $job->post->id === $duePost->id; }); }); test('process scheduled posts does not dispatch for future posts', function () { Queue::fake(); $socialAccount = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]); $futurePost = Post::factory()->create([ 'workspace_id' => $this->workspace->id, 'user_id' => $this->user->id, 'status' => PostStatus::Scheduled, 'scheduled_at' => now()->addDay(), ]); PostPlatform::factory()->create([ 'post_id' => $futurePost->id, 'social_account_id' => $socialAccount->id, ]); $this->artisan(ProcessScheduledPosts::class)->assertSuccessful(); Queue::assertNotPushed(PublishPost::class); }); test('process scheduled posts does not dispatch for draft posts', function () { Queue::fake(); $socialAccount = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]); $draftPost = Post::factory()->draft()->create([ 'workspace_id' => $this->workspace->id, 'user_id' => $this->user->id, ]); PostPlatform::factory()->create([ 'post_id' => $draftPost->id, 'social_account_id' => $socialAccount->id, ]); $this->artisan(ProcessScheduledPosts::class)->assertSuccessful(); Queue::assertNotPushed(PublishPost::class); }); test('process scheduled posts handles multiple due posts', function () { Queue::fake(); $socialAccount = SocialAccount::factory()->create(['workspace_id' => $this->workspace->id]); $posts = Post::factory()->count(3)->create([ 'workspace_id' => $this->workspace->id, 'user_id' => $this->user->id, 'status' => PostStatus::Scheduled, 'scheduled_at' => now()->subMinute(), ]); foreach ($posts as $post) { PostPlatform::factory()->create([ 'post_id' => $post->id, 'social_account_id' => $socialAccount->id, ]); } $this->artisan(ProcessScheduledPosts::class)->assertSuccessful(); Queue::assertPushed(PublishPost::class, 3); });