diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index fd04e594435e608806e75fdb4b55baed5f8c80fa..5288b6ef20f71eb2676ac9556fe1fdd819360694 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php @@ -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', + ], + ]; } }