fedi-feed-router/backend/tests/Unit/Console/Commands/FetchNewArticlesCommandTest.php

127 lines
3.6 KiB
PHP
Raw Normal View History

2025-08-07 21:19:19 +02:00
<?php
namespace Tests\Unit\Console\Commands;
use App\Console\Commands\FetchNewArticlesCommand;
use App\Jobs\ArticleDiscoveryJob;
use App\Models\Feed;
use App\Models\Setting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class FetchNewArticlesCommandTest extends TestCase
{
use RefreshDatabase;
public function test_command_skips_when_article_processing_disabled(): void
{
Queue::fake();
// Create a setting that disables article processing
Setting::factory()->create([
'key' => 'article_processing_enabled',
'value' => 'false'
]);
$this->artisan('article:refresh')
->expectsOutput('Article processing is disabled. Article discovery skipped.')
->assertExitCode(0);
Queue::assertNotPushed(ArticleDiscoveryJob::class);
}
public function test_command_skips_when_no_active_feeds(): void
{
Queue::fake();
// Enable article processing
Setting::factory()->create([
'key' => 'article_processing_enabled',
'value' => 'true'
]);
// Ensure no active feeds exist
Feed::factory()->create(['is_active' => false]);
$this->artisan('article:refresh')
->expectsOutput('No active feeds found. Article discovery skipped.')
->assertExitCode(0);
Queue::assertNotPushed(ArticleDiscoveryJob::class);
}
public function test_command_dispatches_job_when_conditions_met(): void
{
Queue::fake();
// Enable article processing
Setting::factory()->create([
'key' => 'article_processing_enabled',
'value' => 'true'
]);
// Create at least one active feed
Feed::factory()->create(['is_active' => true]);
$this->artisan('article:refresh')
->assertExitCode(0);
Queue::assertPushed(ArticleDiscoveryJob::class);
}
public function test_command_has_correct_signature(): void
{
$command = new FetchNewArticlesCommand();
$this->assertEquals('article:refresh', $command->getName());
}
public function test_command_has_correct_description(): void
{
$command = new FetchNewArticlesCommand();
$this->assertEquals('Fetches latest articles', $command->getDescription());
}
public function test_command_with_multiple_active_feeds_still_dispatches_once(): void
{
Queue::fake();
// Enable article processing
Setting::factory()->create([
'key' => 'article_processing_enabled',
'value' => 'true'
]);
// Create multiple active feeds
Feed::factory()->count(3)->create(['is_active' => true]);
$command = new FetchNewArticlesCommand();
$result = $command->handle();
$this->assertEquals(0, $result);
Queue::assertPushed(ArticleDiscoveryJob::class, 1);
}
public function test_command_ignores_inactive_feeds(): void
{
Queue::fake();
// Enable article processing
Setting::factory()->create([
'key' => 'article_processing_enabled',
'value' => 'true'
]);
// Create mix of active and inactive feeds, but ensure at least one active
Feed::factory()->create(['is_active' => true]);
Feed::factory()->count(2)->create(['is_active' => false]);
$command = new FetchNewArticlesCommand();
$result = $command->handle();
$this->assertEquals(0, $result);
Queue::assertPushed(ArticleDiscoveryJob::class);
}
}