Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Analytics #294

Merged
merged 2 commits into from
Feb 14, 2022
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
11 changes: 8 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use MeiliSearch\Endpoints\Tasks;
use MeiliSearch\Endpoints\Version;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

class Client
{
Expand All @@ -35,9 +36,13 @@ class Client
private Tasks $tasks;
private Dumps $dumps;

public function __construct(string $url, string $apiKey = null, ClientInterface $httpClient = null)
{
$this->http = new Http\Client($url, $apiKey, $httpClient);
public function __construct(
string $url,
string $apiKey = null,
ClientInterface $httpClient = null,
RequestFactoryInterface $requestFactory = null
) {
$this->http = new Http\Client($url, $apiKey, $httpClient, $requestFactory);
$this->index = new Indexes($this->http);
$this->health = new Health($this->http);
$this->version = new Version($this->http);
Expand Down
12 changes: 9 additions & 3 deletions src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use MeiliSearch\Exceptions\JsonDecodingException;
use MeiliSearch\Exceptions\JsonEncodingException;
use MeiliSearch\Http\Serialize\Json;
use MeiliSearch\MeiliSearch;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\NetworkExceptionInterface;
Expand All @@ -34,15 +35,20 @@ class Client implements Http
/**
* Client constructor.
*/
public function __construct(string $url, string $apiKey = null, ClientInterface $httpClient = null)
{
public function __construct(
string $url,
string $apiKey = null,
ClientInterface $httpClient = null,
RequestFactoryInterface $reqFactory = null
) {
$this->baseUrl = $url;
$this->apiKey = $apiKey;
$this->http = $httpClient ?? Psr18ClientDiscovery::find();
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
$this->requestFactory = $reqFactory ?? Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
$this->headers = array_filter([
'Authorization' => sprintf('Bearer %s', $this->apiKey),
'User-Agent' => MeiliSearch::qualifiedVersion(),
]);
$this->json = new Json();
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use MeiliSearch\Exceptions\JsonDecodingException;
use MeiliSearch\Exceptions\JsonEncodingException;
use MeiliSearch\Http\Client;
use MeiliSearch\MeiliSearch;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand Down Expand Up @@ -191,6 +193,35 @@ public function testInvalidResponseContentTypeThrowsException(): void
$client->get('/');
}

public function testClientHasDefaultUserAgent(): void
{
$httpClient = $this->createHttpClientMock(200, '{}');
$reqFactory = $this->createMock(RequestFactoryInterface::class);
$requestStub = $this->createMock(RequestInterface::class);

$requestStub->expects($this->any())
->method('withAddedHeader')
->withConsecutive(
[
$this->equalTo('Authorization'),
$this->equalTo('Bearer masterKey'),
],
[
$this->equalTo('User-Agent'),
$this->equalTo(MeiliSearch::qualifiedVersion()),
],
)
->willReturnOnConsecutiveCalls($requestStub, $requestStub);

$reqFactory->expects($this->any())
->method('createRequest')
->willReturn($requestStub);

$client = new \MeiliSearch\Client('http://localhost:7070', 'masterKey', $httpClient, $reqFactory);

$client->health();
}

public function testParseResponseReturnsNullForNoContent(): void
{
$response = $this->createMock(ResponseInterface::class);
Expand Down