Browse Source

Test and correct ancillary features

remotes/github/master
J. King 4 years ago
parent
commit
7543a91c6b
  1. 3
      composer.json
  2. 6
      composer.lock
  3. 7
      lib/MimeType.php
  4. 47
      tests/cases/MimeTypeTest.php

3
composer.json

@ -15,7 +15,8 @@
"php": "^7.1"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.3"
"bamarni/composer-bin-plugin": "^1.3",
"ext-intl": "*"
},
"config": {
"platform": {

6
composer.lock

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "8ccabde36f4d647880e48bda8d68b1bc",
"content-hash": "8caea8ec6313d5a0b1057c54c7782356",
"packages": [],
"packages-dev": [
{
@ -55,7 +55,9 @@
"platform": {
"php": "^7.1"
},
"platform-dev": [],
"platform-dev": {
"ext-intl": "*"
},
"platform-overrides": {
"php": "7.1.33"
},

7
lib/MimeType.php

@ -89,7 +89,7 @@ PATTERN;
for ($a = 0; $a < strlen($bytes); $a++) {
$c = $bytes[$a];
$p = ord($c);
$out .= $p < 0x80 ? $c : self::CHAR_MAP[$a];
$out .= $p < 0x80 ? $c : self::CHAR_MAP[$p];
}
return $out;
}
@ -97,7 +97,10 @@ PATTERN;
public static function encode(string $chars): ?string {
$map = array_combine(array_values(self::CHAR_MAP), range(chr(0x80), chr(0xFF)));
$out = "";
foreach (preg_split("<>u", $chars) as $c) {
$set = array_reverse(preg_split("<>u", $chars));
array_pop($set);
while (sizeof($set) > 1) {
$c = array_pop($set);
if (strlen($c) === 1) {
$out .= $c;
} elseif (isset($map[$c])) {

47
tests/cases/MimeTypeTest.php

@ -39,4 +39,51 @@ class MimeTypeTest extends \PHPUnit\Framework\TestCase {
}
}
}
public function testDecodeAByteString(): void {
// set up the text with the Intl extension
$input = "";
$exp = "";
for ($a = 0; $a <= 0xFF; $a++) {
$input .= chr($a);
$exp .= \IntlChar::chr($a);
}
// perform the test
$this->assertSame($exp, Mime::decode($input));
}
public function testEncodeAValidString(): void {
// set up the text with the Intl extension
$input = "";
$exp = "";
for ($a = 0; $a <= 0xFF; $a++) {
$exp .= chr($a);
$input .= \IntlChar::chr($a);
}
// perform the test
$this->assertSame($exp, Mime::encode($input));
}
public function testEncodeAnInvalidString(): void {
// set up the text with the Intl extension
$input = "!\u{1F4A9}!";
// perform the test
$this->assertNull(Mime::encode($input));
}
public function testParseAByteString(): void {
$input = "application/unknown;param=\"\xE9tude\"";
$exp = "application/unknown;param=\"\u{E9}tude\"";
$this->assertSame($exp, (string) Mime::parseBytes($input));
}
public function testAccessInstanceProperties(): void {
$input = "TEXT/HTML; VERSION=3.2; charset=utf-8; charset=iso-8859-1;";
$obj = Mime::parse($input);
$this->assertInstanceOf(Mime::class, $obj);
$this->assertSame("text", $obj->type);
$this->assertSame("html", $obj->subtype);
$this->assertSame("text/html", $obj->essence);
$this->assertSame(['version' => "3.2", 'charset' => "utf-8"], $obj->params);
}
}

Loading…
Cancel
Save