Skip to content
Snippets Groups Projects
Commit 5f8c72e0 authored by Stein Magne Bjorklund's avatar Stein Magne Bjorklund
Browse files

Make Utils::getBestContentType() tests use a data provider

- Makes assertions tests faster and easier to read.
parent 84a81b73
No related branches found
No related tags found
1 merge request!12Get best content type
......@@ -5,28 +5,20 @@ use uib\ub\loadspeakr\Utils;
final class UtilsTest extends TestCase
{
private array $http_accept;
public function setUp(): void
{
parent::setUp();
}
/**
* Test Loadspeaker content negotiation.
*
* @covers \uib\ub\loadspeakr\Utils::getBestContentType
*/
public function testGetBestContentType(): void
{
$http_accept = [
parent::setUp();
$this->http_accept = [
'html' => ['text/html'],
'rdf' => ['application/rdf+xml'],
'ttl' => [
'text/n3',
'application/x-turtle',
'application/turtle',
'text/turtle',
'application/rdf+turtle',
'text/n3',
'application/x-turtle',
'application/turtle',
'text/turtle',
'application/rdf+turtle',
],
'json' => [
'application/json',
......@@ -37,15 +29,108 @@ final class UtilsTest extends TestCase
],
'nt' => ['text/plain'],
];
}
/**
* Test Loadspeakr content negotiation.
*
* @dataProvider requestProvider
* @covers \uib\ub\loadspeakr\Utils::getBestContentType
*/
public function testGetBestContentType($expected, $requestHeader): void
{
self::assertSame($expected, Utils::getBestContentType($requestHeader, $this->http_accept));
}
/**
* Test Loadspeakr extension negotiation.
*
* @dataProvider requestGetExtensionProvider
* @covers \uib\ub\loadspeakr\Utils::getExtension
*/
public function testGetExtension($expected, $mediaType): void
{
self::assertEquals($expected, Utils::getExtension($mediaType, $this->http_accept));
}
/**
* @see testGetBestContentType
*/
public function requestProvider(): array
{
return [
'null header' => [
'expected' => 'text/html',
'media-type' => 'text/html',
],
'Empty header' => [
'expected' => 'text/html',
'media-type' => 'text/html',
],
'Empty string' => [
'expected' => 'text/html',
'media-type' => 'text/html',
],
'Illegal string' => [
'expected' => 'text/html',
'media-type' => 'foo, bar',
],
'html request' => [
'expected' => 'text/html',
'media-type' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
],
'CSS request' => [
'expected' => 'text/html',
'media-type' => 'text/css,*/*;q=0.1',
],
'JSON request' => [
'expected' => 'application/json',
'media-type' => 'json/txt,application/json,*/*;q=0.1',
],
'Illegal JSON' => [
'expected' => 'application/json',
'media-type' => 'json/born, application/jsons, */* ;q=0.1',
],
];
}
self::assertSame('text/html', Utils::getBestContentType(null, $http_accept));
self::assertSame('text/html', Utils::getBestContentType('', $http_accept));
self::assertSame('text/html', Utils::getBestContentType('foo, bar', $http_accept));
self::assertSame('text/html', Utils::getBestContentType('text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', $http_accept));
self::assertSame('text/html', Utils::getBestContentType('text/css,*/*;q=0.1', $http_accept));
self::assertSame('application/json', Utils::getBestContentType('json/txt,application/json,*/*;q=0.1', $http_accept));
self::assertSame('application/json', Utils::getBestContentType('json/txt, application/json, */* ;q=0.1', $http_accept));
self::assertSame('application/json', Utils::getBestContentType('json/txt,application/foo,*/*;q=0.1', $http_accept));
/**
* @see testGetExtension
*/
public function requestGetExtensionProvider(): array
{
return [
'Text html' => [
'expected' => 'html',
'media-type' => 'text/html',
],
'Unknown media type' => [
'expected' => 'html',
'media-type' => 'foo/bar',
],
'Empty string' => [
'expected' => 'html',
'media-type' => '',
],
'html request' => [
'expected' => 'html',
'media-type' => 'text/html',
],
'CSS request' => [
'expected' => 'json',
'media-type' => 'application/json',
],
'JSON request' => [
'expected' => 'json',
'media-type' => 'application/json',
],
'Illegal JSON' => [
'expected' => 'json',
'media-type' => 'application/json',
],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment