From ca6faae8a98c7f37f8b797f6d33c64d7fb23320c Mon Sep 17 00:00:00 2001 From: allflame Date: Wed, 29 Nov 2017 15:03:59 +0200 Subject: [PATCH] Added Client::getCurlInfo to expose curl_getinfo --- src/Client.php | 16 ++++++++++++++++ tests/ClientTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/Client.php b/src/Client.php index 015e7ed..0e87cab 100644 --- a/src/Client.php +++ b/src/Client.php @@ -175,6 +175,22 @@ public function sendAsyncRequest(RequestInterface $request) return $promise; } + /** + * @param int $option + * + * @throws \RuntimeException On invalid resource + * + * @return mixed + */ + public function getCurlInfo($option = 0) + { + if (false === is_resource($this->handle)) { + throw new \RuntimeException('Cannot retrieve info: resource missing'); + } + + return $option ? curl_getinfo($this->handle, $option) : curl_getinfo($this->handle); + } + /** * Generates cURL options. * diff --git a/tests/ClientTest.php b/tests/ClientTest.php index e21130f..7b7aa34 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -80,4 +80,31 @@ public function testFactoryDiscovery() static::assertInstanceOf(Client::class, $client); } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage Cannot retrieve info: resource missing + */ + public function testEmptyResource() + { + $client = new Client(); + + $client->getCurlInfo(); + } + + public function testFailedRequest() + { + $client = new Client(); + + $handle = new \ReflectionProperty(Client::class, 'handle'); + $handle->setAccessible(true); + $handle->setValue($client, curl_init()); + $optionInfo = $client->getCurlInfo(CURLINFO_HTTP_CODE); + static::assertTrue(is_int($optionInfo)); + static::assertEquals(0, $optionInfo); + $allInfo = $client->getCurlInfo(); + static::assertTrue(is_array($allInfo)); + static::assertEquals(0, $allInfo['request_size']); + static::assertEquals(0, $allInfo['http_code']); + } }