Skip to content
Merged
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
58 changes: 58 additions & 0 deletions tests/Unit/Api/AbstractApi/RetrieveDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Redmine\Api\AbstractApi;
use Redmine\Exception\SerializerException;
use Redmine\Exception\UnexpectedResponseException;
use Redmine\Http\HttpClient;
use Redmine\Tests\Fixtures\AssertingHttpClient;
Expand Down Expand Up @@ -184,6 +185,63 @@ public static function getRetrieveDataWithParamsData(): array
];
}

/**
* @dataProvider getRequestContentTypeData
*/
#[DataProvider('getRequestContentTypeData')]
public function testRetrieveDataSendsContentTypeGuessedFromPath(string $path, string $expectedContentType): void
{
$api = $this->createApi(AssertingHttpClient::create(
$this,
['GET', $path, $expectedContentType, '', 200, 'application/json', '{}'],
));

$this->assertSame([], $this->retrieveData($api, $path));
}

/**
* @return array<string, array{string, string}>
*/
public static function getRequestContentTypeData(): array
{
return [
'JSON path' => ['/issues.json', 'application/json'],
'JSON path with query' => ['/issues.json?limit=1', 'application/json'],
'XML path' => ['/issues.xml', 'application/xml'],
'JSON upload path' => ['/uploads.json', 'application/octet-stream'],
'XML upload path' => ['/uploads.xml', 'application/octet-stream'],
'other path' => ['/issues', ''],
];
}

/**
* @dataProvider getNotConvertibleResponseData
*/
#[DataProvider('getNotConvertibleResponseData')]
public function testRetrieveDataAndNotConvertibleResponseThrowsException(string $contentType, string $response): void
{
$api = $this->createApi(AssertingHttpClient::create(
$this,
['GET', '/issues.json', 'application/json', '', 200, $contentType, $response],
));

$this->expectException(SerializerException::class);
$this->expectExceptionMessage('Could not convert response body into array: ' . $response);

$this->retrieveData($api, '/issues.json');
}

/**
* @return array<string, array{string, string}>
*/
public static function getNotConvertibleResponseData(): array
{
return [
'unsupported content type' => ['text/plain', 'not an array'],
'JSON scalar' => ['application/json', '12345'],
];
}

private function createApi(HttpClient $client): AbstractApi
{
return new class ($client) extends AbstractApi {};
Expand Down