26
26
use Symfony \AI \Platform \Message \UserMessage ;
27
27
use Symfony \AI \Platform \Model ;
28
28
use Symfony \Component \HttpClient \MockHttpClient ;
29
+ use Symfony \Component \HttpClient \Response \MockResponse ;
29
30
30
31
#[CoversClass(ModelClient::class)]
31
32
#[Small]
@@ -35,16 +36,23 @@ final class ModelClientTest extends TestCase
35
36
#[DataProvider('urlTestCases ' )]
36
37
public function testGetUrlForDifferentInputsAndTasks (?string $ task , string $ expectedUrl )
37
38
{
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
+ });
40
49
41
50
$ model = new Model ('test-model ' );
42
- $ httpClient = new MockHttpClient ();
43
51
$ modelClient = new ModelClient ($ httpClient , 'test-provider ' , 'test-api-key ' );
44
52
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 );
48
56
}
49
57
50
58
public static function urlTestCases (): \Iterator
@@ -76,37 +84,56 @@ public static function urlTestCases(): \Iterator
76
84
#[DataProvider('payloadTestCases ' )]
77
85
public function testGetPayloadForDifferentInputsAndTasks (object |array |string $ input , array $ options , array $ expectedKeys , array $ expectedValues = [])
78
86
{
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
+
79
93
// Contract handling first
80
94
$ contract = Contract::create (
81
95
new FileNormalizer (),
82
96
new MessageBagNormalizer ()
83
97
);
84
98
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 );
89
100
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 );
92
103
93
- $ actual = $ getPayloadMethod ->invoke ($ modelClient , $ payload , $ options );
104
+ // Get the request options that were sent
105
+ $ requestOptions = $ response ->getRequestOptions ();
94
106
95
- // Check that expected keys exist
107
+ // Check that expected keys exist in the transformed structure
96
108
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
+ }
98
115
}
99
116
100
117
// Check expected values if specified
101
118
foreach ($ expectedValues as $ path => $ value ) {
102
119
$ keys = explode ('. ' , $ path );
103
- $ current = $ actual ;
104
- foreach ($ keys as $ key ) {
105
- $ this ->assertArrayHasKey ($ key , $ current );
106
- $ current = $ current [$ key ];
107
- }
108
120
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
+ }
110
137
}
111
138
}
112
139
0 commit comments