|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace FastForward\Http\Message\Tests\Header; |
| 6 | + |
| 7 | +use FastForward\Http\Message\Header\ContentType; |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +#[CoversClass(ContentType::class)] |
| 13 | +class ContentTypeTest extends TestCase |
| 14 | +{ |
| 15 | + #[DataProvider('providerHeaderStrings')] |
| 16 | + public function testFromHeaderString(string $header, ?ContentType $expected): void |
| 17 | + { |
| 18 | + $this->assertSame($expected, ContentType::fromHeaderString($header)); |
| 19 | + } |
| 20 | + |
| 21 | + #[DataProvider('providerCharsets')] |
| 22 | + public function testGetCharset(string $header, ?string $expectedCharset): void |
| 23 | + { |
| 24 | + $this->assertSame($expectedCharset, ContentType::getCharset($header)); |
| 25 | + } |
| 26 | + |
| 27 | + public function testWithCharset(): void |
| 28 | + { |
| 29 | + $expected = 'application/json; charset=utf-8'; |
| 30 | + $this->assertSame($expected, ContentType::ApplicationJson->withCharset('utf-8')); |
| 31 | + } |
| 32 | + |
| 33 | + #[DataProvider('providerJsonCases')] |
| 34 | + public function testIsJson(ContentType $case, bool $expected): void |
| 35 | + { |
| 36 | + $this->assertSame($expected, $case->isJson()); |
| 37 | + } |
| 38 | + |
| 39 | + #[DataProvider('providerXmlCases')] |
| 40 | + public function testIsXml(ContentType $case, bool $expected): void |
| 41 | + { |
| 42 | + $this->assertSame($expected, $case->isXml()); |
| 43 | + } |
| 44 | + |
| 45 | + #[DataProvider('providerTextCases')] |
| 46 | + public function testIsText(ContentType $case, bool $expected): void |
| 47 | + { |
| 48 | + $this->assertSame($expected, $case->isText()); |
| 49 | + } |
| 50 | + |
| 51 | + #[DataProvider('providerMultipartCases')] |
| 52 | + public function testIsMultipart(ContentType $case, bool $expected): void |
| 53 | + { |
| 54 | + $this->assertSame($expected, $case->isMultipart()); |
| 55 | + } |
| 56 | + |
| 57 | + public static function providerHeaderStrings(): array |
| 58 | + { |
| 59 | + return [ |
| 60 | + 'json with charset' => ['application/json; charset=utf-8', ContentType::ApplicationJson], |
| 61 | + 'html without charset' => ['text/html', ContentType::TextHtml], |
| 62 | + 'invalid type' => ['application/invalid', null], |
| 63 | + 'empty string' => ['', null], |
| 64 | + ]; |
| 65 | + } |
| 66 | + |
| 67 | + public static function providerCharsets(): array |
| 68 | + { |
| 69 | + return [ |
| 70 | + 'json with utf-8' => ['application/json; charset=utf-8', 'utf-8'], |
| 71 | + 'html with iso' => ['text/html; charset=ISO-8859-1', 'ISO-8859-1'], |
| 72 | + 'plain text without charset' => ['text/plain', null], |
| 73 | + 'header with extra spaces' => [' application/json ; charset=UTF-8 ', 'UTF-8'], |
| 74 | + 'case insensitive' => ['application/json; CharSet=UTF-8', 'UTF-8'], |
| 75 | + 'empty header' => ['', null], |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + public static function providerJsonCases(): array |
| 80 | + { |
| 81 | + return [ |
| 82 | + [ContentType::ApplicationJson, true], |
| 83 | + [ContentType::TextHtml, false], |
| 84 | + [ContentType::ApplicationXml, false], |
| 85 | + ]; |
| 86 | + } |
| 87 | + |
| 88 | + public static function providerXmlCases(): array |
| 89 | + { |
| 90 | + return [ |
| 91 | + [ContentType::ApplicationXml, true], |
| 92 | + [ContentType::TextXml, true], |
| 93 | + [ContentType::ApplicationJson, false], |
| 94 | + [ContentType::TextHtml, false], |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + public static function providerTextCases(): array |
| 99 | + { |
| 100 | + return [ |
| 101 | + [ContentType::TextPlain, true], |
| 102 | + [ContentType::TextHtml, true], |
| 103 | + [ContentType::TextCss, true], |
| 104 | + [ContentType::TextCsv, true], |
| 105 | + [ContentType::TextXml, true], |
| 106 | + [ContentType::ApplicationJson, false], |
| 107 | + [ContentType::ImagePng, false], |
| 108 | + ]; |
| 109 | + } |
| 110 | + |
| 111 | + public static function providerMultipartCases(): array |
| 112 | + { |
| 113 | + return [ |
| 114 | + [ContentType::MultipartFormData, true], |
| 115 | + [ContentType::ApplicationFormUrlencoded, false], |
| 116 | + [ContentType::ApplicationJson, false], |
| 117 | + ]; |
| 118 | + } |
| 119 | +} |
0 commit comments