浏览代码

testing Str class

euromark 12 年之前
父节点
当前提交
2bc5783e55
共有 3 个文件被更改,包括 218 次插入4 次删除
  1. 137 0
      Lib/Utility/Str.php
  2. 4 4
      Lib/Utility/Utility.php
  3. 77 0
      Test/Case/Lib/Utility/StrTest.php

+ 137 - 0
Lib/Utility/Str.php

@@ -0,0 +1,137 @@
+<?php
+/**
+ * Draft 0.2 for PHP argument order fix
+ * 2012-04-14 ms
+ */
+
+/**
+ * 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
+ *
+ * 2012-04-13 ms
+ */
+final class Str {
+
+	/**
+	 * Avoid constructor conflicts.
+	 *
+	 * @return void
+	 */
+	final public function __construct() {
+	}
+
+	/**
+	 * Find the first occurrence of a string.
+	 * Note: use iStr for CI
+	 *
+	 * @return mixed
+	 */
+	final public static function str($needle, $haystack, $beforeNeedle  = false) {
+		return strstr($haystack, $needle, $beforeNeedle);
+	}
+
+	/**
+	 * Case-insensitive strstr().
+	 *
+	 * @return mixed
+	 */
+	final public static function iStr($needle, $haystack, $beforeNeedle  = false) {
+		return stristr($haystack, $needle, $beforeNeedle);
+	}
+
+	/**
+	 * Find the first occurrence of a string - alias of strstr().
+	 *
+	 * @return mixed
+	 */
+	final public static function chr($needle, $haystack, $beforeNeedle  = false) {
+		return strchr($haystack, $needle, $beforeNeedle);
+	}
+
+	/**
+	 * Find the last occurrence of a character in a string.
+	 * Note: If needle contains more than one character, only the first is used.
+	 * 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.
+	 *
+	 * @return mixed
+	 */
+	final public static function rChr($needle, $haystack) {
+		return strrchr($haystack, $needle);
+	}
+
+	/**
+	 * Replace all occurrences of the search string with the replacement string.
+	 * Note: use iReplace for CI
+	 *
+	 * @return mixed
+	 */
+	final public static function replace($search, $replace, $subject, &$count = null) {
+		return str_replace($search, $replace, $subject, $count);
+	}
+
+	/**
+	 * Case-insensitive version of str_replace().
+	 *
+	 * @return mixed
+	 */
+	final public static function iReplace($search, $replace, $subject, &$count = null) {
+		return str_ireplace($search, $replace, $subject, $count);
+	}
+
+	/**
+	 * Replace text within a portion of a string.
+	 *
+	 * @return mixed
+	 */
+	final public static function substrReplace($string, $replacement, $start, $length = null) {
+		return substr_replace($string, $replacement, $start, $length);
+	}
+
+	/**
+	 * Count the number of substring occurrences.
+	 *
+	 * @return int
+	 */
+	final public static function count($needle, $haystack, $offset = 0, $length = null) {
+		return substr_count($needle, $haystack, $offset = 0, $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)
+	 *
+	 * @return mixed
+	 */
+	final public static function compare($mainStr, $str, $offset = 0, $length = null) {
+		return substr_compare($mainStr, $str, $offset = 0, $length);
+	}
+
+	/**
+	 * Binary safe comparison of two strings from an offset, up to length characters.
+	 *
+	 * @return mixed
+	 */
+	final public static function iCompare($mainStr, $str, $offset = 0, $length = null) {
+		return substr_compare($needle, $haystack, $offset = 0, $length, true);
+	}
+
+	/**
+	 * Find the position of the first occurrence of a substring in a string.
+	 *
+	 * @return mixed
+	 */
+	final public static function pos($needle, $haystack, $offset = 0) {
+		return strpos($haystack, $needle, $offset);
+	}
+
+	/**
+	 * Find the position of the last occurrence of a substring in a string.
+	 *
+	 * @return mixed
+	 */
+	final public static function rPos($needle, $haystack, $offset = 0) {
+		return strrpos($haystack, $needle, $offset);
+	}
+
+}

+ 4 - 4
Lib/Utility/Utility.php

@@ -290,10 +290,10 @@ class Utility {
 
 	/**
 	 * Returns true only if all values are true.
+	 * //TODO: maybe move to bootstrap?
 	 *
 	 * @param array $array
 	 * @return bool $result
-	 * maybe move to bootstrap?
 	 * 2011-11-02 ms
 	 */
 	public static function logicalAnd($array) {
@@ -327,8 +327,8 @@ class Utility {
 	}
 
 	/**
-	 * On non-transaction db connections it will return a deep array of bools instead of bool
-	 * So we need to call this method inside the modified saveAll() method to return the expected single bool there, too
+	 * On non-transaction db connections it will return a deep array of bools instead of bool.
+	 * So we need to call this method inside the modified saveAll() method to return the expected single bool there, too.
 	 *
 	 * @param array
 	 * @return bool
@@ -350,7 +350,7 @@ 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

+ 77 - 0
Test/Case/Lib/Utility/StrTest.php

@@ -0,0 +1,77 @@
+<?php
+/**
+ * Draft 0.2 for PHP argument order fix
+ * 2012-04-14 ms
+ */
+
+App::uses('Str', 'Tools.Utility');
+App::uses('MyCakeTestCase', 'Tools.TestSuite');
+
+/**
+ * @see https://bugs.php.net/bug.php?id=44794
+ * 2012-04-14 ms
+ */
+class StrTest extends MyCakeTestCase {
+
+	/**
+	 * fixed
+	 * - documented return type (mixed)
+	 * - argument order
+	 * - missing underscore
+	 */
+	public function testStrStr() {
+		$res = Str::str('some', 'more some text');
+		$expected = 'some text';
+		$this->assertSame($expected, $res);
+
+		$res = Str::str('some', 'more som text');
+		$expected = false;
+		$this->assertSame($expected, $res);
+	}
+
+	/**
+	 * no changes
+	 */
+	public function testStrReplace() {
+		$res = Str::replace('some', 'more', 'in some text');
+		$expected = 'in more text';
+		$this->assertSame($expected, $res);
+
+		$count = 0;
+		$res = Str::replace('some', 'more', 'in some text', $count);
+		$this->assertSame($expected, $res);
+		$this->assertSame(1, $count);
+	}
+
+	/**
+	 * fixed
+	 * - documented return type (mixed)
+	 * - argument order
+	 * - missing underscore
+	 *
+	 * very strange method
+	 */
+	public function testStrRchr() {
+		$res = Str::rChr('some', 'more some text');
+		$expected = 'some text';
+		$this->assertSame($expected, $res);
+
+		# WTF?
+		$res = Str::rChr('some', 'more som text');
+		$expected = 'som text';
+		$this->assertSame($expected, $res);
+
+		$res = Str::rChr('xome', 'more som text');
+		$expected = 'xt';
+		$this->assertSame($expected, $res);
+
+		$res = Str::rChr('abc', 'more som text');
+		$expected = false;
+		$this->assertSame($expected, $res);
+
+		$res = Str::rChr(120, 'more som text');
+		$expected = 'xt';
+		$this->assertSame($expected, $res);
+	}
+
+}