Skip to content

Commit 24eff07

Browse files
committed
improved tests for transport and httpFactoryTrait
1 parent 488efe8 commit 24eff07

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"mockery/mockery": "^1.5",
2020
"phpunit/php-code-coverage": "^10.1@dev",
2121
"symfony/http-client": "^6.4.3|^7.1.0",
22-
"nyholm/psr7": "^1.8@dev"
22+
"nyholm/psr7": "^1.8@dev",
23+
"php-http/mock-client": "1.x-dev"
2324
},
2425
"autoload": {
2526
"psr-4": {

tests/Unit/Http/TransportTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* @since Mar 2023
4+
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
5+
*/
6+
7+
namespace Qdrant\Tests\Unit\Http;
8+
9+
use Http\Discovery\Psr17FactoryDiscovery;
10+
use Http\Mock\Client;
11+
use PHPUnit\Framework\TestCase;
12+
use Psr\Http\Message\ResponseInterface;
13+
use Qdrant\Config;
14+
use Qdrant\Http\Transport;
15+
16+
class TransportTest extends TestCase
17+
{
18+
public function testTransportApiKeyHeader(): void
19+
{
20+
$client = new Client();
21+
$config = new Config('127.0.0.1', 3232);
22+
$config->setApiKey('foo-bar');
23+
$transport = new Transport($client, $config);
24+
25+
$httpFactory = Psr17FactoryDiscovery::findRequestFactory();
26+
$request = $httpFactory->createRequest('GET', '/');
27+
28+
$this->assertInstanceOf(ResponseInterface::class, $transport->sendRequest($request));
29+
$lastRequest = $client->getLastRequest();
30+
$this->assertEquals(['foo-bar'], $lastRequest->getHeader('api-key'));
31+
}
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Qdrant\Tests\Unit\Models\Traits;
4+
5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\TestCase;
7+
use Psr\Http\Message\RequestFactoryInterface;
8+
use Psr\Http\Message\RequestInterface;
9+
use Qdrant\Endpoints\HttpFactoryTrait;
10+
use Qdrant\Models\Traits\ProtectedPropertyAccessor;
11+
12+
class HttpFactoryTraitTest extends TestCase
13+
{
14+
public function testCustomHttpRequestFactory()
15+
{
16+
$requestFactory = new class implements RequestFactoryInterface {
17+
public function createRequest(string $method, $uri): RequestInterface
18+
{
19+
}
20+
};
21+
22+
$mock = new class {
23+
use HttpFactoryTrait;
24+
};
25+
$mock->setHttpFactory($requestFactory);
26+
$this->assertInstanceOf(get_class($requestFactory), $mock->getHttpFactory());
27+
}
28+
29+
public function testHeaders()
30+
{
31+
$mock = new class {
32+
use HttpFactoryTrait;
33+
34+
public function test(string $method, string $uri, array $body = [], $queryString = []): RequestInterface
35+
{
36+
return $this->createRequest($method, $uri . $this->queryBuild($queryString), $body);
37+
}
38+
};
39+
40+
$request = $mock->test('GET', '/api', ['foo' => 'bar'], ['query' => 'string']);
41+
42+
$this->assertEquals('{"foo":"bar"}', $request->getBody()->getContents());
43+
$this->assertEquals('query=string', $request->getUri()->getQuery());
44+
$this->assertEquals('/api', $request->getUri()->getPath());
45+
}
46+
}

0 commit comments

Comments
 (0)