Skip to content

Commit

Permalink
feat: simplify creation of SimpleHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed May 11, 2023
1 parent 013ac80 commit 5fdd2f0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/HttpClient/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Psr\SimpleCache\CacheInterface;
use Shopware\App\SDK\HttpClient\SimpleHttpClient\SimpleHttpClient;
use Shopware\App\SDK\Shop\ShopInterface;

class ClientFactory
Expand All @@ -24,4 +25,9 @@ public function createClient(ShopInterface $shop): ClientInterface
{
return new AuthenticatedClient(new LoggerClient($this->client, $this->logger), $shop, $this->cache);
}

public function createSimpleClient(ShopInterface $shop): SimpleHttpClient
{
return new SimpleHttpClient($this->createClient($shop));
}
}
16 changes: 8 additions & 8 deletions src/HttpClient/SimpleHttpClient/SimpleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ public function __construct(private readonly ClientInterface $client)
/**
* @param array<string, string> $headers
*/
public function get(string $url, array $headers = []): Response
public function get(string $url, array $headers = []): SimpleHttpClientResponse
{
$request = $this->createRequest('GET', $url, $headers);

$response = $this->client->sendRequest($request);

return new Response($response);
return new SimpleHttpClientResponse($response);
}

/**
* @param array<mixed> $body
* @param array<string, string> $headers
*/
public function post(string $url, array $body = [], array $headers = []): Response
public function post(string $url, array $body = [], array $headers = []): SimpleHttpClientResponse
{
return $this->doRequest('POST', $url, $body, $headers);
}
Expand All @@ -39,7 +39,7 @@ public function post(string $url, array $body = [], array $headers = []): Respon
* @param array<mixed> $body
* @param array<string, string> $headers
*/
public function patch(string $url, array $body = [], array $headers = []): Response
public function patch(string $url, array $body = [], array $headers = []): SimpleHttpClientResponse
{
return $this->doRequest('PATCH', $url, $body, $headers);
}
Expand All @@ -48,7 +48,7 @@ public function patch(string $url, array $body = [], array $headers = []): Respo
* @param array<mixed> $body
* @param array<string, string> $headers
*/
public function put(string $url, array $body = [], array $headers = []): Response
public function put(string $url, array $body = [], array $headers = []): SimpleHttpClientResponse
{
return $this->doRequest('PUT', $url, $body, $headers);
}
Expand All @@ -57,7 +57,7 @@ public function put(string $url, array $body = [], array $headers = []): Respons
* @param array<mixed> $body
* @param array<string, string> $headers
*/
public function delete(string $url, array $body = [], array $headers = []): Response
public function delete(string $url, array $body = [], array $headers = []): SimpleHttpClientResponse
{
return $this->doRequest('DELETE', $url, $body, $headers);
}
Expand All @@ -67,7 +67,7 @@ public function delete(string $url, array $body = [], array $headers = []): Resp
* @param array<mixed> $body
* @param array<string, string> $headers
*/
private function doRequest(string $method, string $url, array $body = [], array $headers = []): Response
private function doRequest(string $method, string $url, array $body = [], array $headers = []): SimpleHttpClientResponse
{
$factory = new Psr17Factory();

Expand All @@ -78,7 +78,7 @@ private function doRequest(string $method, string $url, array $body = [], array

$response = $this->client->sendRequest($request);

return new Response($response);
return new SimpleHttpClientResponse($response);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Psr\Http\Message\ResponseInterface;

class Response
class SimpleHttpClientResponse
{
public function __construct(private readonly ResponseInterface $response)
{
Expand Down
10 changes: 10 additions & 0 deletions tests/HttpClient/ClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public function testFactory(): void
$factory->createClient(new MockShop('shop-id', 'shop-secret', ''));
}

/**
* Should not throw an exception when discover works
*/
#[DoesNotPerformAssertions]
public function testSimpleFactory(): void
{
$factory = new ClientFactory();
$factory->createSimpleClient(new MockShop('shop-id', 'shop-secret', ''));
}

public function testFactoryOwnClient(): void
{
$testClient = $this->createMock(ClientInterface::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
namespace Shopware\App\SDK\Tests\HttpClient\SimpleHttpClient;

use PHPUnit\Framework\Attributes\CoversClass;
use Shopware\App\SDK\HttpClient\SimpleHttpClient\Response;
use Shopware\App\SDK\HttpClient\SimpleHttpClient\SimpleHttpClientResponse;
use PHPUnit\Framework\TestCase;

#[CoversClass(Response::class)]
class ResponseTest extends TestCase
#[CoversClass(SimpleHttpClientResponse::class)]
class SimpleHttpClientResponseTest extends TestCase
{
public function testResponse(): void
{
$raw = new \Nyholm\Psr7\Response(200, ['Content-Type' => 'application/json'], '{"foo": "bar", "baz": 1}');
$response = new Response($raw);
$response = new SimpleHttpClientResponse($raw);

static::assertSame(200, $response->getStatusCode());
static::assertSame('application/json', $response->getHeader('Content-Type'));
Expand All @@ -27,7 +27,7 @@ public function testResponse(): void
public function testNonArrayResponse(): void
{
$raw = new \Nyholm\Psr7\Response(200, ['Content-Type' => 'application/json'], 'true');
$response = new Response($raw);
$response = new SimpleHttpClientResponse($raw);

static::assertSame(200, $response->getStatusCode());
static::assertSame('application/json', $response->getHeader('Content-Type'));
Expand All @@ -41,7 +41,7 @@ public function testNonArrayResponse(): void
public function testOk(int $status, bool $shouldBeOk): void
{
$raw = new \Nyholm\Psr7\Response($status, ['Content-Type' => 'application/json'], 'true');
$response = new Response($raw);
$response = new SimpleHttpClientResponse($raw);

static::assertSame($response->ok(), $shouldBeOk);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Shopware\App\SDK\HttpClient\SimpleHttpClient\Response;
use Shopware\App\SDK\HttpClient\SimpleHttpClient\SimpleHttpClientResponse;
use Shopware\App\SDK\HttpClient\SimpleHttpClient\SimpleHttpClient;
use PHPUnit\Framework\TestCase;
use Shopware\App\SDK\Test\MockClient;

#[CoversClass(SimpleHttpClient::class)]
#[CoversClass(Response::class)]
#[CoversClass(SimpleHttpClientResponse::class)]
#[CoversClass(MockClient::class)]
class SimpleHttpClientTest extends TestCase
{
Expand Down

0 comments on commit 5fdd2f0

Please sign in to comment.