* @license PHP License * @package WB * @subpackage unittest */ WBClass::load( 'WBException_Datasource'); /** * Unit Test * * Test file * * @version 0.1.0 * @package WB * @subpackage unittest */ class TestCaseVFSMimeType extends UnitTestCase { protected $knownFiles = array( 'text' => array( 'README.txt' => array('text', 'plain'), ), 'application' => array( 'manual-en.pdf' => array('application', 'pdf'), ), 'image' => array( 'bluemchen.gif' => array('image', 'gif'), 'bearBlind.jpg' => array('image', 'jpeg'), 'frogstonea.jpg' => array('image', 'jpeg'), 'fuertabruta0279.jpg' => array('image', 'jpeg'), 'p1070889.jpg' => array('image', 'jpeg'), 'ssl10393.jpg' => array('image', 'jpeg'), 'smiley-64x64.png' => array('image', 'png'), ), 'video' => array( 'casque-test.3gp' => array('video', '3gpp'), 'container.mov' => array('video', 'quicktime'), 'aitd-29.4xm' => array('video', '4xm'), 'gameoverp01s03.4xm' => array('video', '4xm'), 'aasc.avi' => array('video', 'x-msvideo'), 'snowboardjump10.wmv' => array('video', 'x-ms-asf'), 'boarder1-320x180-356k.flv' => array('video', 'x-flv'), 'boarder1-640x360-864k.mp4' => array('video', 'mp4'), 'boarder2-320x180-356k.flv' => array('video', 'x-flv'), 'boarder2-720p.mpeg' => array('video', 'mpeg'), // 'boarder2-720p.m2t' => array('video', 'octet-stream'), ), 'application' => array( 'text.txt.bz2' => array('application', 'x-bzip2'), 'text.txt.gz' => array('application', 'x-gzip'), ) ); public function testInit() { $this->assertTrue(function_exists('finfo_open'), 'finfo_open is not a function!'); $this->assertTrue(function_exists('mime_content_type'), 'mime_content_type is not a function!'); } public function testGetMimeType() { $majors = UnitTest_VFS_Sample::getMajors(); foreach ($majors as $major) { $dir = UnitTest_VFS_Sample::getDirIterator($major); foreach ($dir as $name) { if ($name->isDot() || $name->isDir()) { continue; } $file = $name->getFilename(); if (!isset($this->knownFiles[$major][$file])) { echo "file " . $file . " is not in list of known file!
\n"; continue; } $mime = $this->getMimeType($name->getPath() . '/' . $file); $this->assertEqual($mime[0], $this->knownFiles[$major][$file][0], $file . ': major mime type does not match! expected: ' . $this->knownFiles[$major][$file][0] . ' got ' . $mime[0]); $this->assertEqual($mime[1], $this->knownFiles[$major][$file][1], $file . ': minor mime type does not match! expected: ' . $this->knownFiles[$major][$file][1] . ' got ' . $mime[1]); $this->assertTrue(true); } } } /** * guess mime type * * Try several methods to guess mime type and normalize output * * @param string $file * @return array major and minor mime type */ protected function getMimeType($file) { $magic = WBParam::get('wb/dir/system') . '/resource/mime/magic'; // import file using mime-type importer if (function_exists('finfo_open')) { $finfo = new finfo(FILEINFO_MIME, $magic); $mime = $finfo->file($file); } else { exec('file -bi ' . $file, $out); $mime = array_shift($out); } if (!$mime || empty($mime)) { $mime = 'unknown/unknown'; } // separate major and minor mime type $mime = explode('/', $mime, 2); // strip optional charset and stuff list($mime[1]) = explode(' ', $mime[1]); list($mime[1]) = explode(';', $mime[1]); return $mime; } } ?>