Browse Source

more tests

euromark 12 years ago
parent
commit
e3de63133f
2 changed files with 130 additions and 16 deletions
  1. 86 8
      Lib/Utility/Str.php
  2. 44 8
      Test/Case/Lib/Utility/StrTest.php

+ 86 - 8
Lib/Utility/Str.php

@@ -8,6 +8,18 @@
  * Fix/Unify order, unify _ (strstr to str_str etc).
  * @inspired by http://www.skyrocket.be/2009/05/30/php-function-naming-and-argument-order/comment-page-1
  *
+ * The following functions use "needle hackstack":
+ *  - array_search
+ *  - in_array
+ *
+ * The following do it in reverse order and will be fixed with this class:
+ * - strchr, stristr, strrchr, strstr
+ * - strpos, strrpos, stripos, strripos, substr_count
+ *
+ * Also corrected is the naming of "rchr vs rrchr" by using "last" instead of "r":
+ * - chr and lastChr
+ * - pos, lastPos and iLastPos
+ *
  * 2012-04-13 ms
  */
 final class Str {
@@ -24,27 +36,36 @@ final class Str {
 	 * Find the first occurrence of a string.
 	 * Note: use iStr for CI
 	 *
+	 * @param mixed $needle
+	 * @param string $haystack
+	 * @param bool $beforeNeedle (defaults to false)
 	 * @return mixed
 	 */
-	final public static function str($needle, $haystack, $beforeNeedle  = false) {
+	final public static function str($needle, $haystack, $beforeNeedle = false) {
 		return strstr($haystack, $needle, $beforeNeedle);
 	}
 
 	/**
 	 * Case-insensitive strstr().
 	 *
+	 * @param mixed $needle
+	 * @param string $haystack
+	 * @param bool $beforeNeedle (defaults to false)
 	 * @return mixed
 	 */
-	final public static function iStr($needle, $haystack, $beforeNeedle  = false) {
+	final public static function iStr($needle, $haystack, $beforeNeedle = false) {
 		return stristr($haystack, $needle, $beforeNeedle);
 	}
 
 	/**
 	 * Find the first occurrence of a string - alias of strstr().
 	 *
+	 * @param mixed $needle
+	 * @param string $haystack
+	 * @param bool $beforeNeedle (defaults to false)
 	 * @return mixed
 	 */
-	final public static function chr($needle, $haystack, $beforeNeedle  = false) {
+	final public static function chr($needle, $haystack, $beforeNeedle = false) {
 		return strchr($haystack, $needle, $beforeNeedle);
 	}
 
@@ -54,9 +75,11 @@ final class Str {
 	 * This behavior is different from that of strstr(). This behavior is different from that of strstr().
 	 * If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
 	 *
+	 * @param mixed $needle
+	 * @param string $haystack
 	 * @return mixed
 	 */
-	final public static function rChr($needle, $haystack) {
+	final public static function lastChr($needle, $haystack) {
 		return strrchr($haystack, $needle);
 	}
 
@@ -64,6 +87,10 @@ final class Str {
 	 * Replace all occurrences of the search string with the replacement string.
 	 * Note: use iReplace for CI
 	 *
+	 * @param mixed $search
+	 * @param mixed $replace
+	 * @param mixed $subject
+	 * @param int $count Reference to store count in
 	 * @return mixed
 	 */
 	final public static function replace($search, $replace, $subject, &$count = null) {
@@ -73,6 +100,10 @@ final class Str {
 	/**
 	 * Case-insensitive version of str_replace().
 	 *
+	 * @param mixed $search
+	 * @param mixed $replace
+	 * @param mixed $subject
+	 * @param int $count Reference to store count in
 	 * @return mixed
 	 */
 	final public static function iReplace($search, $replace, $subject, &$count = null) {
@@ -82,6 +113,8 @@ final class Str {
 	/**
 	 * Replace text within a portion of a string.
 	 *
+	 * @param mixed $string
+	 * @param string $replacement
 	 * @return mixed
 	 */
 	final public static function substrReplace($string, $replacement, $start, $length = null) {
@@ -91,34 +124,52 @@ final class Str {
 	/**
 	 * Count the number of substring occurrences.
 	 *
+	 * @param string $needle
+	 * @param string $haystack
+	 * @param int $offset
+	 * @param int $length
 	 * @return int
 	 */
 	final public static function count($needle, $haystack, $offset = 0, $length = null) {
-		return substr_count($needle, $haystack, $offset = 0, $length);
+		if ($length === null) {
+			return substr_count($haystack, $needle, $offset);
+		}
+		return substr_count($haystack, $needle, $offset, $length);
 	}
 
 	/**
 	 * Binary safe comparison of two strings from an offset, up to length characters.
 	 * Note: use iCompare for CI (for the sake of consistency and less arguments - already enough)
 	 *
+	 * @param string $mainStr
+	 * @param string $str
+	 * @param int $offset
+	 * @param int $length
 	 * @return mixed
 	 */
 	final public static function compare($mainStr, $str, $offset = 0, $length = null) {
-		return substr_compare($mainStr, $str, $offset = 0, $length);
+		return substr_compare($mainStr, $str, $offset, $length);
 	}
 
 	/**
 	 * Binary safe comparison of two strings from an offset, up to length characters.
 	 *
+	 * @param string $mainStr
+	 * @param string $str
+	 * @param int $offset
+	 * @param int $length
 	 * @return mixed
 	 */
 	final public static function iCompare($mainStr, $str, $offset = 0, $length = null) {
-		return substr_compare($needle, $haystack, $offset = 0, $length, true);
+		return substr_compare($mainStr, $str, $offset, $length, true);
 	}
 
 	/**
 	 * Find the position of the first occurrence of a substring in a string.
 	 *
+	 * @param string $needle
+	 * @param string $haystack
+	 * @param int $offset
 	 * @return mixed
 	 */
 	final public static function pos($needle, $haystack, $offset = 0) {
@@ -126,12 +177,39 @@ final class Str {
 	}
 
 	/**
+	 * Case-insensitive version of stripos().
+	 *
+	 * @param string $needle
+	 * @param string $haystack
+	 * @param int $offset
+	 * @return mixed
+	 */
+	final public static function iPos($needle, $haystack, $offset = 0) {
+		return stripos($haystack, $needle, $offset);
+	}
+
+	/**
 	 * Find the position of the last occurrence of a substring in a string.
 	 *
+	 * @param string $needle
+	 * @param string $haystack
+	 * @param int $offset
 	 * @return mixed
 	 */
-	final public static function rPos($needle, $haystack, $offset = 0) {
+	final public static function lastPos($needle, $haystack, $offset = 0) {
 		return strrpos($haystack, $needle, $offset);
 	}
 
+	/**
+	 * Case-insensitive version of strrpos().
+	 *
+	 * @param string $needle
+	 * @param string $haystack
+	 * @param int $offset
+	 * @return mixed
+	 */
+	final public static function iLastPos($needle, $haystack, $offset = 0) {
+		return strripos($haystack, $needle, $offset);
+	}
+
 }

+ 44 - 8
Test/Case/Lib/Utility/StrTest.php

@@ -30,7 +30,9 @@ class StrTest extends MyCakeTestCase {
 	}
 
 	/**
-	 * no changes
+	 * No changes
+	 *
+	 * @return void
 	 */
 	public function testStrReplace() {
 		$res = Str::replace('some', 'more', 'in some text');
@@ -44,32 +46,66 @@ class StrTest extends MyCakeTestCase {
 	}
 
 	/**
+	 * No changes
+	 *
+	 * @return void
+	 */
+	public function testSubstrReplace() {
+		$res = Str::substrReplace('some', 'more', 0, 0);
+		$expected = 'moresome';
+		$this->assertSame($expected, $res);
+
+		$res = Str::substrReplace('some', 'more', 1, 0);
+		$expected = 'smoreome';
+		$this->assertSame($expected, $res);
+	}
+
+	/**
+	 * No changes
+	 *
+	 * @return void
+	 */
+	public function testCount() {
+		$res = Str::count('more', 'some more and more text');
+		$this->assertSame(2, $res);
+
+		$res = Str::count('more', 'some text');
+		$this->assertSame(0, $res);
+
+		$res = Str::count('more', 'some more and more text and even more text', 10, 20);
+		$this->assertSame(1, $res);
+	}
+
+	/**
+	 * Very strange method
+	 *
 	 * fixed
 	 * - documented return type (mixed)
 	 * - argument order
 	 * - missing underscore
+	 * - naming scheme
 	 *
-	 * very strange method
+	 * @return void
 	 */
-	public function testStrRchr() {
-		$res = Str::rChr('some', 'more some text');
+	public function testStrLastChr() {
+		$res = Str::lastChr('some', 'more some text');
 		$expected = 'some text';
 		$this->assertSame($expected, $res);
 
 		# WTF?
-		$res = Str::rChr('some', 'more som text');
+		$res = Str::lastChr('some', 'more som text');
 		$expected = 'som text';
 		$this->assertSame($expected, $res);
 
-		$res = Str::rChr('xome', 'more som text');
+		$res = Str::lastChr('xome', 'more som text');
 		$expected = 'xt';
 		$this->assertSame($expected, $res);
 
-		$res = Str::rChr('abc', 'more som text');
+		$res = Str::lastChr('abc', 'more som text');
 		$expected = false;
 		$this->assertSame($expected, $res);
 
-		$res = Str::rChr(120, 'more som text');
+		$res = Str::lastChr(120, 'more som text');
 		$expected = 'xt';
 		$this->assertSame($expected, $res);
 	}