|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Quantum\Tests\Unit\HttpClient\Adapters; |
| 4 | + |
| 5 | +use Quantum\HttpClient\Adapters\CurlAdapter; |
| 6 | +use Quantum\Tests\Unit\AppTestCase; |
| 7 | +use Curl\CaseInsensitiveArray; |
| 8 | +use Curl\Curl; |
| 9 | +use Mockery; |
| 10 | + |
| 11 | +class CurlAdapterTest extends AppTestCase |
| 12 | +{ |
| 13 | + public function tearDown(): void |
| 14 | + { |
| 15 | + Mockery::close(); |
| 16 | + |
| 17 | + parent::tearDown(); |
| 18 | + } |
| 19 | + |
| 20 | + public function testCurlAdapterDelegatesRequestMethods(): void |
| 21 | + { |
| 22 | + $curl = Mockery::mock(Curl::class); |
| 23 | + $curl->shouldReceive('setUrl')->once()->with('https://example.com'); |
| 24 | + $curl->shouldReceive('setHeader')->once()->with('Accept', 'application/json'); |
| 25 | + $curl->shouldReceive('setHeaders')->once()->with(['X-Test' => 'yes']); |
| 26 | + $curl->shouldReceive('setOpt')->once()->with(CURLOPT_TIMEOUT, 10); |
| 27 | + $curl->shouldReceive('setOpts')->once()->with([CURLOPT_CONNECTTIMEOUT => 5]); |
| 28 | + $curl->shouldReceive('buildPostData')->once()->with(['a' => 1])->andReturn('a=1'); |
| 29 | + $curl->shouldReceive('exec')->once()->andReturn('ok'); |
| 30 | + |
| 31 | + $adapter = new CurlAdapter($curl); |
| 32 | + |
| 33 | + $this->assertSame($adapter, $adapter->setUrl('https://example.com')); |
| 34 | + $this->assertSame($adapter, $adapter->setHeader('Accept', 'application/json')); |
| 35 | + $this->assertSame($adapter, $adapter->setHeaders(['X-Test' => 'yes'])); |
| 36 | + $this->assertSame($adapter, $adapter->setOpt(CURLOPT_TIMEOUT, 10)); |
| 37 | + $this->assertSame($adapter, $adapter->setOpts([CURLOPT_CONNECTTIMEOUT => 5])); |
| 38 | + $this->assertSame('a=1', $adapter->buildPostData(['a' => 1])); |
| 39 | + $this->assertSame('ok', $adapter->start()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testCurlAdapterDelegatesResponseMethods(): void |
| 43 | + { |
| 44 | + $headers = new CaseInsensitiveArray(['Content-Type' => 'application/json']); |
| 45 | + $response = (object) ['ok' => true]; |
| 46 | + |
| 47 | + $curl = Mockery::mock(Curl::class); |
| 48 | + $curl->shouldReceive('getId')->once()->andReturn(7); |
| 49 | + $curl->shouldReceive('isError')->once()->andReturn(false); |
| 50 | + $curl->shouldReceive('getErrorCode')->once()->andReturn(0); |
| 51 | + $curl->shouldReceive('getErrorMessage')->once()->andReturn(null); |
| 52 | + $curl->shouldReceive('getResponseHeaders')->once()->andReturn($headers); |
| 53 | + $curl->shouldReceive('getResponseCookies')->once()->andReturn(['sid' => 'abc']); |
| 54 | + $curl->shouldReceive('getResponse')->once()->andReturn($response); |
| 55 | + $curl->shouldReceive('getInfo')->with(CURLINFO_HTTP_CODE)->once()->andReturn(200); |
| 56 | + $curl->shouldReceive('getUrl')->once()->andReturn('https://example.com'); |
| 57 | + |
| 58 | + $adapter = new CurlAdapter($curl); |
| 59 | + |
| 60 | + $this->assertSame(7, $adapter->getId()); |
| 61 | + $this->assertFalse($adapter->isError()); |
| 62 | + $this->assertSame(0, $adapter->getErrorCode()); |
| 63 | + $this->assertNull($adapter->getErrorMessage()); |
| 64 | + $this->assertSame($headers, $adapter->getResponseHeaders()); |
| 65 | + $this->assertSame(['sid' => 'abc'], $adapter->getResponseCookies()); |
| 66 | + $this->assertSame($response, $adapter->getResponse()); |
| 67 | + $this->assertSame(200, $adapter->getInfo(CURLINFO_HTTP_CODE)); |
| 68 | + $this->assertSame('https://example.com', $adapter->getUrl()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testCurlAdapterSupportsAndCallsVendorMethods(): void |
| 72 | + { |
| 73 | + $curl = Mockery::mock(Curl::class); |
| 74 | + $curl->shouldReceive('setTimeout')->once()->with(15)->andReturnNull(); |
| 75 | + |
| 76 | + $adapter = new CurlAdapter($curl); |
| 77 | + |
| 78 | + $this->assertTrue($adapter->supportsMethod('setTimeout')); |
| 79 | + $this->assertFalse($adapter->supportsMethod('missingMethod')); |
| 80 | + $this->assertNull($adapter->callMethod('setTimeout', [15])); |
| 81 | + } |
| 82 | +} |
0 commit comments