Browse Source

add tests

euromark 12 years ago
parent
commit
bebd2fe490
2 changed files with 174 additions and 36 deletions
  1. 52 15
      Lib/Utility/Utility.php
  2. 122 21
      Test/Case/Lib/Utility/UtilityTest.php

+ 52 - 15
Lib/Utility/Utility.php

@@ -90,6 +90,35 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
+	 * Will escape a string to be used as a regular expression pattern.
+	 *
+	 * - Escapes the following
+	 *   - \ ^ . $ | ( ) [ ] * + ? { } ,
+	 *
+	 * - Example
+	 *   - Utility::patternEscape('http://www.example.com/s?q=php.net+docs')
+	 *   - http:\/\/www\.example\.com\/s\?q=php\.net\+docs
+	 *
+	 * @see http://www.php.net/manual/en/function.preg-replace.php#92456
+	 * @author alammar at gmail dot com
+	 *
+	 * @param string $str the stuff you want escaped
+	 * @return string the escaped string
+	 */
+	public static function patternEscape($str) {
+		$patterns = array(
+			'/\//', '/\^/', '/\./', '/\$/', '/\|/',
+			'/\(/', '/\)/', '/\[/', '/\]/', '/\*/',
+			'/\+/', '/\?/', '/\{/', '/\}/', '/\,/'
+		);
+
+		$replace = array('\/', '\^', '\.', '\$', '\|', '\(', '\)',
+		'\[', '\]', '\*', '\+', '\?', '\{', '\}', '\,');
+
+		return preg_replace($patterns, $replace, $str);
+	}
+
+	/**
 	 * get the current ip address
 	 * get the current ip address
 	 * @param bool $safe
 	 * @param bool $safe
 	 * @return string $ip
 	 * @return string $ip
@@ -230,8 +259,11 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
-	 * encode strings with base64_encode and also
-	 * replace chars base64 uses that would mess up the url
+	 * Encode strings with base64_encode and also
+	 * replace chars base64 uses that would mess up the url.
+	 *
+	 * Do not use this for querystrings. Those will escape automatically.
+	 * This is only useful for named or passed params.
 	 *
 	 *
 	 * @param string $string Unsafe string
 	 * @param string $string Unsafe string
 	 * @return string Encoded string
 	 * @return string Encoded string
@@ -242,8 +274,11 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
-	 * decode strings with base64_encode and also
-	 * replace back chars base64 uses that would mess up the url
+	 * Decode strings with base64_encode and also
+	 * replace back chars base64 uses that would mess up the url.
+	 *
+	 * Do not use this for querystrings. Those will escape automatically.
+	 * This is only useful for named or passed params.
 	 *
 	 *
 	 * @param string $string Safe string
 	 * @param string $string Safe string
 	 * @return string Decoded string
 	 * @return string Decoded string
@@ -254,7 +289,7 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
-	 * returns true only if all values are true
+	 * Returns true only if all values are true.
 	 *
 	 *
 	 * @param array $array
 	 * @param array $array
 	 * @return bool $result
 	 * @return bool $result
@@ -274,11 +309,12 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
-	 * returns true if at least one value is true
+	 * Returns true if at least one value is true.
+	 * //TODO: maybe move to bootstrap?
 	 *
 	 *
 	 * @param array $array
 	 * @param array $array
 	 * @return bool $result
 	 * @return bool $result
-	 * maybe move to bootstrap?
+	 *
 	 * 2011-11-02 ms
 	 * 2011-11-02 ms
 	 */
 	 */
 	public static function logicalOr($array) {
 	public static function logicalOr($array) {
@@ -315,11 +351,11 @@ class Utility {
 
 
 	/**
 	/**
 	 * Convenience function for automatic casting in form methods etc
 	 * Convenience function for automatic casting in form methods etc
+	 * //TODO: maybe move to bootstrap?
 	 *
 	 *
 	 * @param mixed $value
 	 * @param mixed $value
 	 * @param string $type
 	 * @param string $type
 	 * @return safe value for DB query, or NULL if type was not a valid one
 	 * @return safe value for DB query, or NULL if type was not a valid one
-	 * maybe move to bootstrap?
 	 * 2008-12-12 ms
 	 * 2008-12-12 ms
 	 */
 	 */
 	public static function typeCast($value, $type) {
 	public static function typeCast($value, $type) {
@@ -349,7 +385,7 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
-	 * trim recursivly
+	 * Trim recursivly
 	 *
 	 *
 	 * 2009-07-07 ms
 	 * 2009-07-07 ms
 	 */
 	 */
@@ -369,7 +405,7 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
-	 * removes all except A-Z,a-z,0-9 and allowedChars (allowedChars array) recursivly
+	 * Removes all except A-Z,a-z,0-9 and allowedChars (allowedChars array) recursivly
 	 *
 	 *
 	 * 2009-07-07 ms
 	 * 2009-07-07 ms
 	 */
 	 */
@@ -379,7 +415,7 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
-	 * transfers/removes all < > from text (remove TRUE/FALSE)
+	 * Transfers/removes all < > from text (remove TRUE/FALSE)
 	 *
 	 *
 	 * 2009-07-07 ms
 	 * 2009-07-07 ms
 	 */
 	 */
@@ -399,7 +435,7 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
-	 * Flattens an array
+	 * Flattens an array.
 	 *
 	 *
 	 * @param array $array to flatten
 	 * @param array $array to flatten
 	 * @param boolean $perserveKeys
 	 * @param boolean $perserveKeys
@@ -426,9 +462,10 @@ class Utility {
 
 
 	/**
 	/**
 	 * Flatten an array and preserve the keys
 	 * Flatten an array and preserve the keys
+	 *
 	 * @return array
 	 * @return array
 	 */
 	 */
-	public static function _arrayFlatten($a, $f = array()) {
+	protected static function _arrayFlatten($a, $f = array()) {
 		if (!$a) {
 		if (!$a) {
 			return array();
 			return array();
 		}
 		}
@@ -444,10 +481,10 @@ class Utility {
 
 
 	/**
 	/**
 	 * Similar to array_shift but on the keys of the array
 	 * Similar to array_shift but on the keys of the array
+	 * like array_shift() only for keys and not values
 	 *
 	 *
 	 * @param array $keyValuePairs
 	 * @param array $keyValuePairs
 	 * @return string $key
 	 * @return string $key
-	 * like array_shift() only for keys and not values
 	 * 2011-01-22 ms
 	 * 2011-01-22 ms
 	 */
 	 */
 	public static function arrayShiftKeys(&$array) {
 	public static function arrayShiftKeys(&$array) {
@@ -492,7 +529,7 @@ class Utility {
 	}
 	}
 
 
 	/**
 	/**
-	 * returns microtime as float value
+	 * Returns microtime as float value
 	 * (to be subtracted right away)
 	 * (to be subtracted right away)
 	 *
 	 *
 	 * @return float
 	 * @return float

+ 122 - 21
Test/Case/Lib/Utility/UtilityTest.php

@@ -7,6 +7,11 @@ App::uses('MyCakeTestCase', 'Tools.TestSuite');
  */
  */
 class UtilityTest extends MyCakeTestCase {
 class UtilityTest extends MyCakeTestCase {
 
 
+	/**
+	 * UtilityTest::testInArray()
+	 *
+	 * @return void
+	 */
 	public function testInArray() {
 	public function testInArray() {
 		$res = Utility::inArray(2, array(1, 2, 3));
 		$res = Utility::inArray(2, array(1, 2, 3));
 		$this->assertTrue($res);
 		$this->assertTrue($res);
@@ -30,6 +35,11 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertFalse($res);
 		$this->assertFalse($res);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testPregMatch()
+	 *
+	 * @return void
+	 */
 	public function testPregMatch() {
 	public function testPregMatch() {
 		$string = '<abc>';
 		$string = '<abc>';
 		preg_match('/\<(\w+)\>/', $string, $matches);
 		preg_match('/\<(\w+)\>/', $string, $matches);
@@ -60,6 +70,21 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertSame($expected, $matches);
 		$this->assertSame($expected, $matches);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testPatternEscape()
+	 *
+	 * @return void
+	 */
+	public function testPatternEscape() {
+		$res = Utility::patternEscape('http://www.example.com/s?q=php.net+docs');
+		$this->assertSame('http:\/\/www\.example\.com\/s\?q=php\.net\+docs', $res);
+	}
+
+	/**
+	 * UtilityTest::testPregMatchAll()
+	 *
+	 * @return void
+	 */
 	public function testPregMatchAll() {
 	public function testPregMatchAll() {
 		$string = 'D-81245 München';
 		$string = 'D-81245 München';
 		preg_match_all('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches, PREG_SET_ORDER);
 		preg_match_all('/(*UTF8)([\w+])-([a-z0-9]+)\s+\b([\w\s]+)\b/iu', $string, $matches, PREG_SET_ORDER);
@@ -78,6 +103,11 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertSame($expected, $matches);
 		$this->assertSame($expected, $matches);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testStrSplit()
+	 *
+	 * @return void
+	 */
 	public function testStrSplit() {
 	public function testStrSplit() {
 		$res = str_split('some äöü string', 7);
 		$res = str_split('some äöü string', 7);
 		$expected = array('some äö', 'ü strin', 'g');
 		$expected = array('some äö', 'ü strin', 'g');
@@ -87,34 +117,84 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertSame($expected, $res);
 		$this->assertSame($expected, $res);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testUrlEncode()
+	 *
+	 * @return void
+	 */
+	public function testUrlEncode() {
+		$res = Utility::urlEncode('Some/cool=value+more-infos');
+		$this->assertSame('U29tZS9jb29sPXZhbHVlK21vcmUtaW5mb3M_', $res);
+	}
+
+	/**
+	 * UtilityTest::testUrlDecode()
+	 *
+	 * @return void
+	 */
+	public function testUrlDecode() {
+		$res = Utility::urlDecode('U29tZS9jb29sPXZhbHVlK21vcmUtaW5mb3M_');
+		$this->assertSame('Some/cool=value+more-infos', $res);
+	}
+
+	/**
+	 * UtilityTest::testTypeCast()
+	 *
+	 * @return void
+	 */
 	public function testTypeCast() {
 	public function testTypeCast() {
 		$res = Utility::typeCast(2, 'string');
 		$res = Utility::typeCast(2, 'string');
 		$this->assertNotSame(2, $res);
 		$this->assertNotSame(2, $res);
 		$this->assertSame('2', $res);
 		$this->assertSame('2', $res);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testGetClientIp()
+	 *
+	 * @return void
+	 */
 	public function testGetClientIp() {
 	public function testGetClientIp() {
 		$res = Utility::getClientIp();
 		$res = Utility::getClientIp();
 		$this->assertEquals(env('REMOTE_ADDR'), $res);
 		$this->assertEquals(env('REMOTE_ADDR'), $res);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testGetReferer()
+	 *
+	 * @return void
+	 */
 	public function testGetReferer() {
 	public function testGetReferer() {
 		$res = Utility::getReferer();
 		$res = Utility::getReferer();
 		//$this->assertTrue(env(''), $res);
 		//$this->assertTrue(env(''), $res);
 		$this->assertEquals(env('HTTP_REFERER'), $res);
 		$this->assertEquals(env('HTTP_REFERER'), $res);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testGetHeaderFromUrl()
+	 *
+	 * @return void
+	 */
 	public function testGetHeaderFromUrl() {
 	public function testGetHeaderFromUrl() {
 		$res = Utility::getHeaderFromUrl('http://www.spiegel.de');
 		$res = Utility::getHeaderFromUrl('http://www.spiegel.de');
 		$this->assertTrue(is_array($res) && count($res) > 10);
 		$this->assertTrue(is_array($res) && count($res) > 10);
 		$this->assertEquals('HTTP/1.0 200 OK', $res[0]);
 		$this->assertEquals('HTTP/1.0 200 OK', $res[0]);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testAutoPrefixUrl()
+	 *
+	 * @return void
+	 */
 	public function testAutoPrefixUrl() {
 	public function testAutoPrefixUrl() {
 		$res = Utility::autoPrefixUrl('www.spiegel.de');
 		$res = Utility::autoPrefixUrl('www.spiegel.de');
 		$this->assertEquals('http://www.spiegel.de', $res);
 		$this->assertEquals('http://www.spiegel.de', $res);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testCleanUrl()
+	 *
+	 * @return void
+	 */
 	public function testCleanUrl() {
 	public function testCleanUrl() {
 		$res = Utility::cleanUrl('www.spiegel.de');
 		$res = Utility::cleanUrl('www.spiegel.de');
 		$this->assertEquals('http://www.spiegel.de', $res);
 		$this->assertEquals('http://www.spiegel.de', $res);
@@ -133,16 +213,21 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertEquals('http://www.spiegel.de', $res);
 		$this->assertEquals('http://www.spiegel.de', $res);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testDeep()
+	 *
+	 * @return void
+	 */
 	public function testDeep() {
 	public function testDeep() {
 		$is = array(
 		$is = array(
 			'f some',
 			'f some',
 			'e 49r ' => 'rf r ',
 			'e 49r ' => 'rf r ',
-			'er' => array(array('ee'=>array('rr '=>' tt ')))
+			'er' => array(array('ee' => array('rr ' => ' tt ')))
 		);
 		);
 		$expected = array(
 		$expected = array(
 			'f some',
 			'f some',
 			'e 49r ' => 'rf r',
 			'e 49r ' => 'rf r',
-			'er' => array(array('ee'=>array('rr '=>'tt')))
+			'er' => array(array('ee' => array('rr ' => 'tt')))
 		);
 		);
 		//$this->assertSame($is, $expected);
 		//$this->assertSame($is, $expected);
 
 
@@ -158,12 +243,12 @@ class UtilityTest extends MyCakeTestCase {
 		$is = array(
 		$is = array(
 			'f some',
 			'f some',
 			'e 49r ' => 'rf r ',
 			'e 49r ' => 'rf r ',
-			'er' => array(array('ee'=>array('rr '=>' tt ')))
+			'er' => array(array('ee' => array('rr ' => ' tt ')))
 		);
 		);
 		$expected = array(
 		$expected = array(
 			'f some',
 			'f some',
 			'e 49r ' => 'rf r',
 			'e 49r ' => 'rf r',
-			'er' => array(array('ee'=>array('rr '=>'tt')))
+			'er' => array(array('ee' => array('rr ' => 'tt')))
 		);
 		);
 
 
 		$res = Utility::deep('trim', $is);
 		$res = Utility::deep('trim', $is);
@@ -171,10 +256,15 @@ class UtilityTest extends MyCakeTestCase {
 
 
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testArrayFlatten()
+	 *
+	 * @return void
+	 */
 	public function testArrayFlatten() {
 	public function testArrayFlatten() {
-		$array=array(
+		$array = array(
 			'a' => 1,
 			'a' => 1,
-			'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
+			'b' => array('c' => array('d' => array('f' => 'g', 'h' => true))),
 			'k' => 'm',
 			'k' => 'm',
 		);
 		);
 		$res = Utility::arrayFlatten($array);
 		$res = Utility::arrayFlatten($array);
@@ -188,8 +278,13 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertSame($expected, $res);
 		$this->assertSame($expected, $res);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testArrayShiftKeys()
+	 *
+	 * @return void
+	 */
 	public function testArrayShiftKeys() {
 	public function testArrayShiftKeys() {
-		$array=array(
+		$array = array(
 			'a' => 1,
 			'a' => 1,
 			'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
 			'b' => array('c'=>array('d'=>array('f'=>'g', 'h'=>true))),
 			'k' => 'm',
 			'k' => 'm',
@@ -205,6 +300,11 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertSame($expected, $array);
 		$this->assertSame($expected, $array);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testTime()
+	 *
+	 * @return void
+	 */
 	public function testTime() {
 	public function testTime() {
 		Utility::startClock();
 		Utility::startClock();
 		time_nanosleep(0, 200000000);
 		time_nanosleep(0, 200000000);
@@ -220,8 +320,13 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertTrue(round($res, 1) === 0.1);
 		$this->assertTrue(round($res, 1) === 0.1);
 	}
 	}
 
 
+	/**
+	 * UtilityTest::testLogicalAnd()
+	 *
+	 * @return void
+	 */
 	public function testLogicalAnd() {
 	public function testLogicalAnd() {
-		$array=array(
+		$array = array(
 			'a' => 1,
 			'a' => 1,
 			'b' => 1,
 			'b' => 1,
 			'c' => 0,
 			'c' => 0,
@@ -230,7 +335,7 @@ class UtilityTest extends MyCakeTestCase {
 		$is = Utility::logicalAnd($array);
 		$is = Utility::logicalAnd($array);
 		$this->assertSame($is, false);
 		$this->assertSame($is, false);
 
 
-		$array=array(
+		$array = array(
 			'a' => 1,
 			'a' => 1,
 			'b' => 1,
 			'b' => 1,
 			'c' => 1,
 			'c' => 1,
@@ -240,10 +345,13 @@ class UtilityTest extends MyCakeTestCase {
 		$this->assertSame($is, true);
 		$this->assertSame($is, true);
 	}
 	}
 
 
-
-
+	/**
+	 * UtilityTest::testLogicalOr()
+	 *
+	 * @return void
+	 */
 	public function testLogicalOr() {
 	public function testLogicalOr() {
-		$array=array(
+		$array = array(
 			'a' => 0,
 			'a' => 0,
 			'b' => 1,
 			'b' => 1,
 			'c' => 0,
 			'c' => 0,
@@ -252,7 +360,7 @@ class UtilityTest extends MyCakeTestCase {
 		$is = Utility::logicalOr($array);
 		$is = Utility::logicalOr($array);
 		$this->assertSame($is, true);
 		$this->assertSame($is, true);
 
 
-		$array=array(
+		$array = array(
 			'a' => 1,
 			'a' => 1,
 			'b' => 1,
 			'b' => 1,
 			'c' => 1,
 			'c' => 1,
@@ -261,8 +369,7 @@ class UtilityTest extends MyCakeTestCase {
 		$is = Utility::logicalOr($array);
 		$is = Utility::logicalOr($array);
 		$this->assertSame($is, true);
 		$this->assertSame($is, true);
 
 
-
-		$array=array(
+		$array = array(
 			'a' => 0,
 			'a' => 0,
 			'b' => 0,
 			'b' => 0,
 			'c' => 0,
 			'c' => 0,
@@ -270,12 +377,6 @@ class UtilityTest extends MyCakeTestCase {
 		);
 		);
 		$is = Utility::logicalOr($array);
 		$is = Utility::logicalOr($array);
 		$this->assertSame($is, false);
 		$this->assertSame($is, false);
-
-
-
 	}
 	}
 
 
-
-
-
 }
 }