|
18 | 18 | use Symfony\AI\Platform\Exception\NotFoundException;
|
19 | 19 | use Symfony\AI\Platform\Exception\RuntimeException;
|
20 | 20 | use Symfony\AI\Platform\Exception\ServiceUnavailableException;
|
| 21 | +use Symfony\Component\HttpClient\MockHttpClient; |
21 | 22 | use Symfony\Component\HttpClient\Response\MockResponse;
|
22 | 23 |
|
23 | 24 | #[CoversClass(HttpErrorHandler::class)]
|
24 | 25 | class HttpErrorHandlerTest extends TestCase
|
25 | 26 | {
|
26 |
| - public function testHandleHttpErrorWithSuccessfulResponse(): |
| 27 | + public function testHandleHttpErrorWithSuccessfulResponse() |
27 | 28 | {
|
28 |
| - $response = new MockResponse('{"success": true}', ['http_code' => 200]); |
| 29 | + $mockResponse = new MockResponse('{"success": true}', ['http_code' => 200]); |
| 30 | + $client = new MockHttpClient($mockResponse); |
| 31 | + $response = $client->request('GET', 'https://example.com'); |
29 | 32 |
|
30 | 33 | $this->expectNotToPerformAssertions();
|
31 | 34 | HttpErrorHandler::handleHttpError($response);
|
32 | 35 | }
|
33 | 36 |
|
34 |
| - public function testHandleAuthenticationError(): |
| 37 | + public function testHandleAuthenticationError() |
35 | 38 | {
|
36 |
| - $response = new MockResponse( |
| 39 | + $mockResponse = new MockResponse( |
37 | 40 | '{"error": {"message": "Invalid API key"}}',
|
38 | 41 | ['http_code' => 401]
|
39 | 42 | );
|
| 43 | + $client = new MockHttpClient($mockResponse); |
| 44 | + $response = $client->request('GET', 'https://example.com'); |
40 | 45 |
|
41 | 46 | $this->expectException(AuthenticationException::class);
|
42 | 47 | $this->expectExceptionMessage('Invalid API key');
|
43 | 48 | HttpErrorHandler::handleHttpError($response);
|
44 | 49 | }
|
45 | 50 |
|
46 |
| - public function testHandleNotFoundError(): |
| 51 | + public function testHandleNotFoundError() |
47 | 52 | {
|
48 |
| - $response = new MockResponse( |
| 53 | + $mockResponse = new MockResponse( |
49 | 54 | '{"error": {"message": "Model not found"}}',
|
50 | 55 | ['http_code' => 404]
|
51 | 56 | );
|
| 57 | + $client = new MockHttpClient($mockResponse); |
| 58 | + $response = $client->request('GET', 'https://example.com'); |
52 | 59 |
|
53 | 60 | $this->expectException(NotFoundException::class);
|
54 | 61 | $this->expectExceptionMessage('Model not found');
|
55 | 62 | HttpErrorHandler::handleHttpError($response);
|
56 | 63 | }
|
57 | 64 |
|
58 |
| - public function testHandleServiceUnavailableError(): |
| 65 | + public function testHandleServiceUnavailableError() |
59 | 66 | {
|
60 |
| - $response = new MockResponse( |
| 67 | + $mockResponse = new MockResponse( |
61 | 68 | '{"error": {"message": "Service temporarily unavailable"}}',
|
62 | 69 | ['http_code' => 503]
|
63 | 70 | );
|
| 71 | + $client = new MockHttpClient($mockResponse); |
| 72 | + $response = $client->request('GET', 'https://example.com'); |
64 | 73 |
|
65 | 74 | $this->expectException(ServiceUnavailableException::class);
|
66 | 75 | $this->expectExceptionMessage('Service temporarily unavailable');
|
67 | 76 | HttpErrorHandler::handleHttpError($response);
|
68 | 77 | }
|
69 | 78 |
|
70 |
| - public function testHandleGenericClientError(): |
| 79 | + public function testHandleGenericClientError() |
71 | 80 | {
|
72 |
| - $response = new MockResponse( |
| 81 | + $mockResponse = new MockResponse( |
73 | 82 | '{"error": {"message": "Bad request"}}',
|
74 | 83 | ['http_code' => 400]
|
75 | 84 | );
|
| 85 | + $client = new MockHttpClient($mockResponse); |
| 86 | + $response = $client->request('GET', 'https://example.com'); |
76 | 87 |
|
77 | 88 | $this->expectException(RuntimeException::class);
|
78 | 89 | $this->expectExceptionMessage('HTTP 400: Bad request');
|
79 | 90 | HttpErrorHandler::handleHttpError($response);
|
80 | 91 | }
|
81 | 92 |
|
82 |
| - public function testHandleErrorWithDifferentMessageFormats(): |
| 93 | + public function testHandleErrorWithDifferentMessageFormats() |
83 | 94 | {
|
84 |
| - $response = new MockResponse( |
| 95 | + $mockResponse = new MockResponse( |
85 | 96 | '{"error": "Direct error message"}',
|
86 | 97 | ['http_code' => 400]
|
87 | 98 | );
|
| 99 | + $client = new MockHttpClient($mockResponse); |
| 100 | + $response = $client->request('GET', 'https://example.com'); |
88 | 101 |
|
89 | 102 | $this->expectException(RuntimeException::class);
|
90 | 103 | $this->expectExceptionMessage('HTTP 400: Direct error message');
|
91 | 104 | HttpErrorHandler::handleHttpError($response);
|
92 | 105 | }
|
93 | 106 |
|
94 |
| - public function testHandleErrorWithMessageField(): |
| 107 | + public function testHandleErrorWithMessageField() |
95 | 108 | {
|
96 |
| - $response = new MockResponse( |
| 109 | + $mockResponse = new MockResponse( |
97 | 110 | '{"message": "Simple message format"}',
|
98 | 111 | ['http_code' => 400]
|
99 | 112 | );
|
| 113 | + $client = new MockHttpClient($mockResponse); |
| 114 | + $response = $client->request('GET', 'https://example.com'); |
100 | 115 |
|
101 | 116 | $this->expectException(RuntimeException::class);
|
102 | 117 | $this->expectExceptionMessage('HTTP 400: Simple message format');
|
103 | 118 | HttpErrorHandler::handleHttpError($response);
|
104 | 119 | }
|
105 | 120 |
|
106 |
| - public function testHandleErrorWithDetailField(): |
| 121 | + public function testHandleErrorWithDetailField() |
107 | 122 | {
|
108 |
| - $response = new MockResponse( |
| 123 | + $mockResponse = new MockResponse( |
109 | 124 | '{"detail": "Detailed error information"}',
|
110 | 125 | ['http_code' => 400]
|
111 | 126 | );
|
| 127 | + $client = new MockHttpClient($mockResponse); |
| 128 | + $response = $client->request('GET', 'https://example.com'); |
112 | 129 |
|
113 | 130 | $this->expectException(RuntimeException::class);
|
114 | 131 | $this->expectExceptionMessage('HTTP 400: Detailed error information');
|
115 | 132 | HttpErrorHandler::handleHttpError($response);
|
116 | 133 | }
|
117 | 134 |
|
118 |
| - public function testHandleErrorWithInvalidJson(): |
| 135 | + public function testHandleErrorWithInvalidJson() |
119 | 136 | {
|
120 |
| - $response = new MockResponse( |
| 137 | + $mockResponse = new MockResponse( |
121 | 138 | 'Plain text error message',
|
122 | 139 | ['http_code' => 500]
|
123 | 140 | );
|
| 141 | + $client = new MockHttpClient($mockResponse); |
| 142 | + $response = $client->request('GET', 'https://example.com'); |
124 | 143 |
|
125 | 144 | $this->expectException(RuntimeException::class);
|
126 | 145 | $this->expectExceptionMessage('HTTP 500: Plain text error message');
|
127 | 146 | HttpErrorHandler::handleHttpError($response);
|
128 | 147 | }
|
129 | 148 |
|
130 |
| - public function testHandleErrorWithEmptyResponse(): |
| 149 | + public function testHandleErrorWithEmptyResponse() |
131 | 150 | {
|
132 |
| - $response = new MockResponse('', ['http_code' => 500]); |
| 151 | + $mockResponse = new MockResponse('', ['http_code' => 500]); |
| 152 | + $client = new MockHttpClient($mockResponse); |
| 153 | + $response = $client->request('GET', 'https://example.com'); |
133 | 154 |
|
134 | 155 | $this->expectException(RuntimeException::class);
|
135 | 156 | $this->expectExceptionMessage('HTTP 500: HTTP 500 error');
|
|
0 commit comments