2bdeb486712467b645aa03c6692a335977c269ac
[m6w6/seekat] / tests / ContentTypeTest.php
1 <?php
2
3 use seekat\API\ContentType;
4 use http\Header;
5 use http\Message\Body;
6
7 class ContentTypeTest extends BaseTest
8 {
9 /**
10 * @group testdox
11 * @dataProvider provideAPI
12 */
13 function testIsAbleToApplyVersionedContentTypeToApi($api) {
14 $api = ContentType::apply($api, "json");
15 $this->assertEquals(
16 $this->getVersionedContentType("json")->value,
17 $api->export()["headers"]["Accept"]);
18
19 $api = ContentType::apply($api, "+raw");
20 $this->assertEquals(
21 $this->getVersionedContentType("+raw")->value,
22 $api->export()["headers"]["Accept"]);
23
24 $api = ContentType::apply($api, ".html");
25 $this->assertEquals(
26 $this->getVersionedContentType(".html")->value,
27 $api->export()["headers"]["Accept"]);
28 }
29
30 /**
31 * @group testdox
32 * @dataProvider provideAPI
33 */
34 function testIsAbleToApplyBasicContentTypeToApi($api) {
35 $api = ContentType::apply($api, "text/plain");
36 $this->assertEquals("text/plain", $api->export()["headers"]["Accept"]);
37 }
38
39 /**
40 * @group testdox
41 */
42 function testUserCanOverrideApiVersion() {
43 $this->assertEquals(3, ContentType::version(2));
44 $this->assertEquals(2, ContentType::version(3));
45 }
46
47 /**
48 * @group testdox
49 */
50 function testAllowsToRegisterAndUnregisterContentTypeHandlers() {
51 $this->assertFalse(ContentType::registered("foobar"));
52 ContentType::register("foobar", function() {});
53 $this->assertTrue(ContentType::registered("foobar"));
54 ContentType::unregister("foobar");
55 $this->assertFalse(ContentType::registered("foobar"));
56 }
57
58 /**
59 * @group testdox
60 */
61 function testAcceptsContentTypeHeader() {
62 new ContentType(new Header("Content-Type"));
63 new ContentType(new Header("content-type"));
64 }
65
66 /**
67 * @group testdox
68 * @expectedException \seekat\Exception\InvalidArgumentException
69 */
70 function testDoesNotAcceptNonContentTypeHeader() {
71 new ContentType(new Header("ContentType"));
72 }
73
74 /**
75 * @group testdox
76 * @expectedException \seekat\Exception\UnexpectedValueException
77 */
78 function testThrowsOnUnknownContentType() {
79 $ct = new ContentType($this->getVersionedContentType("foo"));
80 $ct->parseBody((new Body)->append("foo"));
81 }
82
83 /**
84 * @group testdox
85 */
86 function testHandlesJson() {
87 $this->assertTrue(ContentType::registered("json"));
88 $ct = new ContentType(new Header("Content-Type", "application/json"));
89 $result = $ct->parseBody((new Body())->append("[1,2,3]"));
90 $this->assertEquals([1, 2, 3], $result);
91 return $ct;
92 }
93
94 /**
95 * @group testdox
96 * @depends testHandlesJson
97 * @expectedException \seekat\Exception\UnexpectedValueException
98 */
99 function testThrowsOnInvalidJson(ContentType $ct) {
100 $ct->parseBody((new Body)->append("yaml:\n - data"));
101 }
102
103 /**
104 * @group testdox
105 */
106 function testHandlesBase64() {
107 $this->assertTrue(ContentType::registered("base64"));
108 $ct = new ContentType($this->getVersionedContentType("base64"));
109 $result = $ct->parseBody((new Body())->append(base64_encode("This is a test")));
110 $this->assertEquals("This is a test", $result);
111 return $ct;
112 }
113
114 /**
115 * @group testdox
116 * @depends testHandlesBase64
117 * @expectedException \seekat\Exception\UnexpectedValueException
118 */
119 function testThrowsOnInvalidBase64(ContentType $ct) {
120 $ct->parseBody((new Body)->append("[1,2,3]"));
121 }
122
123 /**
124 * @group testdox
125 */
126 function testHandlesOctetStream() {
127 $this->assertTrue(ContentType::registered("application/octet-stream"));
128 $ct = new ContentType(new Header("Content-Type", "application/octet-stream"));
129 $result = $ct->parseBody((new Body)->append("This is a test"));
130 $this->assertInternalType("resource", $result);
131 rewind($result);
132 $this->assertEquals("This is a test", stream_get_contents($result));
133 }
134
135 /**
136 * @group testdox
137 */
138 function testHandlesData() {
139 $this->assertTrue(ContentType::registered("text/plain"));
140 $ct = new ContentType(new Header("Content-Type", "text/plain"));
141 $result = $ct->parseBody((new Body)->append("This is a test"));
142 $this->assertInternalType("string", $result);
143 $this->assertEquals("This is a test", $result);
144 }
145
146 /**
147 * @param string $type
148 * @return Header Content-Type header
149 */
150 private function getVersionedContentType($type) {
151 switch ($type{0}) {
152 case ".":
153 case "+":
154 case "":
155 break;
156 default:
157 $type = ".$type";
158 }
159 return new Header("Content-Type",
160 sprintf("application/vnd.github.v%d%s", ContentType::version(), $type));
161 }
162 }