fedi-feed-router/backend/tests/Unit/Services/RoutingValidationServiceTest.php

202 lines
No EOL
8.9 KiB
PHP

<?php
namespace Tests\Unit\Services;
use App\Exceptions\RoutingMismatchException;
use App\Models\Feed;
use App\Models\Language;
use App\Models\PlatformChannel;
use App\Models\PlatformInstance;
use App\Services\RoutingValidationService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
use Tests\TestCase;
class RoutingValidationServiceTest extends TestCase
{
use RefreshDatabase;
private RoutingValidationService $service;
protected function setUp(): void
{
parent::setUp();
$this->service = new RoutingValidationService();
}
public function test_validate_language_compatibility_passes_when_feed_has_no_language(): void
{
$feed = Feed::factory()->create(['language_id' => null]);
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$esLanguage = Language::factory()->create(['short_code' => 'es']);
$instance = PlatformInstance::factory()->create();
$channels = collect([
PlatformChannel::factory()->create(['language_id' => $enLanguage->id, 'platform_instance_id' => $instance->id]),
PlatformChannel::factory()->create(['language_id' => $esLanguage->id, 'platform_instance_id' => $instance->id]),
]);
$this->service->validateLanguageCompatibility($feed, $channels);
// If we get here without exception, the test passes
$this->assertTrue(true);
}
public function test_validate_language_compatibility_passes_when_channels_have_no_language(): void
{
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$feed = Feed::factory()->create(['language_id' => $enLanguage->id]);
$instance = PlatformInstance::factory()->create();
$channels = collect([
PlatformChannel::factory()->create(['language_id' => null, 'platform_instance_id' => $instance->id]),
PlatformChannel::factory()->create(['language_id' => null, 'platform_instance_id' => $instance->id]),
]);
$this->service->validateLanguageCompatibility($feed, $channels);
// If we get here without exception, the test passes
$this->assertTrue(true);
}
public function test_validate_language_compatibility_passes_when_languages_match(): void
{
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$feed = Feed::factory()->create(['language_id' => $enLanguage->id]);
$instance = PlatformInstance::factory()->create();
$channels = collect([
PlatformChannel::factory()->create(['language_id' => $enLanguage->id, 'platform_instance_id' => $instance->id]),
PlatformChannel::factory()->create(['language_id' => $enLanguage->id, 'platform_instance_id' => $instance->id]),
]);
$this->service->validateLanguageCompatibility($feed, $channels);
// If we get here without exception, the test passes
$this->assertTrue(true);
}
public function test_validate_language_compatibility_passes_with_mixed_null_and_matching_languages(): void
{
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$feed = Feed::factory()->create(['language_id' => $enLanguage->id]);
$instance = PlatformInstance::factory()->create();
$channels = collect([
PlatformChannel::factory()->create(['language_id' => $enLanguage->id, 'platform_instance_id' => $instance->id]),
PlatformChannel::factory()->create(['language_id' => null, 'platform_instance_id' => $instance->id]),
PlatformChannel::factory()->create(['language_id' => $enLanguage->id, 'platform_instance_id' => $instance->id]),
]);
$this->service->validateLanguageCompatibility($feed, $channels);
// If we get here without exception, the test passes
$this->assertTrue(true);
}
public function test_validate_language_compatibility_throws_exception_when_languages_mismatch(): void
{
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$esLanguage = Language::factory()->create(['short_code' => 'es']);
$feed = Feed::factory()->create(['language_id' => $enLanguage->id]);
$instance = PlatformInstance::factory()->create();
$channel = PlatformChannel::factory()->create(['language_id' => $esLanguage->id, 'platform_instance_id' => $instance->id]);
$channels = collect([$channel]);
$this->expectException(RoutingMismatchException::class);
$this->service->validateLanguageCompatibility($feed, $channels);
}
public function test_validate_language_compatibility_throws_exception_on_first_mismatch(): void
{
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$esLanguage = Language::factory()->create(['short_code' => 'es']);
$feed = Feed::factory()->create(['language_id' => $enLanguage->id]);
$instance = PlatformInstance::factory()->create();
$mismatchChannel = PlatformChannel::factory()->create(['language_id' => $esLanguage->id, 'platform_instance_id' => $instance->id]);
$matchingChannel = PlatformChannel::factory()->create(['language_id' => $enLanguage->id, 'platform_instance_id' => $instance->id]);
$channels = collect([$mismatchChannel, $matchingChannel]);
$this->expectException(RoutingMismatchException::class);
$this->service->validateLanguageCompatibility($feed, $channels);
}
public function test_validate_language_compatibility_with_empty_collection(): void
{
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$feed = Feed::factory()->create(['language_id' => $enLanguage->id]);
$channels = collect([]);
$this->service->validateLanguageCompatibility($feed, $channels);
// If we get here without exception, the test passes
$this->assertTrue(true);
}
public function test_validate_language_compatibility_exception_contains_correct_feed_and_channel(): void
{
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$esLanguage = Language::factory()->create(['short_code' => 'es']);
$feed = Feed::factory()->create(['language_id' => $enLanguage->id]);
$instance = PlatformInstance::factory()->create();
$channel = PlatformChannel::factory()->create(['language_id' => $esLanguage->id, 'platform_instance_id' => $instance->id]);
$channels = collect([$channel]);
try {
$this->service->validateLanguageCompatibility($feed, $channels);
$this->fail('Expected RoutingMismatchException to be thrown');
} catch (RoutingMismatchException $e) {
// Exception should contain the feed and channel that caused the mismatch
$this->assertInstanceOf(RoutingMismatchException::class, $e);
}
}
public function test_validate_language_compatibility_with_multiple_mismatching_channels(): void
{
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$esLanguage = Language::factory()->create(['short_code' => 'es']);
$frLanguage = Language::factory()->create(['short_code' => 'fr']);
$feed = Feed::factory()->create(['language_id' => $enLanguage->id]);
$instance = PlatformInstance::factory()->create();
$channels = collect([
PlatformChannel::factory()->create(['language_id' => $esLanguage->id, 'platform_instance_id' => $instance->id]), // This should trigger exception
PlatformChannel::factory()->create(['language_id' => $frLanguage->id, 'platform_instance_id' => $instance->id]), // This won't be reached
]);
$this->expectException(RoutingMismatchException::class);
$this->service->validateLanguageCompatibility($feed, $channels);
}
public function test_validate_language_compatibility_ignores_channels_after_first_mismatch(): void
{
$enLanguage = Language::factory()->create(['short_code' => 'en']);
$esLanguage = Language::factory()->create(['short_code' => 'es']);
$frLanguage = Language::factory()->create(['short_code' => 'fr']);
$feed = Feed::factory()->create(['language_id' => $enLanguage->id]);
$instance = PlatformInstance::factory()->create();
$channels = collect([
PlatformChannel::factory()->create(['language_id' => null, 'platform_instance_id' => $instance->id]), // Should be skipped
PlatformChannel::factory()->create(['language_id' => $esLanguage->id, 'platform_instance_id' => $instance->id]), // Should cause exception
PlatformChannel::factory()->create(['language_id' => $frLanguage->id, 'platform_instance_id' => $instance->id]), // Should not be reached
]);
$this->expectException(RoutingMismatchException::class);
$this->service->validateLanguageCompatibility($feed, $channels);
}
}