| 12 | use PHPUnit\Framework\TestCase; |
| 13 | |
| 14 | class ExternalHttpClientTest extends TestCase { |
| 15 | public function testRequestUsesSecurityOptionsAndReturnsBody(): void { |
| 16 | $captured = []; |
| 17 | $response = new class() { |
| 18 | public function getStatusCode(): int { |
| 19 | return 200; |
| 20 | } |
| 21 | |
| 22 | public function getBody(): string { |
| 23 | return '{"ok":true}'; |
| 24 | } |
| 25 | }; |
| 26 | $client = new class($response, $captured) { |
| 27 | public array $captured; |
| 28 | private object $response; |
| 29 | |
| 30 | public function __construct(object $response, array &$captured) { |
| 31 | $this->response = $response; |
| 32 | $this->captured =& $captured; |
| 33 | } |
| 34 | |
| 35 | public function request(string $method, string $url, array $options): object { |
| 36 | $this->captured = compact('method', 'url', 'options'); |
| 37 | return $this->response; |
| 38 | } |
| 39 | }; |
| 40 | $service = new class($client) implements IClientService { |
| 41 | public function __construct(private object $client) { |
| 42 | } |
| 43 | |
| 44 | public function newClient(): object { |
| 45 | return $this->client; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | $urlValidator = $this->createMock(ExternalUrlValidator::class); |
| 50 | $urlValidator->method('validate')->willReturn(null); |
| 51 | |
| 52 | $result = (new ExternalHttpClient($service, $urlValidator))->request('https://93.184.216.34/data'); |
| 53 | |
| 54 | $this->assertSame(200, $result['status']); |
| 55 | $this->assertSame('{"ok":true}', $result['body']); |
| 56 | $this->assertNull($result['error']); |
| 57 | $this->assertFalse($captured['options']['allow_redirects']); |
| 58 | $this->assertTrue($captured['options']['verify']); |
| 59 | $this->assertSame(10, $captured['options']['connect_timeout']); |
| 60 | $this->assertSame(60, $captured['options']['timeout']); |
| 61 | $this->assertArrayNotHasKey('stream', $captured['options']); |
| 62 | } |
| 63 | } |
nothing calls this directly
no outgoing calls
no test coverage detected