Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions src/platform/tests/Bridge/HuggingFace/ApiClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Tests\Bridge\HuggingFace;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\HuggingFace\ApiClient;
use Symfony\AI\Platform\Model;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\JsonMockResponse;

/**
* @author Oskar Stark <oskarstark@googlemail.com>
*/
#[CoversClass(ApiClient::class)]
#[Small]
#[UsesClass(Model::class)]
final class ApiClientTest extends TestCase
{
#[TestDox('Returns array of Model objects when API responds with model data')]
public function testModelsWithProviderAndTask()
{
$responseData = [
['id' => 'model-1'],
['id' => 'model-2'],
['id' => 'model-3'],
];

$httpClient = new MockHttpClient(new JsonMockResponse($responseData));
$apiClient = new ApiClient($httpClient);

$models = $apiClient->models('test-provider', 'text-generation');

$this->assertCount(3, $models);
$this->assertInstanceOf(Model::class, $models[0]);
$this->assertSame('model-1', $models[0]->getName());
$this->assertSame('model-2', $models[1]->getName());
$this->assertSame('model-3', $models[2]->getName());
}

#[TestDox('Handles null provider and task parameters correctly')]
public function testModelsWithNullProviderAndTask()
{
$responseData = [
['id' => 'model-1'],
['id' => 'model-2'],
];

$httpClient = new MockHttpClient(new JsonMockResponse($responseData));
$apiClient = new ApiClient($httpClient);

$models = $apiClient->models(null, null);

$this->assertCount(2, $models);
$this->assertInstanceOf(Model::class, $models[0]);
$this->assertSame('model-1', $models[0]->getName());
$this->assertSame('model-2', $models[1]->getName());
}

#[TestDox('Returns empty array when API responds with no models')]
public function testModelsWithEmptyResponse()
{
$responseData = [];

$httpClient = new MockHttpClient(new JsonMockResponse($responseData));
$apiClient = new ApiClient($httpClient);

$models = $apiClient->models('test-provider', 'text-generation');

$this->assertCount(0, $models);
}

#[TestDox('Sends correct HTTP request with provider and task parameters')]
public function testModelsRequestParameters()
{
$httpClient = new MockHttpClient(function (string $method, string $url, array $options): JsonMockResponse {
$this->assertSame('GET', $method);
$this->assertStringStartsWith('https://huggingface.co/api/models', $url);
$this->assertArrayHasKey('query', $options);
$this->assertSame('test-provider', $options['query']['inference_provider']);
$this->assertSame('text-generation', $options['query']['pipeline_tag']);

return new JsonMockResponse([]);
});

$apiClient = new ApiClient($httpClient);
$apiClient->models('test-provider', 'text-generation');
}

#[TestDox('Sends correct HTTP request with null provider and task parameters')]
public function testModelsRequestParametersWithNullValues()
{
$httpClient = new MockHttpClient(function (string $method, string $url, array $options): JsonMockResponse {
$this->assertSame('GET', $method);
$this->assertStringStartsWith('https://huggingface.co/api/models', $url);
$this->assertArrayHasKey('query', $options);
$this->assertNull($options['query']['inference_provider']);
$this->assertNull($options['query']['pipeline_tag']);

return new JsonMockResponse([]);
});

$apiClient = new ApiClient($httpClient);
$apiClient->models(null, null);
}
}
86 changes: 86 additions & 0 deletions src/platform/tests/Bridge/HuggingFace/PlatformFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Tests\Bridge\HuggingFace;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\FileNormalizer;
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\MessageBagNormalizer;
use Symfony\AI\Platform\Bridge\HuggingFace\ModelClient;
use Symfony\AI\Platform\Bridge\HuggingFace\PlatformFactory;
use Symfony\AI\Platform\Bridge\HuggingFace\Provider;
use Symfony\AI\Platform\Bridge\HuggingFace\ResultConverter;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Component\HttpClient\MockHttpClient;

/**
* @author Oskar Stark <oskarstark@googlemail.com>
*/
#[CoversClass(PlatformFactory::class)]
#[Small]
#[UsesClass(Platform::class)]
#[UsesClass(ModelClient::class)]
#[UsesClass(ResultConverter::class)]
#[UsesClass(Contract::class)]
#[UsesClass(FileNormalizer::class)]
#[UsesClass(MessageBagNormalizer::class)]
final class PlatformFactoryTest extends TestCase
{
#[TestDox('Creates Platform with default provider and auto-generated components')]
public function testCreateWithDefaults()
{
$platform = PlatformFactory::create('test-api-key');

$this->assertInstanceOf(Platform::class, $platform);
}

#[TestDox('Creates Platform with custom provider')]
public function testCreateWithCustomProvider()
{
$platform = PlatformFactory::create('test-api-key', Provider::COHERE);

$this->assertInstanceOf(Platform::class, $platform);
}

#[TestDox('Handles EventSourceHttpClient correctly')]
public function testCreateWithEventSourceHttpClient()
{
$httpClient = new EventSourceHttpClient(new MockHttpClient());
$platform = PlatformFactory::create('test-api-key', Provider::HF_INFERENCE, $httpClient);

$this->assertInstanceOf(Platform::class, $platform);
}

#[TestDox('Creates Platform successfully with all supported providers')]
#[TestWith([Provider::CEREBRAS])]
#[TestWith([Provider::COHERE])]
#[TestWith([Provider::FAL_AI])]
#[TestWith([Provider::FIREWORKS])]
#[TestWith([Provider::HYPERBOLIC])]
#[TestWith([Provider::HF_INFERENCE])]
#[TestWith([Provider::NEBIUS])]
#[TestWith([Provider::NOVITA])]
#[TestWith([Provider::REPLICATE])]
#[TestWith([Provider::SAMBA_NOVA])]
#[TestWith([Provider::TOGETHER])]
public function testCreateWithDifferentProviders(string $provider)
{
$platform = PlatformFactory::create('test-api-key', $provider);
$this->assertInstanceOf(Platform::class, $platform);
}
}
Loading