. */ /** * Tests for App\Services\AI\AIPlatformRegistry */ declare(strict_types=1); namespace App\Tests\Services\AI; use App\Services\AI\AIPlatformRegistry; use App\Services\AI\AIPlatforms; use App\Services\AI\AIPlatformSettingsInterface; use Jbtronics\SettingsBundle\Manager\SettingsManagerInterface; use PHPUnit\Framework\TestCase; use Symfony\AI\Platform\PlatformInterface; class AIPlatformRegistryTest extends TestCase { public function testRegistersEnabledPlatformsAndReturnsPlatform(): void { // Create a platform mock and expose it under the service tag name (openrouter) $platformMock = $this->createMock(PlatformInterface::class); // Settings for OpenRouter -> enabled $openRouterSettings = $this->createMock(AIPlatformSettingsInterface::class); $openRouterSettings->method('isAIPlatformEnabled')->willReturn(true); // Settings for LMStudio -> disabled $lmSettings = $this->createMock(AIPlatformSettingsInterface::class); $lmSettings->method('isAIPlatformEnabled')->willReturn(false); // Settings manager should return the corresponding settings object depending on the requested class name $settingsManager = $this->createMock(SettingsManagerInterface::class); $settingsManager->method('get')->willReturnMap([ [AIPlatforms::OPENROUTER->toSettingsClass(), $openRouterSettings], [AIPlatforms::LMSTUDIO->toSettingsClass(), $lmSettings], ]); $platforms = new \ArrayIterator([ AIPlatforms::OPENROUTER->toServiceTagName() => $platformMock, ]); $registry = new AIPlatformRegistry($settingsManager, $platforms); // OPENROUTER should be enabled and retrievable $this->assertTrue($registry->isEnabled(AIPlatforms::OPENROUTER)); $this->assertSame($platformMock, $registry->getPlatform(AIPlatforms::OPENROUTER)); // LMSTUDIO is either not registered or disabled -> should not be enabled $this->assertFalse($registry->isEnabled(AIPlatforms::LMSTUDIO)); $this->expectException(\InvalidArgumentException::class); $registry->getPlatform(AIPlatforms::LMSTUDIO); } public function testGetEnabledPlatformsReturnsIndexedArray(): void { $platformMock = $this->createMock(PlatformInterface::class); $openRouterSettings = $this->createMock(AIPlatformSettingsInterface::class); $openRouterSettings->method('isAIPlatformEnabled')->willReturn(true); $settingsManager = $this->createMock(SettingsManagerInterface::class); $settingsManager->method('get')->willReturnMap([ [AIPlatforms::OPENROUTER->toSettingsClass(), $openRouterSettings], [AIPlatforms::LMSTUDIO->toSettingsClass(), $this->createMock(AIPlatformSettingsInterface::class)], ]); $platforms = new \ArrayIterator([ AIPlatforms::OPENROUTER->toServiceTagName() => $platformMock, // lmstudio not registered ]); $registry = new AIPlatformRegistry($settingsManager, $platforms); $enabled = $registry->getEnabledPlatforms(); $this->assertArrayHasKey(AIPlatforms::OPENROUTER->value, $enabled); $this->assertSame($platformMock, $enabled[AIPlatforms::OPENROUTER->value]); } }