Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/HttpClient/Adapters/CurlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
7 changes: 2 additions & 5 deletions src/HttpClient/Adapters/MultiCurlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/HttpClient/Contracts/HttpClientAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ interface HttpClientAdapterInterface
{
/**
* Starts request execution
* @return mixed
*/
public function start();
public function start(): void;

/**
* Sets request header
Expand Down
27 changes: 0 additions & 27 deletions src/HttpClient/Enums/HttpClientType.php

This file was deleted.

29 changes: 6 additions & 23 deletions src/HttpClient/Factories/HttpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,26 @@

namespace Quantum\HttpClient\Factories;

use Quantum\App\Exceptions\BaseException;
use Quantum\Di\Exceptions\DiException;
use Quantum\HttpClient\HttpClient;
use Quantum\Di\Di;
use ReflectionException;

/**
* Class HttpClientFactory
* @package Quantum\HttpClient
*/
class HttpClientFactory
{
private ?HttpClient $instance = null;

/**
* @throws DiException|BaseException|ReflectionException
*/
public static function get(): HttpClient
public static function createRequest(string $url): HttpClient
{
if (!Di::isRegistered(self::class)) {
Di::register(self::class);
}

return Di::get(self::class)->resolve();
return (new HttpClient())->createRequest($url);
Comment thread
armanist marked this conversation as resolved.
}

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);
}
}
34 changes: 34 additions & 0 deletions src/HttpClient/Helpers/http_client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Quantum PHP Framework
* An open-source software development framework for PHP
* @link https://quantumphp.io
*/

use Quantum\HttpClient\Factories\HttpClientFactory;
use Quantum\HttpClient\HttpClient;

/**
* Creates single HTTP request client
*/
function httpRequest(string $url): HttpClient
{
return HttpClientFactory::createRequest($url);
}

/**
* Creates multi HTTP request client
*/
function httpMultiRequest(): HttpClient
{
return HttpClientFactory::createMultiRequest();
}

/**
* Creates async multi HTTP request client
*/
function httpAsyncMultiRequest(callable $success, callable $error): HttpClient
{
return HttpClientFactory::createAsyncMultiRequest($success, $error);
}
3 changes: 2 additions & 1 deletion tests/Unit/HttpClient/Adapters/CurlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function testCurlAdapterDelegatesRequestMethods(): void
$this->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
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/HttpClient/Adapters/MultiCurlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 26 additions & 27 deletions tests/Unit/HttpClient/Factories/HttpClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
49 changes: 49 additions & 0 deletions tests/Unit/HttpClient/Helpers/HttpClientHelperFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Quantum\Tests\Unit\HttpClient\Helpers;

use Quantum\HttpClient\Adapters\MultiCurlAdapter;
use Quantum\HttpClient\Adapters\CurlAdapter;
use Quantum\Tests\Unit\AppTestCase;
use Quantum\HttpClient\HttpClient;

class HttpClientHelperFunctionsTest extends AppTestCase
{
public function testHttpRequestHelperCreatesSingleRequest(): void
{
$httpClient1 = httpRequest('https://example.com');
$httpClient2 = httpRequest('https://example.org');

$this->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);
}
}
Loading