109 lines
3.5 KiB
PHP
109 lines
3.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Tests\Unit\Jobs;
|
||
|
|
|
||
|
|
use App\Exceptions\PublishException;
|
||
|
|
use App\Jobs\PublishToLemmyJob;
|
||
|
|
use App\Models\Article;
|
||
|
|
use App\Models\Feed;
|
||
|
|
use App\Services\Article\ArticleFetcher;
|
||
|
|
use App\Services\Publishing\ArticlePublishingService;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Mockery;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class PublishToLemmyJobTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
public function test_constructor_sets_correct_queue_and_properties(): void
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
$article = new Article(['title' => 'Test Article']);
|
||
|
|
|
||
|
|
// Act
|
||
|
|
$job = new PublishToLemmyJob($article);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
$this->assertEquals('lemmy-posts', $job->queue);
|
||
|
|
$this->assertEquals(3, $job->tries);
|
||
|
|
$this->assertEquals([60, 120, 300], $job->backoff);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_job_implements_should_queue(): void
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
$article = new Article(['title' => 'Test Article']);
|
||
|
|
$job = new PublishToLemmyJob($article);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
$this->assertInstanceOf(\Illuminate\Contracts\Queue\ShouldQueue::class, $job);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_job_uses_queueable_trait(): void
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
$article = new Article(['title' => 'Test Article']);
|
||
|
|
$job = new PublishToLemmyJob($article);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
$this->assertTrue(method_exists($job, 'onQueue'));
|
||
|
|
$this->assertTrue(method_exists($job, 'onConnection'));
|
||
|
|
$this->assertTrue(method_exists($job, 'delay'));
|
||
|
|
$this->assertTrue(method_exists($job, 'fail'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_handle_method_exists(): void
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
$article = new Article(['title' => 'Test Article']);
|
||
|
|
$job = new PublishToLemmyJob($article);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
$this->assertTrue(method_exists($job, 'handle'));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_job_calls_article_fetcher_and_publishing_service(): void
|
||
|
|
{
|
||
|
|
// This is a structural test - we can't easily mock static methods
|
||
|
|
// But we can verify the job has the correct structure
|
||
|
|
|
||
|
|
// Arrange
|
||
|
|
$article = new Article(['title' => 'Test Article']);
|
||
|
|
$job = new PublishToLemmyJob($article);
|
||
|
|
|
||
|
|
// Assert - Job should have handle method that uses the required services
|
||
|
|
$this->assertTrue(method_exists($job, 'handle'));
|
||
|
|
$this->assertIsObject($job);
|
||
|
|
|
||
|
|
// We can't easily test the actual execution due to static method calls
|
||
|
|
// but we can verify the job structure is correct
|
||
|
|
$this->assertTrue(true);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_job_properties_are_correct_type(): void
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
$article = new Article(['title' => 'Test Article']);
|
||
|
|
$job = new PublishToLemmyJob($article);
|
||
|
|
|
||
|
|
// Assert
|
||
|
|
$this->assertIsInt($job->tries);
|
||
|
|
$this->assertIsArray($job->backoff);
|
||
|
|
$this->assertGreaterThan(0, $job->tries);
|
||
|
|
$this->assertNotEmpty($job->backoff);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_job_backoff_increases_progressively(): void
|
||
|
|
{
|
||
|
|
// Arrange
|
||
|
|
$article = new Article(['title' => 'Test Article']);
|
||
|
|
$job = new PublishToLemmyJob($article);
|
||
|
|
|
||
|
|
// Assert - Backoff should increase with each attempt
|
||
|
|
$backoff = $job->backoff;
|
||
|
|
$this->assertCount(3, $backoff); // Should match tries
|
||
|
|
$this->assertLessThan($backoff[1], $backoff[0]); // Second attempt waits longer than first
|
||
|
|
$this->assertLessThan($backoff[2], $backoff[1]); // Third attempt waits longer than second
|
||
|
|
}
|
||
|
|
}
|