HummingBirdAnimeClient/tests/AnimeClient/API/APIRequestBuilderTest.php

152 lines
3.5 KiB
PHP
Raw Permalink Normal View History

<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2023-07-13 11:08:05 -04:00
* PHP version 8.1
*
2023-07-13 11:08:05 -04:00
* @copyright 2015 - 2023 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2023-07-13 11:08:05 -04:00
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\Tests\API;
2018-11-29 11:00:50 -05:00
use Aviat\AnimeClient\API\APIRequestBuilder;
use Aviat\Ion\Json;
2022-03-04 12:19:47 -05:00
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
2022-03-04 12:19:47 -05:00
use function Amp\Promise\wait;
use function Aviat\AnimeClient\getResponse;
2022-03-04 12:19:47 -05:00
/**
* @internal
*/
final class APIRequestBuilderTest extends TestCase
{
2017-02-22 15:08:29 -05:00
protected $builder;
2022-03-04 12:19:47 -05:00
protected function setUp(): void
{
$this->builder = new class () extends APIRequestBuilder {
2020-04-10 16:35:01 -04:00
protected string $baseUrl = 'https://httpbin.org/';
protected array $defaultHeaders = ['User-Agent' => "Tim's Anime Client Testsuite / 4.0"];
};
2022-03-04 12:19:47 -05:00
$this->builder->setLogger(new NullLogger());
}
2023-05-19 10:56:23 -04:00
public function testGzipRequest(): never
{
$this->markTestSkipped('Need new test API');
$request = $this->builder->newRequest('GET', 'gzip')
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
2022-03-04 12:19:47 -05:00
$this->assertTrue($body['gzipped']);
}
2023-05-19 10:56:23 -04:00
public function testInvalidRequestMethod(): never
{
$this->markTestSkipped('Need new test API');
2022-03-04 12:19:47 -05:00
$this->expectException(InvalidArgumentException::class);
$this->builder->newRequest('FOO', 'gzip')
->getFullRequest();
}
2023-05-19 10:56:23 -04:00
public function testRequestWithBasicAuth(): never
{
$this->markTestSkipped('Need new test API');
$request = $this->builder->newRequest('GET', 'headers')
->setBasicAuth('username', 'password')
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
2022-03-04 12:19:47 -05:00
$this->assertSame('Basic dXNlcm5hbWU6cGFzc3dvcmQ=', $body['headers']['Authorization']);
}
2023-05-19 10:56:23 -04:00
public function testRequestWithQueryString(): never
{
$this->markTestSkipped('Need new test API');
$query = [
'foo' => 'bar',
'bar' => [
2022-03-04 12:19:47 -05:00
'foo' => 'bar',
],
'baz' => [
2022-03-04 12:19:47 -05:00
'bar' => 'foo',
],
];
$expected = [
'bar[foo]' => 'bar',
2022-03-04 12:19:47 -05:00
'baz[bar]' => 'foo',
'foo' => 'bar',
];
$request = $this->builder->newRequest('GET', 'get')
->setQuery($query)
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
2022-03-04 12:19:47 -05:00
$this->assertSame($expected, $body['args']);
}
2023-05-19 10:56:23 -04:00
public function testFormValueRequest(): never
{
$this->markTestSkipped('Need new test API');
$formValues = [
2022-03-04 12:19:47 -05:00
'bar' => 'foo',
'foo' => 'bar',
];
$request = $this->builder->newRequest('POST', 'post')
->setFormFields($formValues)
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
2022-03-04 12:19:47 -05:00
$this->assertSame($formValues, $body['form']);
}
2023-05-19 10:56:23 -04:00
public function testFullUrlRequest(): never
{
$this->markTestSkipped('Need new test API');
$data = [
'foo' => [
'bar' => 1,
'baz' => [2, 3, 4],
2018-01-16 14:58:07 -05:00
'bazbar' => [
'a' => 1,
2022-03-04 12:19:47 -05:00
'b' => 2,
],
],
];
$request = $this->builder->newRequest('PUT', 'https://httpbin.org/put')
->setHeader('Content-Type', 'application/json')
->setJsonBody($data)
->getFullRequest();
2018-11-29 11:00:50 -05:00
$response = getResponse($request);
$body = Json::decode(wait($response->getBody()->buffer()));
2022-03-04 12:19:47 -05:00
$this->assertSame($data, $body['json']);
}
2022-03-04 12:19:47 -05:00
}