2bdeb486712467b645aa03c6692a335977c269ac
3 use seekat\API\ContentType
;
7 class ContentTypeTest
extends BaseTest
11 * @dataProvider provideAPI
13 function testIsAbleToApplyVersionedContentTypeToApi($api) {
14 $api = ContentType
::apply($api, "json");
16 $this->getVersionedContentType("json")->value
,
17 $api->export()["headers"]["Accept"]);
19 $api = ContentType
::apply($api, "+raw");
21 $this->getVersionedContentType("+raw")->value
,
22 $api->export()["headers"]["Accept"]);
24 $api = ContentType
::apply($api, ".html");
26 $this->getVersionedContentType(".html")->value
,
27 $api->export()["headers"]["Accept"]);
32 * @dataProvider provideAPI
34 function testIsAbleToApplyBasicContentTypeToApi($api) {
35 $api = ContentType
::apply($api, "text/plain");
36 $this->assertEquals("text/plain", $api->export()["headers"]["Accept"]);
42 function testUserCanOverrideApiVersion() {
43 $this->assertEquals(3, ContentType
::version(2));
44 $this->assertEquals(2, ContentType
::version(3));
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"));
61 function testAcceptsContentTypeHeader() {
62 new ContentType(new Header("Content-Type"));
63 new ContentType(new Header("content-type"));
68 * @expectedException \seekat\Exception\InvalidArgumentException
70 function testDoesNotAcceptNonContentTypeHeader() {
71 new ContentType(new Header("ContentType"));
76 * @expectedException \seekat\Exception\UnexpectedValueException
78 function testThrowsOnUnknownContentType() {
79 $ct = new ContentType($this->getVersionedContentType("foo"));
80 $ct->parseBody((new Body
)->append("foo"));
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);
96 * @depends testHandlesJson
97 * @expectedException \seekat\Exception\UnexpectedValueException
99 function testThrowsOnInvalidJson(ContentType
$ct) {
100 $ct->parseBody((new Body
)->append("yaml:\n - data"));
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);
116 * @depends testHandlesBase64
117 * @expectedException \seekat\Exception\UnexpectedValueException
119 function testThrowsOnInvalidBase64(ContentType
$ct) {
120 $ct->parseBody((new Body
)->append("[1,2,3]"));
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);
132 $this->assertEquals("This is a test", stream_get_contents($result));
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);
147 * @param string $type
148 * @return Header Content-Type header
150 private function getVersionedContentType($type) {
159 return new Header("Content-Type",
160 sprintf("application/vnd.github.v%d%s", ContentType
::version(), $type));