diff --git a/src/HttpClient/Adapters/CurlAdapter.php b/src/HttpClient/Adapters/CurlAdapter.php index 0efa7f8e..b21bb2e0 100644 --- a/src/HttpClient/Adapters/CurlAdapter.php +++ b/src/HttpClient/Adapters/CurlAdapter.php @@ -82,12 +82,9 @@ public function buildPostData($data) return $this->client->buildPostData($data); } - /** - * @return mixed - */ - public function start() + public function start(): void { - return $this->client->exec(); + $this->client->exec(); } /** diff --git a/src/HttpClient/Adapters/MultiCurlAdapter.php b/src/HttpClient/Adapters/MultiCurlAdapter.php index 825b8512..8ace7e4c 100644 --- a/src/HttpClient/Adapters/MultiCurlAdapter.php +++ b/src/HttpClient/Adapters/MultiCurlAdapter.php @@ -54,12 +54,9 @@ public function error(callable $callback): MultiCurlAdapterInterface return $this; } - /** - * @return mixed - */ - public function start() + public function start(): void { - return $this->client->start(); + $this->client->start(); } /** diff --git a/src/HttpClient/Contracts/HttpClientAdapterInterface.php b/src/HttpClient/Contracts/HttpClientAdapterInterface.php index be48c272..f1ddbcd3 100644 --- a/src/HttpClient/Contracts/HttpClientAdapterInterface.php +++ b/src/HttpClient/Contracts/HttpClientAdapterInterface.php @@ -18,9 +18,8 @@ interface HttpClientAdapterInterface { /** * Starts request execution - * @return mixed */ - public function start(); + public function start(): void; /** * Sets request header diff --git a/src/HttpClient/Enums/HttpClientType.php b/src/HttpClient/Enums/HttpClientType.php deleted file mode 100644 index a57c3916..00000000 --- a/src/HttpClient/Enums/HttpClientType.php +++ /dev/null @@ -1,27 +0,0 @@ -resolve(); + return (new HttpClient())->createRequest($url); } - public function resolve(): HttpClient + public static function createMultiRequest(): HttpClient { - if (!$this->instance) { - $this->instance = $this->createInstance(); - } - - return $this->instance; + return (new HttpClient())->createMultiRequest(); } - private function createInstance(): HttpClient + public static function createAsyncMultiRequest(callable $success, callable $error): HttpClient { - return new HttpClient(); + return (new HttpClient())->createAsyncMultiRequest($success, $error); } } diff --git a/src/HttpClient/Helpers/http_client.php b/src/HttpClient/Helpers/http_client.php new file mode 100644 index 00000000..ed4226c3 --- /dev/null +++ b/src/HttpClient/Helpers/http_client.php @@ -0,0 +1,34 @@ +assertSame($adapter, $adapter->setOpt(CURLOPT_TIMEOUT, 10)); $this->assertSame($adapter, $adapter->setOpts([CURLOPT_CONNECTTIMEOUT => 5])); $this->assertSame('a=1', $adapter->buildPostData(['a' => 1])); - $this->assertSame('ok', $adapter->start()); + + $adapter->start(); } public function testCurlAdapterDelegatesResponseMethods(): void diff --git a/tests/Unit/HttpClient/Adapters/MultiCurlAdapterTest.php b/tests/Unit/HttpClient/Adapters/MultiCurlAdapterTest.php index f950bb45..64793e4c 100644 --- a/tests/Unit/HttpClient/Adapters/MultiCurlAdapterTest.php +++ b/tests/Unit/HttpClient/Adapters/MultiCurlAdapterTest.php @@ -40,7 +40,8 @@ public function testMultiCurlAdapterDelegatesRequestMethods(): void $this->assertSame($adapter, $adapter->setOpts([CURLOPT_CONNECTTIMEOUT => 5])); $this->assertInstanceOf(CurlAdapter::class, $adapter->addGet('https://example.com', ['a' => 1])); $this->assertInstanceOf(CurlAdapter::class, $adapter->addPost('https://example.com', 'payload', true)); - $this->assertNull($adapter->start()); + + $adapter->start(); } public function testMultiCurlAdapterRegistersCallbacks(): void diff --git a/tests/Unit/HttpClient/Factories/HttpClientFactoryTest.php b/tests/Unit/HttpClient/Factories/HttpClientFactoryTest.php index 771f6cfa..933b241b 100644 --- a/tests/Unit/HttpClient/Factories/HttpClientFactoryTest.php +++ b/tests/Unit/HttpClient/Factories/HttpClientFactoryTest.php @@ -3,49 +3,48 @@ namespace Quantum\Tests\Unit\HttpClient\Factories; use Quantum\HttpClient\Factories\HttpClientFactory; +use Quantum\HttpClient\Adapters\MultiCurlAdapter; +use Quantum\HttpClient\Adapters\CurlAdapter; use Quantum\Tests\Unit\AppTestCase; use Quantum\HttpClient\HttpClient; -use Quantum\Di\Di; class HttpClientFactoryTest extends AppTestCase { - public function setUp(): void + public function testHttpClientFactoryCreatesSingleRequest(): void { - parent::setUp(); + $httpClient1 = HttpClientFactory::createRequest('https://example.com'); + $httpClient2 = HttpClientFactory::createRequest('https://example.org'); - $this->resetHttpClientFactory(); + $this->assertInstanceOf(HttpClient::class, $httpClient1); + $this->assertInstanceOf(CurlAdapter::class, $httpClient1->getAdapter()); + $this->assertNotSame($httpClient1, $httpClient2); } - public function testHttpClientFactoryInstance(): void + public function testHttpClientFactoryCreatesMultiRequest(): void { - $this->assertInstanceOf(HttpClient::class, HttpClientFactory::get()); - } - - public function testHttpClientFactoryReturnsSameInstance(): void - { - $httpClient1 = HttpClientFactory::get(); - $httpClient2 = HttpClientFactory::get(); + $httpClient1 = HttpClientFactory::createMultiRequest(); + $httpClient2 = HttpClientFactory::createMultiRequest(); - $this->assertSame($httpClient1, $httpClient2); + $this->assertInstanceOf(HttpClient::class, $httpClient1); + $this->assertTrue($httpClient1->isMultiRequest()); + $this->assertInstanceOf(MultiCurlAdapter::class, $httpClient1->getAdapter()); + $this->assertNotSame($httpClient1, $httpClient2); } - public function testHttpClientFactoryResolveReturnsSameInstance(): void + public function testHttpClientFactoryCreatesAsyncMultiRequest(): void { - $factory = Di::get(HttpClientFactory::class); + $success = static function (): void { + }; - $httpClient1 = $factory->resolve(); - $httpClient2 = $factory->resolve(); + $error = static function (): void { + }; - $this->assertSame($httpClient1, $httpClient2); - } - - private function resetHttpClientFactory(): void - { - if (!Di::isRegistered(HttpClientFactory::class)) { - Di::register(HttpClientFactory::class); - } + $httpClient1 = HttpClientFactory::createAsyncMultiRequest($success, $error); + $httpClient2 = HttpClientFactory::createAsyncMultiRequest($success, $error); - $factory = Di::get(HttpClientFactory::class); - $this->setPrivateProperty($factory, 'instance', null); + $this->assertInstanceOf(HttpClient::class, $httpClient1); + $this->assertTrue($httpClient1->isMultiRequest()); + $this->assertInstanceOf(MultiCurlAdapter::class, $httpClient1->getAdapter()); + $this->assertNotSame($httpClient1, $httpClient2); } } diff --git a/tests/Unit/HttpClient/Helpers/HttpClientHelperFunctionsTest.php b/tests/Unit/HttpClient/Helpers/HttpClientHelperFunctionsTest.php new file mode 100644 index 00000000..b4663273 --- /dev/null +++ b/tests/Unit/HttpClient/Helpers/HttpClientHelperFunctionsTest.php @@ -0,0 +1,49 @@ +assertInstanceOf(HttpClient::class, $httpClient1); + $this->assertInstanceOf(CurlAdapter::class, $httpClient1->getAdapter()); + $this->assertNotSame($httpClient1, $httpClient2); + } + + public function testHttpMultiRequestHelperCreatesMultiRequest(): void + { + $httpClient1 = httpMultiRequest(); + $httpClient2 = httpMultiRequest(); + + $this->assertInstanceOf(HttpClient::class, $httpClient1); + $this->assertTrue($httpClient1->isMultiRequest()); + $this->assertInstanceOf(MultiCurlAdapter::class, $httpClient1->getAdapter()); + $this->assertNotSame($httpClient1, $httpClient2); + } + + public function testHttpAsyncMultiRequestHelperCreatesAsyncMultiRequest(): void + { + $success = static function (): void { + }; + + $error = static function (): void { + }; + + $httpClient1 = httpAsyncMultiRequest($success, $error); + $httpClient2 = httpAsyncMultiRequest($success, $error); + + $this->assertInstanceOf(HttpClient::class, $httpClient1); + $this->assertTrue($httpClient1->isMultiRequest()); + $this->assertInstanceOf(MultiCurlAdapter::class, $httpClient1->getAdapter()); + $this->assertNotSame($httpClient1, $httpClient2); + } +}