1<?php 2require_once 'MIME/Type.php'; 3 4class MIME_TypeTest extends PHPUnit_Framework_TestCase 5{ 6 public function testParseParameterPearError() 7 { 8 $mt = new MIME_Type(); 9 $mt->parse(new PEAR_Error('this is an error')); 10 $this->assertEquals('', $mt->media); 11 } 12 13 public function testParse() 14 { 15 $mt = new MIME_Type(); 16 $mt->parse('application/ogg;description=Hello there!;asd=fgh'); 17 $this->assertEquals('application', $mt->media); 18 $this->assertEquals('ogg' , $mt->subType); 19 20 $params = array( 21 'description' => array('Hello there!', ''), 22 'asd' => array('fgh', '') 23 ); 24 $this->assertEquals(2, count($mt->parameters)); 25 foreach ($params as $name => $param) { 26 $this->assertTrue(isset($mt->parameters[$name])); 27 $this->assertInstanceOf('MIME_Type_Parameter', $mt->parameters[$name]); 28 $this->assertEquals($name, $mt->parameters[$name]->name); 29 $this->assertEquals($param[0], $mt->parameters[$name]->value); 30 $this->assertEquals($param[1], $mt->parameters[$name]->comment); 31 } 32 } 33 34 public function testParseAgain() 35 { 36 $mt = new MIME_Type(); 37 $mt->parse('application/ogg;description=Hello there!;asd=fgh'); 38 $this->assertEquals(2, count($mt->parameters)); 39 40 $mt->parse('text/plain;hello=there!'); 41 $this->assertEquals(1, count($mt->parameters)); 42 } 43 44 public function testHasParameters() 45 { 46 $this->assertFalse(MIME_Type::hasParameters('text/plain')); 47 $this->assertFalse(MIME_Type::hasParameters('text/*')); 48 $this->assertFalse(MIME_Type::hasParameters('*/*')); 49 $this->assertTrue(MIME_Type::hasParameters('text/xml;description=test')); 50 $this->assertTrue(MIME_Type::hasParameters('text/xml;one=test;two=three')); 51 } 52 53 public function testGetParameters() 54 { 55 $this->assertEquals( 56 array(), 57 MIME_Type::getParameters('text/plain') 58 ); 59 //rest is tested in testParse() 60 } 61 62 public function testStripParameters() 63 { 64 $this->assertEquals( 65 'text/plain', 66 MIME_Type::stripParameters('text/plain') 67 ); 68 $this->assertEquals( 69 'text/plain', 70 MIME_Type::stripParameters('text/plain;asd=def') 71 ); 72 $this->assertEquals( 73 'text/plain', 74 MIME_Type::stripParameters('text/plain;asd=def;ghj=jkl') 75 ); 76 } 77 78 public function testStripComments() 79 { 80 $this->assertEquals('def', MIME_Type::stripComments('(abc)def(ghi)', $null)); 81 $this->assertEquals('def', MIME_Type::stripComments('(abc)def', $null)); 82 $this->assertEquals('def', MIME_Type::stripComments('def(ghi)', $null)); 83 } 84 85 public function testStripCommentsEscaped() 86 { 87 $comment = ''; 88 $this->assertEquals( 89 'def', MIME_Type::stripComments('(\)abc)def(\))', $comment) 90 ); 91 $this->assertEquals(')abc )', $comment); 92 } 93 94 public function testStripCommentsEscapedString() 95 { 96 $comment = false; 97 $this->assertEquals( 98 'foo', MIME_Type::stripComments('\\foo(abc)', $comment) 99 ); 100 $this->assertEquals('abc', $comment); 101 } 102 103 public function testStripCommentsQuoted() 104 { 105 $this->assertEquals('def', MIME_Type::stripComments('(a"bc)def")def', $null)); 106 $this->assertEquals('(abc)def', MIME_Type::stripComments('"(abc)def"', $null)); 107 } 108 109 public function testStripCommentsParameterComment() 110 { 111 $comment = ''; 112 $this->assertEquals( 113 'def', 114 MIME_Type::stripComments('(abc)def(ghi)', $comment) 115 ); 116 $this->assertEquals('abc ghi', $comment); 117 } 118 119 public function testGetMedia() 120 { 121 $this->assertEquals('text', MIME_Type::getMedia('text/plain')); 122 $this->assertEquals('application', MIME_Type::getMedia('application/ogg')); 123 $this->assertEquals('*', MIME_Type::getMedia('*/*')); 124 } 125 126 public function testGetSubType() 127 { 128 $this->assertEquals('plain', MIME_Type::getSubType('text/plain')); 129 $this->assertEquals('ogg', MIME_Type::getSubType('application/ogg')); 130 $this->assertEquals('*', MIME_Type::getSubType('*/*')); 131 $this->assertEquals('plain', MIME_Type::getSubType('text/plain;a=b')); 132 } 133 134 public function testGet() 135 { 136 $mt = new MIME_Type('text/xml'); 137 $this->assertEquals('text/xml', $mt->get()); 138 139 $mt = new MIME_Type('text/xml; this="is"; a="parameter" (with a comment)'); 140 $this->assertEquals( 141 'text/xml; this="is"; a="parameter" (with a comment)', 142 $mt->get() 143 ); 144 } 145 146 public function testIsExperimental() 147 { 148 $this->assertTrue(MIME_Type::isExperimental('text/x-test')); 149 $this->assertTrue(MIME_Type::isExperimental('image/X-test')); 150 $this->assertFalse(MIME_Type::isExperimental('text/plain')); 151 } 152 153 public function testIsVendor() 154 { 155 $this->assertTrue(MIME_Type::isVendor('application/vnd.openoffice')); 156 $this->assertFalse(MIME_Type::isVendor('application/vendor.openoffice')); 157 $this->assertFalse(MIME_Type::isVendor('vnd/fsck')); 158 } 159 160 public function testIsWildcard() 161 { 162 $this->assertTrue(MIME_Type::isWildcard('*/*')); 163 $this->assertTrue(MIME_Type::isWildcard('image/*')); 164 $this->assertFalse(MIME_Type::isWildcard('text/plain')); 165 } 166 167 public function testWildcardMatch() { 168 $this->assertTrue(MIME_Type::wildcardMatch('*/*', 'image/png')); 169 $this->assertTrue(MIME_Type::wildcardMatch('image/*', 'image/png')); 170 $this->assertFalse(MIME_Type::wildcardMatch('image/*', 'text/plain')); 171 } 172 173 public function testWildcardMatchNoWildcard() 174 { 175 $this->assertFalse(MIME_Type::wildcardMatch('image/foo', 'image/png')); 176 } 177 178 public function testAddParameter() 179 { 180 $mt = new MIME_Type('image/png; foo=bar'); 181 $mt->addParameter('baz', 'val', 'this is a comment'); 182 $res = $mt->get(); 183 $this->assertContains('foo=', $res); 184 $this->assertContains('bar', $res); 185 186 $this->assertContains('baz=', $res); 187 $this->assertContains('val', $res); 188 $this->assertContains('(this is a comment)', $res); 189 } 190 191 public function testRemoveParameter() 192 { 193 $mt = new MIME_Type('image/png; foo=bar'); 194 $mt->addParameter('baz', 'val', 'this is a comment'); 195 $mt->removeParameter('foo'); 196 $res = $mt->get(); 197 $this->assertNotContains('foo=', $res); 198 $this->assertNotContains('bar', $res); 199 $this->assertContains('baz=', $res); 200 } 201 202 public function testAutoDetect() 203 { 204 $dir = dirname(__FILE__) . '/files/'; 205 206 $mt = new MIME_Type( 207 MIME_Type::autoDetect($dir . 'example.png') 208 ); 209 $this->assertInstanceOf('MIME_Type', $mt); 210 $this->assertEquals('image', $mt->media); 211 $this->assertEquals('png', $mt->subType); 212 213 $mt = new MIME_Type( 214 MIME_Type::autoDetect($dir . 'example.jpg') 215 ); 216 $this->assertInstanceOf('MIME_Type', $mt); 217 $this->assertEquals('image', $mt->media); 218 $this->assertEquals('jpeg', $mt->subType); 219 } 220 221 public function testAutoDetectNonexistingFile() 222 { 223 $res = MIME_Type::autoDetect('/this/file/does/not/exist'); 224 $this->assertInstanceOf('PEAR_Error', $res); 225 $this->assertContains('doesn\'t exist', $res->getMessage()); 226 } 227 228 public function testAutoDetectFinfo() 229 { 230 $mt = new MIME_Type(); 231 $mt->useMimeContentType = false; 232 $mt->useFileCmd = false; 233 $mt->useExtension = false; 234 $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg'); 235 $this->assertNotInstanceOf('PEAR_Error', $type); 236 $this->assertEquals('image', $mt->media); 237 $this->assertEquals('jpeg', $mt->subType); 238 } 239 240 public function testAutoDetectFinfoMagic() 241 { 242 $mt = new MIME_Type(); 243 $mt->magicFile = dirname(__FILE__) . '/TypeTest.magic'; 244 $mt->useMimeContentType = false; 245 $mt->useFileCmd = false; 246 $mt->useExtension = false; 247 248 $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.php'); 249 $this->assertNotInstanceOf('PEAR_Error', $type); 250 $this->assertEquals('text', $mt->media); 251 $this->assertEquals('x-unittest', $mt->subType); 252 } 253 254 /** 255 * @expectedException PHPUnit_Framework_Error 256 */ 257 public function testAutoDetectFinfoNonExistingMagic() 258 { 259 $mt = new MIME_Type(); 260 $mt->magicFile = dirname(__FILE__) . '/magicdoesnotexist'; 261 $mt->useMimeContentType = false; 262 $mt->useFileCmd = false; 263 $mt->useExtension = false; 264 265 $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.php'); 266 $this->assertInstanceOf('PEAR_Error', $type); 267 } 268 269 public function testAutoDetectMimeContentType() 270 { 271 $mt = new MIME_Type(); 272 $mt->useFinfo = false; 273 $mt->useFileCmd = false; 274 $mt->useExtension = false; 275 $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg'); 276 $this->assertEquals('image', $mt->media); 277 $this->assertEquals('jpeg', $mt->subType); 278 } 279 280 public function testAutoDetectFileCommand() 281 { 282 $mt = new MIME_Type(); 283 $mt->useFinfo = false; 284 $mt->useMimeContentType = false; 285 $mt->useExtension = false; 286 $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg'); 287 $this->assertEquals('image', $mt->media); 288 $this->assertEquals('jpeg', $mt->subType); 289 } 290 291 public function testAutoDetectFileCommandMagic() 292 { 293 $mt = new MIME_Type(); 294 $mt->magicFile = dirname(__FILE__) . '/TypeTest.magic'; 295 $mt->useFinfo = false; 296 $mt->useMimeContentType = false; 297 $mt->useExtension = false; 298 $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.php'); 299 $this->assertEquals('text', $mt->media); 300 $this->assertEquals('x-unittest', $mt->subType); 301 } 302 303 public function testAutoDetectExtension() 304 { 305 $mt = new MIME_Type(); 306 $mt->useFinfo = false; 307 $mt->useMimeContentType = false; 308 $mt->useFileCmd = false; 309 $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg'); 310 $this->assertEquals('image', $mt->media); 311 $this->assertEquals('jpeg', $mt->subType); 312 } 313 314 public function testAutoDetectError() 315 { 316 $mt = new MIME_Type(); 317 $mt->useFinfo = false; 318 $mt->useMimeContentType = false; 319 $mt->useFileCmd = false; 320 $mt->useExtension = false; 321 322 $res = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg'); 323 $this->assertInstanceOf('PEAR_Error', $res); 324 $this->assertEquals('', $mt->media); 325 $this->assertEquals('', $mt->subType); 326 } 327 328 public function test_fileAutoDetectNoFileCommand() 329 { 330 $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd'); 331 $cmd = 'thiscommanddoesnotexist'; 332 333 require_once 'System/Command.php'; 334 $method = new ReflectionMethod('MIME_Type', '_fileAutoDetect'); 335 $method->setAccessible(true); 336 $res = $method->invoke( 337 new MIME_Type(), dirname(__FILE__) . '/files/example.jpg' 338 ); 339 $this->assertInstanceOf('PEAR_Error', $res); 340 $this->assertContains('thiscommanddoesnotexist', $res->getMessage()); 341 } 342 343 public function testComments() 344 { 345 $type = new MIME_Type('(UTF-8 Plain Text) text / plain ; charset = utf-8'); 346 $this->assertEquals( 347 'text/plain; charset="utf-8"', $type->get() 348 ); 349 350 $type = new MIME_Type('text (Text) / plain ; charset = utf-8'); 351 $this->assertEquals( 352 'text/plain; charset="utf-8"', $type->get() 353 ); 354 355 $type = new MIME_Type('text / (Plain) plain ; charset = utf-8'); 356 $this->assertEquals( 357 'text/plain; charset="utf-8"', $type->get() 358 ); 359 360 $type = new MIME_Type('text / plain (Plain Text) ; charset = utf-8'); 361 $this->assertEquals( 362 'text/plain; charset="utf-8"', $type->get() 363 ); 364 365 $type = new MIME_Type('text / plain ; (Charset=utf-8) charset = utf-8'); 366 $this->assertEquals( 367 'text/plain; charset="utf-8" (Charset=utf-8)', $type->get() 368 ); 369 370 $type = new MIME_Type('text / plain ; charset (Charset) = utf-8'); 371 $this->assertEquals( 372 'text/plain; charset="utf-8" (Charset)', $type->get() 373 ); 374 375 $type = new MIME_Type('text / plain ; charset = (UTF8) utf-8'); 376 $this->assertEquals( 377 'text/plain; charset="utf-8" (UTF8)', $type->get() 378 ); 379 380 $type = new MIME_Type('text / plain ; charset = utf-8 (UTF-8 Plain Text)'); 381 $this->assertEquals( 382 'text/plain; charset="utf-8" (UTF-8 Plain Text)', $type->get() 383 ); 384 385 $type = new MIME_Type('application/x-foobar;description="bbgh(kdur"'); 386 $this->assertEquals( 387 'application/x-foobar; description="bbgh(kdur"', $type->get() 388 ); 389 390 $type = new MIME_Type('application/x-foobar;description="a \"quoted string\""'); 391 $this->assertEquals( 392 'application/x-foobar; description="a \"quoted string\""', $type->get() 393 ); 394 395 } 396 397 398 public function test_handleDetectionParamPearError() 399 { 400 $method = new ReflectionMethod('MIME_Type', '_handleDetection'); 401 $method->setAccessible(true); 402 403 $err = new PEAR_Error('test'); 404 $ret = $method->invoke(null, $err, false); 405 $this->assertInstanceOf('PEAR_Error', $ret); 406 } 407 408 public function test_handleDetectionEmptyType() 409 { 410 $method = new ReflectionMethod('MIME_Type', '_handleDetection'); 411 $method->setAccessible(true); 412 413 $ret = $method->invoke(null, '', false); 414 $this->assertInstanceOf('PEAR_Error', $ret); 415 416 $ret = $method->invoke(null, false, false); 417 $this->assertInstanceOf('PEAR_Error', $ret); 418 } 419 420 /** 421 * @expectedException PHPUnit_Framework_Error 422 * @expectedExceptionMessage Call to undefined method MIME_TYPE::doesNotExist() 423 */ 424 public function test__callUnknownMethod() 425 { 426 $type = new MIME_Type(); 427 $type->doesNotExist(); 428 } 429 430 /** 431 * @expectedException PHPUnit_Framework_Error 432 * @expectedExceptionMessage Call to undefined method MIME_TYPE::doesNotExist() 433 */ 434 public function test__callStaticUnknownMethod() 435 { 436 MIME_Type::doesNotExist(); 437 } 438} 439?> 440