fedi-feed-router/backend/tests/Unit/Exceptions/PublishExceptionTest.php

104 lines
No EOL
3.7 KiB
PHP

<?php
namespace Tests\Unit\Exceptions;
use App\Enums\PlatformEnum;
use App\Exceptions\PublishException;
use App\Models\Article;
use Exception;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class PublishExceptionTest extends TestCase
{
use RefreshDatabase;
public function test_get_article_returns_correct_article(): void
{
$article = Article::factory()->create(['id' => 123]);
$exception = new PublishException($article, PlatformEnum::LEMMY);
$this->assertSame($article, $exception->getArticle());
$this->assertEquals(123, $exception->getArticle()->id);
}
public function test_get_platform_returns_correct_platform(): void
{
$article = Article::factory()->create();
$exception = new PublishException($article, PlatformEnum::LEMMY);
$this->assertSame(PlatformEnum::LEMMY, $exception->getPlatform());
}
public function test_get_platform_returns_null_when_no_platform(): void
{
$article = Article::factory()->create();
$exception = new PublishException($article, null);
$this->assertNull($exception->getPlatform());
}
public function test_constructor_creates_message_with_article_id_and_platform(): void
{
$article = Article::factory()->create(['id' => 456]);
$exception = new PublishException($article, PlatformEnum::LEMMY);
$this->assertEquals('Failed to publish article #456 to lemmy', $exception->getMessage());
}
public function test_constructor_creates_message_with_article_id_only(): void
{
$article = Article::factory()->create(['id' => 789]);
$exception = new PublishException($article, null);
$this->assertEquals('Failed to publish article #789', $exception->getMessage());
}
public function test_constructor_includes_previous_exception_message(): void
{
$article = Article::factory()->create(['id' => 321]);
$previousException = new Exception('Original error message');
$exception = new PublishException($article, PlatformEnum::LEMMY, $previousException);
$this->assertEquals('Failed to publish article #321 to lemmy: Original error message', $exception->getMessage());
$this->assertSame($previousException, $exception->getPrevious());
}
public function test_constructor_includes_previous_exception_message_without_platform(): void
{
$article = Article::factory()->create(['id' => 654]);
$previousException = new Exception('Another error');
$exception = new PublishException($article, null, $previousException);
$this->assertEquals('Failed to publish article #654: Another error', $exception->getMessage());
$this->assertSame($previousException, $exception->getPrevious());
}
public function test_exception_properties_are_immutable(): void
{
$article = Article::factory()->create();
$exception = new PublishException($article, PlatformEnum::LEMMY);
// These methods should return the same instances every time
$this->assertSame($exception->getArticle(), $exception->getArticle());
$this->assertSame($exception->getPlatform(), $exception->getPlatform());
}
public function test_works_with_different_platform_enum_values(): void
{
$article = Article::factory()->create();
// Test with different platform values (if more are available)
$exception = new PublishException($article, PlatformEnum::LEMMY);
$this->assertSame(PlatformEnum::LEMMY, $exception->getPlatform());
$this->assertStringContainsString('lemmy', $exception->getMessage());
}
}