Skip to content

Commit a257bde

Browse files
committed
minor #451 [Platform][HuggingFace] Refactor test using MockClient properly (OskarStark)
This PR was merged into the main branch. Discussion ---------- [Platform][HuggingFace] Refactor test using `MockClient` properly | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | -- | License | MIT Commits ------- 3aaa588 refactor: use MockHttpClient callback for URL assertions
2 parents db5ea6e + 3aaa588 commit a257bde

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

src/platform/tests/Bridge/HuggingFace/ModelClientTest.php

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\AI\Platform\Message\UserMessage;
2727
use Symfony\AI\Platform\Model;
2828
use Symfony\Component\HttpClient\MockHttpClient;
29+
use Symfony\Component\HttpClient\Response\MockResponse;
2930

3031
#[CoversClass(ModelClient::class)]
3132
#[Small]
@@ -35,16 +36,23 @@ final class ModelClientTest extends TestCase
3536
#[DataProvider('urlTestCases')]
3637
public function testGetUrlForDifferentInputsAndTasks(?string $task, string $expectedUrl)
3738
{
38-
$reflection = new \ReflectionClass(ModelClient::class);
39-
$getUrlMethod = $reflection->getMethod('getUrl');
39+
$response = new MockResponse('{"result": "test"}', [
40+
'http_code' => 200,
41+
]);
42+
43+
$httpClient = new MockHttpClient(function (string $method, string $url) use ($expectedUrl, $response): MockResponse {
44+
$this->assertSame('POST', $method);
45+
$this->assertSame($expectedUrl, $url);
46+
47+
return $response;
48+
});
4049

4150
$model = new Model('test-model');
42-
$httpClient = new MockHttpClient();
4351
$modelClient = new ModelClient($httpClient, 'test-provider', 'test-api-key');
4452

45-
$actualUrl = $getUrlMethod->invoke($modelClient, $model, $task);
46-
47-
$this->assertEquals($expectedUrl, $actualUrl);
53+
// Make a request to trigger URL generation
54+
$options = $task ? ['task' => $task] : [];
55+
$modelClient->request($model, 'test input', $options);
4856
}
4957

5058
public static function urlTestCases(): \Iterator
@@ -76,37 +84,56 @@ public static function urlTestCases(): \Iterator
7684
#[DataProvider('payloadTestCases')]
7785
public function testGetPayloadForDifferentInputsAndTasks(object|array|string $input, array $options, array $expectedKeys, array $expectedValues = [])
7886
{
87+
$response = new MockResponse('{"result": "test"}');
88+
$httpClient = new MockHttpClient($response);
89+
90+
$model = new Model('test-model');
91+
$modelClient = new ModelClient($httpClient, 'test-provider', 'test-api-key');
92+
7993
// Contract handling first
8094
$contract = Contract::create(
8195
new FileNormalizer(),
8296
new MessageBagNormalizer()
8397
);
8498

85-
$payload = $contract->createRequestPayload(new Model('test-model'), $input);
86-
87-
$reflection = new \ReflectionClass(ModelClient::class);
88-
$getPayloadMethod = $reflection->getMethod('getPayload');
99+
$payload = $contract->createRequestPayload($model, $input);
89100

90-
$httpClient = new MockHttpClient();
91-
$modelClient = new ModelClient($httpClient, 'test-provider', 'test-api-key');
101+
// Make a request to trigger payload generation
102+
$modelClient->request($model, $payload, $options);
92103

93-
$actual = $getPayloadMethod->invoke($modelClient, $payload, $options);
104+
// Get the request options that were sent
105+
$requestOptions = $response->getRequestOptions();
94106

95-
// Check that expected keys exist
107+
// Check that expected keys exist in the transformed structure
96108
foreach ($expectedKeys as $key) {
97-
$this->assertArrayHasKey($key, $actual);
109+
if ('json' === $key) {
110+
// JSON gets transformed to body in HTTP client
111+
$this->assertArrayHasKey('body', $requestOptions);
112+
} elseif ('headers' === $key) {
113+
$this->assertArrayHasKey('headers', $requestOptions);
114+
}
98115
}
99116

100117
// Check expected values if specified
101118
foreach ($expectedValues as $path => $value) {
102119
$keys = explode('.', $path);
103-
$current = $actual;
104-
foreach ($keys as $key) {
105-
$this->assertArrayHasKey($key, $current);
106-
$current = $current[$key];
107-
}
108120

109-
$this->assertEquals($value, $current);
121+
if ('headers' === $keys[0] && 'Content-Type' === $keys[1]) {
122+
// Check Content-Type header in the normalized structure
123+
$this->assertContains('Content-Type: application/json', $requestOptions['headers']);
124+
} elseif ('json' === $keys[0]) {
125+
// JSON content is in the body, need to decode
126+
$body = json_decode($requestOptions['body'], true);
127+
$current = $body;
128+
129+
// Navigate through the remaining keys
130+
for ($i = 1; $i < \count($keys); ++$i) {
131+
$this->assertArrayHasKey($keys[$i], $current);
132+
$current = $current[$keys[$i]];
133+
}
134+
135+
$this->assertEquals($value, $current);
136+
}
110137
}
111138
}
112139

0 commit comments

Comments
 (0)