Skip to content

Commit

Permalink
Merge pull request #10 from mops1k/1.0.9
Browse files Browse the repository at this point in the history
1.0.9
  • Loading branch information
mops1k authored Jan 21, 2024
2 parents 0538d2e + a1bf614 commit 5f7f6f3
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/Builder/QueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@
*/
interface QueryBuilderInterface
{
/**
* @template TQuery of QueryInterface
*
* @param TQuery $query
*/
public static function build(QueryInterface $query, ServiceInterface $service): mixed;
}
3 changes: 3 additions & 0 deletions src/Client/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use ApiClientBundle\Enum\HttpMethodEnum;
use Http\Client\Common\Plugin;

/**
* @implements QueryInterface<ServiceInterface, ResponseInterface>
*/
abstract class AbstractQuery implements QueryInterface
{
protected ?string $path = null;
Expand Down
8 changes: 6 additions & 2 deletions src/Client/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use ApiClientBundle\Enum\HttpMethodEnum;
use Http\Client\Common\Plugin;

/**
* @template TService of ServiceInterface
* @template TResponse of ResponseInterface
*/
interface QueryInterface
{
/**
Expand Down Expand Up @@ -40,12 +44,12 @@ public function getBody(): ?string;
public function getHeaders(): array;

/**
* @return class-string<ServiceInterface>
* @return class-string<TService>
*/
public function getService(): string;

/**
* @return class-string<ResponseInterface>
* @return class-string<TResponse>
*/
public function getResponse(): string;

Expand Down
24 changes: 24 additions & 0 deletions src/HTTP/Context/ContextStorageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,33 @@

interface ContextStorageInterface
{
/**
* @template TQuery of QueryInterface
*
* @param TQuery $query
*/
public function add(QueryInterface $query, RequestInterface $request, ?ResponseInterface $response): static;

/**
* @template TQuery of QueryInterface
*
* @param TQuery $query
*/
public static function get(QueryInterface $query): ?ContextInterface;

/**
* @template TQuery of QueryInterface
*
* @param TQuery $query
*/
public static function remove(QueryInterface $query): void;

/**
* @template TQuery of QueryInterface
*
* @param TQuery $query
*/
public static function has(QueryInterface $query): bool;

public static function clear(): void;
}
6 changes: 6 additions & 0 deletions src/HTTP/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ public function request(QueryInterface $query): ResponseInterface
}
}

/**
* @param QueryInterface<ServiceInterface, ResponseInterface> $query
*/
private function getClient(QueryInterface $query): ClientInterface
{
$service = $this->getService($query);
Expand Down Expand Up @@ -153,6 +156,9 @@ private function mergePlugins(array &$basePlugins, array $plugins): void
}
}

/**
* @param QueryInterface<ServiceInterface, ResponseInterface> $query
*/
private function getService(QueryInterface $query): ServiceInterface
{
$serviceClass = $query->getService();
Expand Down
4 changes: 4 additions & 0 deletions src/HTTP/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use ApiClientBundle\Client\QueryInterface;
use ApiClientBundle\Client\ResponseInterface;
use ApiClientBundle\Client\ServiceInterface;

interface HttpClientInterface
{
/**
* @param QueryInterface<ServiceInterface, ResponseInterface> $query
*/
public function request(QueryInterface $query): ResponseInterface;
}
9 changes: 3 additions & 6 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ protected function setUp(): void
$client = self::getContainer()->get(HttpClientInterface::class);

$reflectionProperty = new \ReflectionProperty($client, 'client');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($client, $this->mockHttpClient);

$this->client = $client;
Expand All @@ -68,8 +67,8 @@ public function testHttpClientRequestSuccess(): void
return $mockResponse;
});

/** @var Response $response */
$response = $this->client->request(new Query());
self::assertInstanceOf(Response::class, $response);
self::assertEquals('Ok!', $response->status);
}

Expand All @@ -95,10 +94,9 @@ function (RequestInterface $request) use ($mockResponseContents, $mockResponse,
);

$query = new Query();
/** @var Response $response */
$response = $this->client->request($query);
self::assertInstanceOf(Response::class, $response);
self::assertEquals('Ok!', $response->status);
self::assertSame('Ok!', $response->status);

$context = ContextStorage::get($query);
self::assertSame($builtRequest, $context->getRequest());
Expand All @@ -122,9 +120,9 @@ public function testHttpClientCollectionRequestSuccess(): void
return $mockResponse;
});

/** @var ListResponse $response */
$response = $this->client->request(new ListQuery());
self::assertInstanceOf(ListResponseInterface::class, $response);
self::assertInstanceOf(ListResponse::class, $response);
self::assertEquals(\json_decode($mockResponseContents, true, 512, JSON_THROW_ON_ERROR), $response->data);
}

Expand All @@ -142,7 +140,6 @@ public function testHttpClientRequestWithFileSuccess(): void
return $mockResponse;
});

/** @var ResponseWithFile $response */
$response = $this->client->request(new QueryWithFile());
self::assertInstanceOf(ResponseWithFile::class, $response);
self::assertEquals('image.png', $response->FileName);
Expand Down

0 comments on commit 5f7f6f3

Please sign in to comment.