diff --git a/src/HttpClient/ClientFactory.php b/src/HttpClient/ClientFactory.php index 38f808f..c0ceed9 100644 --- a/src/HttpClient/ClientFactory.php +++ b/src/HttpClient/ClientFactory.php @@ -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 @@ -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)); + } } diff --git a/src/HttpClient/SimpleHttpClient/SimpleHttpClient.php b/src/HttpClient/SimpleHttpClient/SimpleHttpClient.php index e82169a..d986340 100644 --- a/src/HttpClient/SimpleHttpClient/SimpleHttpClient.php +++ b/src/HttpClient/SimpleHttpClient/SimpleHttpClient.php @@ -17,20 +17,20 @@ public function __construct(private readonly ClientInterface $client) /** * @param array $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 $body * @param array $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); } @@ -39,7 +39,7 @@ public function post(string $url, array $body = [], array $headers = []): Respon * @param array $body * @param array $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); } @@ -48,7 +48,7 @@ public function patch(string $url, array $body = [], array $headers = []): Respo * @param array $body * @param array $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); } @@ -57,7 +57,7 @@ public function put(string $url, array $body = [], array $headers = []): Respons * @param array $body * @param array $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); } @@ -67,7 +67,7 @@ public function delete(string $url, array $body = [], array $headers = []): Resp * @param array $body * @param array $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(); @@ -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); } /** diff --git a/src/HttpClient/SimpleHttpClient/Response.php b/src/HttpClient/SimpleHttpClient/SimpleHttpClientResponse.php similarity index 97% rename from src/HttpClient/SimpleHttpClient/Response.php rename to src/HttpClient/SimpleHttpClient/SimpleHttpClientResponse.php index 7c67082..43984e1 100644 --- a/src/HttpClient/SimpleHttpClient/Response.php +++ b/src/HttpClient/SimpleHttpClient/SimpleHttpClientResponse.php @@ -6,7 +6,7 @@ use Psr\Http\Message\ResponseInterface; -class Response +class SimpleHttpClientResponse { public function __construct(private readonly ResponseInterface $response) { diff --git a/tests/HttpClient/ClientFactoryTest.php b/tests/HttpClient/ClientFactoryTest.php index 3968c80..9dadaf1 100644 --- a/tests/HttpClient/ClientFactoryTest.php +++ b/tests/HttpClient/ClientFactoryTest.php @@ -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); diff --git a/tests/HttpClient/SimpleHttpClient/ResponseTest.php b/tests/HttpClient/SimpleHttpClient/SimpleHttpClientResponseTest.php similarity index 84% rename from tests/HttpClient/SimpleHttpClient/ResponseTest.php rename to tests/HttpClient/SimpleHttpClient/SimpleHttpClientResponseTest.php index cde05fd..87ab6c5 100644 --- a/tests/HttpClient/SimpleHttpClient/ResponseTest.php +++ b/tests/HttpClient/SimpleHttpClient/SimpleHttpClientResponseTest.php @@ -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')); @@ -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')); @@ -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); } diff --git a/tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php b/tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php index 1076099..5c347b6 100644 --- a/tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php +++ b/tests/HttpClient/SimpleHttpClient/SimpleHttpClientTest.php @@ -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 {