145 lines
4.5 KiB
PHP
145 lines
4.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Unit\Console\Commands;
|
||
|
|
|
||
|
|
use App\Console\Commands\SyncChannelPostsCommand;
|
||
|
|
use App\Enums\PlatformEnum;
|
||
|
|
use App\Jobs\SyncChannelPostsJob;
|
||
|
|
use App\Models\PlatformAccount;
|
||
|
|
use App\Models\PlatformChannel;
|
||
|
|
use App\Models\PlatformInstance;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Illuminate\Support\Facades\Queue;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class SyncChannelPostsCommandTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
parent::setUp();
|
||
|
|
Queue::fake();
|
||
|
|
}
|
||
|
|
|
||
|
|
private function createTestChannelData(): void
|
||
|
|
{
|
||
|
|
$instance = PlatformInstance::factory()->create([
|
||
|
|
'platform' => PlatformEnum::LEMMY,
|
||
|
|
'url' => 'https://lemmy.test'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$account = PlatformAccount::factory()->create([
|
||
|
|
'platform' => PlatformEnum::LEMMY,
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
$channel = PlatformChannel::factory()->create([
|
||
|
|
'platform_instance_id' => $instance->id,
|
||
|
|
'is_active' => true
|
||
|
|
]);
|
||
|
|
|
||
|
|
// Link the account to the channel
|
||
|
|
$account->channels()->attach($channel->id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_command_has_correct_signature(): void
|
||
|
|
{
|
||
|
|
$command = new SyncChannelPostsCommand();
|
||
|
|
|
||
|
|
$this->assertEquals('channel:sync', $command->getName());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_command_has_correct_description(): void
|
||
|
|
{
|
||
|
|
$command = new SyncChannelPostsCommand();
|
||
|
|
|
||
|
|
$this->assertEquals('Manually sync channel posts for a platform', $command->getDescription());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_command_syncs_lemmy_by_default(): void
|
||
|
|
{
|
||
|
|
$this->createTestChannelData();
|
||
|
|
|
||
|
|
$this->artisan('channel:sync')
|
||
|
|
->expectsOutput('Successfully dispatched sync jobs for all active Lemmy channels')
|
||
|
|
->assertExitCode(0);
|
||
|
|
|
||
|
|
Queue::assertPushed(SyncChannelPostsJob::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_command_syncs_lemmy_when_explicitly_specified(): void
|
||
|
|
{
|
||
|
|
$this->createTestChannelData();
|
||
|
|
|
||
|
|
$this->artisan('channel:sync lemmy')
|
||
|
|
->expectsOutput('Successfully dispatched sync jobs for all active Lemmy channels')
|
||
|
|
->assertExitCode(0);
|
||
|
|
|
||
|
|
Queue::assertPushed(SyncChannelPostsJob::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_command_fails_with_unsupported_platform(): void
|
||
|
|
{
|
||
|
|
$this->artisan('channel:sync twitter')
|
||
|
|
->expectsOutput('Unsupported platform: twitter')
|
||
|
|
->assertExitCode(1);
|
||
|
|
|
||
|
|
Queue::assertNotPushed(SyncChannelPostsJob::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_command_fails_with_invalid_platform(): void
|
||
|
|
{
|
||
|
|
$this->artisan('channel:sync invalid')
|
||
|
|
->expectsOutput('Unsupported platform: invalid')
|
||
|
|
->assertExitCode(1);
|
||
|
|
|
||
|
|
Queue::assertNotPushed(SyncChannelPostsJob::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_command_handles_empty_platform_argument(): void
|
||
|
|
{
|
||
|
|
$this->createTestChannelData();
|
||
|
|
|
||
|
|
// When no platform is provided, it defaults to 'lemmy' per the signature
|
||
|
|
$this->artisan('channel:sync')
|
||
|
|
->expectsOutput('Successfully dispatched sync jobs for all active Lemmy channels')
|
||
|
|
->assertExitCode(0);
|
||
|
|
|
||
|
|
Queue::assertPushed(SyncChannelPostsJob::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_sync_lemmy_returns_success_code(): void
|
||
|
|
{
|
||
|
|
$this->createTestChannelData();
|
||
|
|
|
||
|
|
$this->artisan('channel:sync lemmy')
|
||
|
|
->expectsOutput('Successfully dispatched sync jobs for all active Lemmy channels')
|
||
|
|
->assertExitCode(0);
|
||
|
|
|
||
|
|
Queue::assertPushed(SyncChannelPostsJob::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_command_signature_accepts_platform_argument(): void
|
||
|
|
{
|
||
|
|
$command = new SyncChannelPostsCommand();
|
||
|
|
|
||
|
|
// Check that the command definition includes the platform argument
|
||
|
|
$definition = $command->getDefinition();
|
||
|
|
$this->assertTrue($definition->hasArgument('platform'));
|
||
|
|
|
||
|
|
$platformArg = $definition->getArgument('platform');
|
||
|
|
$this->assertEquals('lemmy', $platformArg->getDefault());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_private_sync_lemmy_method_calls_job(): void
|
||
|
|
{
|
||
|
|
$this->createTestChannelData();
|
||
|
|
|
||
|
|
$this->artisan('channel:sync lemmy')
|
||
|
|
->expectsOutput('Successfully dispatched sync jobs for all active Lemmy channels')
|
||
|
|
->assertExitCode(0);
|
||
|
|
|
||
|
|
Queue::assertPushed(SyncChannelPostsJob::class);
|
||
|
|
}
|
||
|
|
}
|