3 use seekat\API\ContentType
;
7 class ContentTypeTest
extends BaseTest
11 * @dataProvider provideAPI
13 function testIsAbleToApplyVersionedContentTypeToApi($api) {
14 $api = (new ContentType($api->getVersion(), "json"))->apply($api);
16 $this->getVersionedContentType("json")->value
,
17 $api->export()["headers"]["Accept"]);
19 $api = (new ContentType($api->getVersion(), "+raw"))->apply($api);
21 $this->getVersionedContentType("+raw")->value
,
22 $api->export()["headers"]["Accept"]);
24 $api = (new ContentType($api->getVersion(), ".html"))->apply($api);
26 $this->getVersionedContentType(".html")->value
,
27 $api->export()["headers"]["Accept"]);
32 * @dataProvider provideAPI
34 function testIsAbleToApplyBasicContentTypeToApi($api) {
35 $api = (new ContentType($api->getVersion(), "text/plain"))->apply($api);
36 $this->assertEquals("text/plain", $api->export()["headers"]["Accept"]);
42 function testAllowsToRegisterAndUnregisterContentTypeHandlers() {
43 $this->assertFalse(ContentType
::registered("foobar"));
44 ContentType
::register(new class implements ContentType\Handler
{
45 function types() : array {
48 function encode($data): Body
{
51 function decode(Body
$body) : string {
52 return (string) $body;
55 $this->assertTrue(ContentType
::registered("foobar"));
56 ContentType
::unregister("foobar");
57 $this->assertFalse(ContentType
::registered("foobar"));
63 function testAcceptsContentTypeHeader() {
64 $ct = new ContentType
;
65 $ct->setContentTypeHeader(new Header("Content-Type", "TitleCase"));
66 $this->assertSame("TitleCase", $ct->getType());
67 $ct->setContentTypeHeader(new Header("content-type", "lowercase"));
68 $this->assertSame("lowercase", $ct->getType());
74 function testDoesNotAcceptNonContentTypeHeader() {
75 $this->expectException(\seekat\Exception\InvalidArgumentException
::class);
76 (new ContentType
)->setContentTypeHeader(new Header("ContentType"));
82 function testThrowsOnUnknownContentType() {
83 $this->expectException(\seekat\Exception\UnexpectedValueException
::class);
84 $ct = new ContentType(3, "foo");
85 $ct->decode((new Body
)->append("foo"));
91 function testHandlesJson() {
92 $this->assertTrue(ContentType
::registered("json"));
93 $ct = new ContentType(3, "application/json");
94 $result = $ct->decode((new Body())->append("[1,2,3]"));
95 $this->assertEquals([1, 2, 3], $result);
101 * @depends testHandlesJson
103 function testThrowsOnInvalidJson(ContentType
$ct) {
104 $this->expectException(\seekat\Exception\UnexpectedValueException
::class);
105 $ct->decode((new Body
)->append("yaml:\n - data"));
111 function testHandlesBase64() {
112 $this->assertTrue(ContentType
::registered("base64"));
113 $ct = new ContentType(3,"base64");
114 $result = $ct->decode((new Body())->append(base64_encode("This is a test")));
115 $this->assertEquals("This is a test", $result);
121 * @depends testHandlesBase64
123 function testThrowsOnInvalidBase64(ContentType
$ct) {
124 $this->expectException(\seekat\Exception\UnexpectedValueException
::class);
125 $ct->decode((new Body
)->append("[1,2,3]"));
131 function testHandlesOctetStream() {
132 $this->assertTrue(ContentType
::registered("application/octet-stream"));
133 $ct = new ContentType(3,"application/octet-stream");
134 $result = $ct->decode((new Body
)->append("This is a test"));
135 $this->assertIsResource($result);
137 $this->assertEquals("This is a test", stream_get_contents($result));
143 function testHandlesData() {
144 $this->assertTrue(ContentType
::registered("text/plain"));
145 $ct = new ContentType(3,"text/plain");
146 $result = $ct->decode((new Body
)->append("This is a test"));
147 $this->assertIsString($result);
148 $this->assertEquals("This is a test", $result);
152 * @param string $type
153 * @return Header Content-Type header
155 private function getVersionedContentType($type) {
164 return new Header("Content-Type",
165 sprintf("application/vnd.github.v3%s", $type));