2025-08-10 15:46:20 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Tests\Unit\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Enums\PlatformEnum;
|
2025-08-15 02:50:42 +02:00
|
|
|
use App\Exceptions\PlatformAuthException;
|
2025-08-10 15:46:20 +02:00
|
|
|
use App\Jobs\SyncChannelPostsJob;
|
2025-08-15 02:50:42 +02:00
|
|
|
use App\Models\PlatformAccount;
|
2025-08-10 15:46:20 +02:00
|
|
|
use App\Models\PlatformChannel;
|
2025-08-15 02:50:42 +02:00
|
|
|
use App\Models\PlatformInstance;
|
|
|
|
|
use App\Modules\Lemmy\Services\LemmyApiService;
|
|
|
|
|
use App\Services\Log\LogSaver;
|
|
|
|
|
use Exception;
|
2025-08-10 15:46:20 +02:00
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
2025-08-15 02:50:42 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Illuminate\Support\Facades\Queue;
|
|
|
|
|
use Mockery;
|
2025-08-10 15:46:20 +02:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
|
|
class SyncChannelPostsJobTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
use RefreshDatabase;
|
|
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
protected function setUp(): void
|
2025-08-10 15:46:20 +02:00
|
|
|
{
|
2025-08-15 02:50:42 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
Queue::fake();
|
|
|
|
|
Cache::flush();
|
|
|
|
|
}
|
2025-08-10 15:46:20 +02:00
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
public function test_constructor_sets_correct_queue(): void
|
|
|
|
|
{
|
|
|
|
|
$channel = PlatformChannel::factory()->make();
|
2025-08-10 15:46:20 +02:00
|
|
|
$job = new SyncChannelPostsJob($channel);
|
2025-08-15 02:50:42 +02:00
|
|
|
|
2025-08-10 15:46:20 +02:00
|
|
|
$this->assertEquals('sync', $job->queue);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
public function test_job_implements_should_queue(): void
|
2025-08-10 15:46:20 +02:00
|
|
|
{
|
2025-08-15 02:50:42 +02:00
|
|
|
$channel = PlatformChannel::factory()->make();
|
2025-08-10 15:46:20 +02:00
|
|
|
$job = new SyncChannelPostsJob($channel);
|
2025-08-15 02:50:42 +02:00
|
|
|
|
2025-08-10 15:46:20 +02:00
|
|
|
$this->assertInstanceOf(\Illuminate\Contracts\Queue\ShouldQueue::class, $job);
|
2025-08-15 02:50:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_job_implements_should_be_unique(): void
|
|
|
|
|
{
|
|
|
|
|
$channel = PlatformChannel::factory()->make();
|
|
|
|
|
$job = new SyncChannelPostsJob($channel);
|
|
|
|
|
|
2025-08-10 15:46:20 +02:00
|
|
|
$this->assertInstanceOf(\Illuminate\Contracts\Queue\ShouldBeUnique::class, $job);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_job_uses_queueable_trait(): void
|
2025-08-15 02:50:42 +02:00
|
|
|
{
|
|
|
|
|
$channel = PlatformChannel::factory()->make();
|
|
|
|
|
$job = new SyncChannelPostsJob($channel);
|
|
|
|
|
|
|
|
|
|
$this->assertContains(
|
|
|
|
|
\Illuminate\Foundation\Queue\Queueable::class,
|
|
|
|
|
class_uses($job)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_dispatch_for_all_active_channels_dispatches_jobs(): void
|
2025-08-10 15:46:20 +02:00
|
|
|
{
|
|
|
|
|
// Arrange
|
2025-08-15 02:50:42 +02:00
|
|
|
$platformInstance = PlatformInstance::factory()->create([
|
|
|
|
|
'platform' => PlatformEnum::LEMMY
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$account = PlatformAccount::factory()->create([
|
|
|
|
|
'instance_url' => $platformInstance->url,
|
|
|
|
|
'is_active' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$channel = PlatformChannel::factory()->create([
|
|
|
|
|
'platform_instance_id' => $platformInstance->id,
|
|
|
|
|
'is_active' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Attach account to channel with active status
|
|
|
|
|
$channel->platformAccounts()->attach($account->id, [
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
'created_at' => now(),
|
|
|
|
|
'updated_at' => now()
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Mock LogSaver to avoid strict expectations
|
|
|
|
|
$logSaverMock = Mockery::mock(LogSaver::class);
|
|
|
|
|
$logSaverMock->shouldReceive('info')->zeroOrMoreTimes();
|
|
|
|
|
|
|
|
|
|
$this->app->instance(LogSaver::class, $logSaverMock);
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
SyncChannelPostsJob::dispatchForAllActiveChannels();
|
|
|
|
|
|
|
|
|
|
// Assert - At least one job should be dispatched
|
|
|
|
|
Queue::assertPushed(SyncChannelPostsJob::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_handle_logs_start_message(): void
|
|
|
|
|
{
|
|
|
|
|
// Arrange
|
|
|
|
|
$platformInstance = PlatformInstance::factory()->create([
|
|
|
|
|
'platform' => PlatformEnum::LEMMY,
|
|
|
|
|
'url' => 'https://lemmy.example.com'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$channel = PlatformChannel::factory()->create([
|
|
|
|
|
'platform_instance_id' => $platformInstance->id,
|
|
|
|
|
'name' => 'testcommunity'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Mock LogSaver - only test that logging methods are called
|
|
|
|
|
$logSaverMock = Mockery::mock(LogSaver::class);
|
|
|
|
|
$logSaverMock->shouldReceive('info')->atLeast()->once();
|
|
|
|
|
$logSaverMock->shouldReceive('error')->zeroOrMoreTimes();
|
|
|
|
|
|
2025-08-10 15:46:20 +02:00
|
|
|
$job = new SyncChannelPostsJob($channel);
|
|
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
// Act - This will fail due to no active account, but we test the logging
|
|
|
|
|
try {
|
|
|
|
|
$job->handle($logSaverMock);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
// Expected to fail, we're testing that logging is called
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Assert - Test completes if no exceptions during setup
|
|
|
|
|
$this->assertTrue(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_job_can_be_serialized(): void
|
|
|
|
|
{
|
|
|
|
|
$platformInstance = PlatformInstance::factory()->create();
|
|
|
|
|
$channel = PlatformChannel::factory()->create([
|
|
|
|
|
'platform_instance_id' => $platformInstance->id,
|
|
|
|
|
'name' => 'Test Channel'
|
|
|
|
|
]);
|
|
|
|
|
$job = new SyncChannelPostsJob($channel);
|
|
|
|
|
|
|
|
|
|
$serialized = serialize($job);
|
|
|
|
|
$unserialized = unserialize($serialized);
|
|
|
|
|
|
|
|
|
|
$this->assertInstanceOf(SyncChannelPostsJob::class, $unserialized);
|
|
|
|
|
$this->assertEquals($job->queue, $unserialized->queue);
|
|
|
|
|
// Note: Cannot test channel property directly as it's private
|
|
|
|
|
// but serialization/unserialization working proves the job structure is intact
|
2025-08-10 15:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_dispatch_for_all_active_channels_method_exists(): void
|
|
|
|
|
{
|
|
|
|
|
$this->assertTrue(method_exists(SyncChannelPostsJob::class, 'dispatchForAllActiveChannels'));
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 02:50:42 +02:00
|
|
|
public function test_job_has_handle_method(): void
|
2025-08-10 15:46:20 +02:00
|
|
|
{
|
2025-08-15 02:50:42 +02:00
|
|
|
$channel = PlatformChannel::factory()->make();
|
2025-08-10 15:46:20 +02:00
|
|
|
$job = new SyncChannelPostsJob($channel);
|
2025-08-15 02:50:42 +02:00
|
|
|
|
2025-08-10 15:46:20 +02:00
|
|
|
$this->assertTrue(method_exists($job, 'handle'));
|
2025-08-15 02:50:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
|
{
|
|
|
|
|
Mockery::close();
|
|
|
|
|
parent::tearDown();
|
2025-08-10 15:46:20 +02:00
|
|
|
}
|
|
|
|
|
}
|