Skip to content

Commit 48cc7a9

Browse files
committed
minor #1204 [Platform] Add test for RawHttpResult (OskarStark)
This PR was merged into the main branch. Discussion ---------- [Platform] Add test for `RawHttpResult` | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | -- | License | MIT Should be merged before #1191 Commits ------- ff0ec93 [Platform] Add test for RawHttpResult
2 parents 60756ed + ff0ec93 commit 48cc7a9

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Tests\Result;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\AI\Platform\Result\RawHttpResult;
16+
use Symfony\Component\HttpClient\MockHttpClient;
17+
use Symfony\Component\HttpClient\Response\MockResponse;
18+
use Symfony\Contracts\HttpClient\ResponseInterface;
19+
20+
/**
21+
* @author Oskar Stark <[email protected]>
22+
*/
23+
final class RawHttpResultTest extends TestCase
24+
{
25+
public function testGetData()
26+
{
27+
$responseData = ['key' => 'value', 'nested' => ['foo' => 'bar']];
28+
$response = new MockResponse(json_encode($responseData));
29+
30+
$httpClient = new MockHttpClient([$response]);
31+
$actualResponse = $httpClient->request('GET', 'https://example.com');
32+
33+
$rawResult = new RawHttpResult($actualResponse);
34+
35+
$this->assertSame($responseData, $rawResult->getData());
36+
}
37+
38+
public function testGetObject()
39+
{
40+
$response = new MockResponse('{"key": "value"}');
41+
42+
$httpClient = new MockHttpClient([$response]);
43+
$actualResponse = $httpClient->request('GET', 'https://example.com');
44+
45+
$rawResult = new RawHttpResult($actualResponse);
46+
47+
$this->assertInstanceOf(ResponseInterface::class, $rawResult->getObject());
48+
$this->assertSame($actualResponse, $rawResult->getObject());
49+
}
50+
51+
public function testGetDataWithEmptyResponse()
52+
{
53+
$response = new MockResponse('{}');
54+
55+
$httpClient = new MockHttpClient([$response]);
56+
$actualResponse = $httpClient->request('GET', 'https://example.com');
57+
58+
$rawResult = new RawHttpResult($actualResponse);
59+
60+
$this->assertSame([], $rawResult->getData());
61+
}
62+
63+
public function testGetDataWithArrayResponse()
64+
{
65+
$responseData = [
66+
'choices' => [
67+
['message' => ['content' => 'Hello']],
68+
],
69+
'usage' => [
70+
'prompt_tokens' => 10,
71+
'completion_tokens' => 5,
72+
],
73+
];
74+
$response = new MockResponse(json_encode($responseData));
75+
76+
$httpClient = new MockHttpClient([$response]);
77+
$actualResponse = $httpClient->request('GET', 'https://example.com');
78+
79+
$rawResult = new RawHttpResult($actualResponse);
80+
81+
$this->assertSame($responseData, $rawResult->getData());
82+
}
83+
}

0 commit comments

Comments
 (0)