[limb-svn] r5795 - in 3.x/trunk/limb/net: src tests/cases

svn at limb-project.com svn at limb-project.com
Thu May 3 17:43:28 MSD 2007


Author: pachanga
Date: 2007-05-03 17:43:28 +0400 (Thu, 03 May 2007)
New Revision: 5795
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5795

Added:
   3.x/trunk/limb/net/tests/cases/lmbMimeTypeTest.class.php
Modified:
   3.x/trunk/limb/net/src/lmbMimeType.class.php
Log:
-- lmbMimeType tests added
-- lmbMimeType :: getFileMimeType($file) added

Modified: 3.x/trunk/limb/net/src/lmbMimeType.class.php
===================================================================
--- 3.x/trunk/limb/net/src/lmbMimeType.class.php	2007-05-03 13:04:09 UTC (rev 5794)
+++ 3.x/trunk/limb/net/src/lmbMimeType.class.php	2007-05-03 13:43:28 UTC (rev 5795)
@@ -1,13 +1,13 @@
 <?php
-/**
- * Limb Web Application Framework
- *
- * @link http://limb-project.com
- *
- * @copyright  Copyright &copy; 2004-2007 BIT
- * @license    LGPL http://www.gnu.org/copyleft/lesser.html
- * @version    $Id$
- * @package    net
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id$
+ * @package    net
  */
 class lmbMimeType
 {
@@ -34,29 +34,38 @@
     'avi' => 'video/avi',
     'mpg' => 'video/mpeg',
     'js' => 'text/javascript'
-    );
-  static protected $flipped_mime_types = NULL;
+  );
 
+  static protected $flipped_mime_types = array();
+
   static function getExtension($mime_type)
   {
-    $mime_type = strtolower($mime_type);
-    if ( !is_array(self :: $flipped_mime_types) )
-    {
+    if(!self :: $flipped_mime_types)
       self :: $flipped_mime_types = array_flip(self :: $mime_types);
-    }
 
+    $mime_type = strtolower($mime_type);
+
     return isset(self :: $flipped_mime_types[$mime_type])
       ? self :: $flipped_mime_types[$mime_type]
-      : NULL;
+      : null;
   }
 
   static function getMimeType($extension)
   {
-    $extension = strtolower($extension);
+    $extension = ltrim(strtolower($extension), '.');
 
     return isset(self :: $mime_types[$extension])
       ? self :: $mime_types[$extension]
-      : NULL;
+      : null;
   }
+
+  static function getFileMimeType($file)
+  {
+    if($info = pathinfo($file))
+    {
+      if(isset($info['extension']))
+        return self :: getMimeType($info['extension']);
+    }
+  }
 }
 ?>
\ No newline at end of file

Added: 3.x/trunk/limb/net/tests/cases/lmbMimeTypeTest.class.php
===================================================================
--- 3.x/trunk/limb/net/tests/cases/lmbMimeTypeTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/net/tests/cases/lmbMimeTypeTest.class.php	2007-05-03 13:43:28 UTC (rev 5795)
@@ -0,0 +1,55 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id: lmbIpTest.class.php 5001 2007-02-08 15:36:45Z pachanga $
+ * @package    net
+ */
+lmb_require('limb/net/src/lmbMimeType.class.php');
+
+class lmbMimeTypeTest extends UnitTestCase
+{
+  function getExtensionFailed()
+  {
+    $this->assertNull(lmbMimeType :: getExtension('foo'));
+  }
+
+  function testGetExtension()
+  {
+    $this->assertEqual(lmbMimeType :: getExtension('text/html'), 'html');
+    $this->assertEqual(lmbMimeType :: getExtension('text/rtf'), 'rtf');
+  }
+
+  function testGetMimeTypeForExtensionFailed()
+  {
+    $this->assertNull(lmbMimeType :: getMimeType('booo'));
+  }
+
+  function testGetMimeTypeForExtension()
+  {
+    $this->assertEqual(lmbMimeType :: getMimeType('html'), 'text/html');
+    $this->assertEqual(lmbMimeType :: getMimeType('rtf'), 'text/rtf');
+  }
+
+  function testGetMimeTypeExtensionWithDot()
+  {
+    $this->assertEqual(lmbMimeType :: getMimeType('.html'), 'text/html');
+    $this->assertEqual(lmbMimeType :: getMimeType('.rtf'), 'text/rtf');
+  }
+
+  function testGetMimeTypeForFileFailed()
+  {
+    $this->assertNull(lmbMimeType :: getFileMimeType('booo.fg'));
+  }
+
+  function testGetMimeTypeForFile()
+  {
+    $this->assertEqual(lmbMimeType :: getFileMimeType('test.html'), 'text/html');
+    $this->assertEqual(lmbMimeType :: getFileMimeType('test.rtf'), 'text/rtf');
+  }
+}
+?>
\ No newline at end of file



More information about the limb-svn mailing list