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
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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\Contract;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Medium;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\FileNormalizer;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Message\Content\File;
use Symfony\AI\Platform\Model;

#[Medium]
#[CoversClass(FileNormalizer::class)]
final class FileNormalizerTest extends TestCase
{
public function testSupportsNormalization()
{
$normalizer = new FileNormalizer();

$this->assertTrue($normalizer->supportsNormalization(new File('some content', 'image/jpeg'), context: [
Contract::CONTEXT_MODEL => new Model('test-model'),
]));
$this->assertFalse($normalizer->supportsNormalization('not a file'));
}

public function testGetSupportedTypes()
{
$normalizer = new FileNormalizer();

$expected = [
File::class => true,
];

$this->assertSame($expected, $normalizer->getSupportedTypes(null));
}

#[DataProvider('normalizeDataProvider')]
public function testNormalize(File $file, array $expected)
{
$normalizer = new FileNormalizer();

$normalized = $normalizer->normalize($file);

$this->assertEquals($expected, $normalized);
}

public static function normalizeDataProvider(): iterable
{
yield 'image from file' => [
File::fromFile(\dirname(__DIR__, 3).'/fixtures/image.jpg'),
[
'headers' => ['Content-Type' => 'image/jpeg'],
'body' => file_get_contents(\dirname(__DIR__, 3).'/fixtures/image.jpg'),
],
];

yield 'pdf document from file' => [
File::fromFile(\dirname(__DIR__, 3).'/fixtures/document.pdf'),
[
'headers' => ['Content-Type' => 'application/pdf'],
'body' => file_get_contents(\dirname(__DIR__, 3).'/fixtures/document.pdf'),
],
];

yield 'audio from file' => [
File::fromFile(\dirname(__DIR__, 3).'/fixtures/audio.mp3'),
[
'headers' => ['Content-Type' => 'audio/mpeg'],
'body' => file_get_contents(\dirname(__DIR__, 3).'/fixtures/audio.mp3'),
],
];

yield 'text file from content' => [
new File('Hello World', 'text/plain'),
[
'headers' => ['Content-Type' => 'text/plain'],
'body' => 'Hello World',
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?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\Contract;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Medium;
use PHPUnit\Framework\TestCase;
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\MessageBagNormalizer;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Contract\Normalizer\Message\UserMessageNormalizer;
use Symfony\AI\Platform\Message\Content\Text;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Message\UserMessage;
use Symfony\AI\Platform\Model;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

#[Medium]
#[CoversClass(MessageBagNormalizer::class)]
final class MessageBagNormalizerTest extends TestCase
{
public function testSupportsNormalization()
{
$normalizer = new MessageBagNormalizer();

$this->assertTrue($normalizer->supportsNormalization(new MessageBag(), context: [
Contract::CONTEXT_MODEL => new Model('test-model'),
]));
$this->assertFalse($normalizer->supportsNormalization('not a message bag'));
}

public function testGetSupportedTypes()
{
$normalizer = new MessageBagNormalizer();

$expected = [
MessageBag::class => true,
];

$this->assertSame($expected, $normalizer->getSupportedTypes(null));
}

#[DataProvider('provideMessageBagData')]
public function testNormalize(MessageBag $bag, array $expected)
{
$normalizer = new MessageBagNormalizer();

// Set up the concrete user message normalizer
$userMessageNormalizer = new UserMessageNormalizer();

// Mock a normalizer that delegates to the concrete normalizer
$mockNormalizer = $this->createMock(NormalizerInterface::class);
$mockNormalizer->method('normalize')
->willReturnCallback(function ($messages) use ($userMessageNormalizer): array {
$result = [];
foreach ($messages as $message) {
if ($message instanceof UserMessage) {
$result[] = $userMessageNormalizer->normalize($message);
}
}

return $result;
});

$normalizer->setNormalizer($mockNormalizer);

$normalized = $normalizer->normalize($bag);

$this->assertEquals($expected, $normalized);
}

/**
* @return iterable<array{0: MessageBag, 1: array}>
*/
public static function provideMessageBagData(): iterable
{
yield 'simple text message' => [
new MessageBag(Message::ofUser('Hello, how are you?')),
[
'headers' => ['Content-Type' => 'application/json'],
'json' => [
'messages' => [
[
'role' => 'user',
'content' => 'Hello, how are you?',
],
],
],
],
];

yield 'multiple messages' => [
new MessageBag(
Message::ofUser('What is the capital of France?'),
new UserMessage(new Text('Please provide a detailed answer.'))
),
[
'headers' => ['Content-Type' => 'application/json'],
'json' => [
'messages' => [
[
'role' => 'user',
'content' => 'What is the capital of France?',
],
[
'role' => 'user',
'content' => 'Please provide a detailed answer.',
],
],
],
],
];

yield 'empty message bag' => [
new MessageBag(),
[
'headers' => ['Content-Type' => 'application/json'],
'json' => [
'messages' => [],
],
],
];
}
}
Binary file added src/platform/tests/fixtures/audio.mp3
Binary file not shown.
Binary file added src/platform/tests/fixtures/document.pdf
Binary file not shown.
Binary file added src/platform/tests/fixtures/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.