feat: add UsetrenoHttpClient
!fixup bb244e40f5fa42294acc7cc7f925796325ada4c5
This commit is contained in:
18
tests/Common/LoggerTrait.php
Normal file
18
tests/Common/LoggerTrait.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Common;
|
||||
|
||||
use Monolog\Handler\TestHandler;
|
||||
use Monolog\Logger;
|
||||
|
||||
trait LoggerTrait
|
||||
{
|
||||
/**
|
||||
* @return Logger
|
||||
*/
|
||||
protected function getLogger(): Logger {
|
||||
$logger = new Logger('test');
|
||||
$logger->pushHandler(new TestHandler());
|
||||
return $logger;
|
||||
}
|
||||
}
|
||||
54
tests/Service/Remote/UsetrenoHttpClientTest.php
Normal file
54
tests/Service/Remote/UsetrenoHttpClientTest.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Service\Remote;
|
||||
|
||||
use App\Service\Remote\Exception\AuthorizeException;
|
||||
use App\Service\Remote\UsetrenoHttpClient;
|
||||
use App\Tests\Common\LoggerTrait;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpClient\MockHttpClient;
|
||||
use Symfony\Component\HttpClient\Response\JsonMockResponse;
|
||||
|
||||
class UsetrenoHttpClientTest extends TestCase
|
||||
{
|
||||
use LoggerTrait;
|
||||
|
||||
public function testRequestWillDoAutomaticAuthorization() {
|
||||
$authMockResponse = new JsonMockResponse([
|
||||
"data" => [
|
||||
"expires_in" => 1000,
|
||||
"token" => "foobarfoobar"
|
||||
],
|
||||
"timestamp" => "2024-01-17T22:47:59+01:00",
|
||||
"uri" => "/api/v1/token"
|
||||
]);
|
||||
$authorizedRequestResponse = clone $authMockResponse;
|
||||
$mockedClient = new MockHttpClient([$authMockResponse, $authorizedRequestResponse]);
|
||||
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar");
|
||||
$client->request("POST", "https://www.root.cz/");
|
||||
$this->assertEquals("https://topapi.top-test.cz/chameleon/api/v1/token", $authMockResponse->getRequestUrl());
|
||||
$headers = $authorizedRequestResponse->getRequestOptions()['headers'];
|
||||
$this->assertEquals("https://www.root.cz/", $authorizedRequestResponse->getRequestUrl());
|
||||
$this->assertTrue(in_array("Authorization: Bearer foobarfoobar", $headers), "missing bearer authorization header");
|
||||
}
|
||||
|
||||
public function testRequestFailedAuthorization() {
|
||||
$this->expectException(AuthorizeException::class);
|
||||
$authMockResponse = new JsonMockResponse([
|
||||
"errors" => [
|
||||
[
|
||||
"message" => "Bad credentials, please verify that your username/password are correctly set.",
|
||||
"specificType" => "bad_credentials",
|
||||
"type" => "security_error"
|
||||
]
|
||||
],
|
||||
"timestamp" => "2024-01-17T23:55:25+01:00",
|
||||
"uri" => "/api/v1/token"
|
||||
]);
|
||||
|
||||
$authorizedRequestResponse = clone $authMockResponse;
|
||||
$mockedClient = new MockHttpClient([$authMockResponse, $authorizedRequestResponse]);
|
||||
$client = new UsetrenoHttpClient($mockedClient, $this->getLogger(), "foo", "bar");
|
||||
$client->request("POST", "https://www.root.cz/");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user