| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- <?php
- /**
- * Draft 0.2 for PHP argument order fix
- */
- App::uses('Str', 'Tools.Utility');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- /**
- * @see https://bugs.php.net/bug.php?id=44794
- */
- class StrTest extends MyCakeTestCase {
- public function testDebugInfo() {
- $this->skipIf(php_sapi_name() === 'cli', 'Only for webtest runner');
- $functionLists = [
- 'Array' => [
- 'array_change_key_case',
- 'array_chunk',
- 'array_combine',
- 'array_count_values',
- 'array_diff_assoc',
- 'array_diff_key',
- 'array_diff_uassoc',
- 'array_diff_ukey',
- 'array_diff',
- 'array_fill_keys',
- 'array_fill',
- 'array_filter',
- 'array_flip',
- 'array_intersect_assoc',
- 'array_intersect_key',
- 'array_intersect_uassoc',
- 'array_intersect_ukey',
- 'array_intersect',
- 'array_key_exists',
- 'array_keys',
- 'array_map',
- 'array_merge_recursive',
- 'array_merge',
- 'array_multisort',
- 'array_pad',
- 'array_pop',
- 'array_product',
- 'array_push',
- 'array_rand',
- 'array_reduce',
- 'array_replace_recursive',
- 'array_replace',
- 'array_reverse',
- 'array_search',
- 'array_shift',
- 'array_slice',
- 'array_splice',
- 'array_sum',
- 'array_udiff_assoc',
- 'array_udiff_uassoc',
- 'array_udiff',
- 'array_uintersect_assoc',
- 'array_uintersect_uassoc',
- 'array_uintersect',
- 'array_unique',
- 'array_unshift',
- 'array_values',
- 'array_walk_recursive',
- 'array_walk',
- 'array',
- 'arsort',
- 'asort',
- 'compact',
- 'count',
- 'current',
- 'each',
- 'end',
- 'extract',
- 'in_array',
- 'key',
- 'krsort',
- 'ksort',
- 'list',
- 'natcasesort',
- 'natsort',
- 'next',
- 'pos',
- 'prev',
- 'range',
- 'reset',
- 'rsort',
- 'shuffle',
- 'sizeof',
- 'sort',
- 'uasort',
- 'uksort',
- 'usort'],
- 'String' => [
- 'addcslashes',
- 'addslashes',
- 'bin2hex',
- 'chop',
- 'chr',
- 'chunk_split',
- 'convert_cyr_string',
- 'convert_uudecode',
- 'convert_uuencode',
- 'count_chars',
- 'crc32',
- 'crypt',
- 'echo',
- 'explode',
- 'fprintf',
- 'get_html_translation_table',
- 'hebrev',
- 'hebrevc',
- 'hex2bin',
- 'html_entity_decode',
- 'htmlentities',
- 'htmlspecialchars_decode',
- 'htmlspecialchars',
- 'implode',
- 'join',
- 'lcfirst',
- 'levenshtein',
- 'localeconv',
- 'ltrim',
- 'md5_file',
- 'md5',
- 'metaphone',
- 'money_format',
- 'nl_langinfo',
- 'nl2br',
- 'number_format',
- 'ord',
- 'parse_str',
- 'print',
- 'printf',
- 'quoted_printable_decode',
- 'quoted_printable_encode',
- 'quotemeta',
- 'rtrim',
- 'setlocale',
- 'sha1_file',
- 'sha1',
- 'similar_text',
- 'soundex',
- 'sprintf',
- 'sscanf',
- 'str_getcsv',
- 'str_ireplace',
- 'str_pad',
- 'str_repeat',
- 'str_replace',
- 'str_rot13',
- 'str_shuffle',
- 'str_split',
- 'str_word_count',
- 'strcasecmp',
- 'strchr',
- 'strcmp',
- 'strcoll',
- 'strcspn',
- 'strip_tags',
- 'stripcslashes',
- 'stripos',
- 'stripslashes',
- 'stristr',
- 'strlen',
- 'strnatcasecmp',
- 'strnatcmp',
- 'strncasecmp',
- 'strncmp',
- 'strpbrk',
- 'strpos',
- 'strrchr',
- 'strrev',
- 'strripos',
- 'strrpos',
- 'strspn',
- 'strstr',
- 'strtok',
- 'strtolower',
- 'strtoupper',
- 'strtr',
- 'substr_compare',
- 'substr_count',
- 'substr_replace',
- 'substr',
- 'trim',
- 'ucfirst',
- 'ucwords',
- 'vfprintf',
- 'vprintf',
- 'vsprintf',
- 'wordwrap'],
- ];
- $res = '';
- foreach ($functionLists as $type => $functions) {
- $res .= "$type functions:\n";
- foreach ($functions as $function) {
- try {
- $needle = new ReflectionParameter($function, "needle");
- $haystack = new ReflectionParameter($function, "haystack");
- $order = ($needle->getPosition() < $haystack->getPosition() ? '$needle, $haystack' : '$haystack, $needle');
- $res .= sprintf("%20s %s\n", $function, $order);
- }
- catch (ReflectionException $e) {
- continue;
- }
- }
- $res .= "\n";
- }
- $this->debug($res);
- }
- /**
- * 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
- *
- * @return void
- */
- 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);
- }
- /**
- * 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
- *
- * @return void
- */
- public function testStrLastChr() {
- $res = Str::lastChr('some', 'more some text');
- $expected = 'some text';
- $this->assertSame($expected, $res);
- // WTF?
- $res = Str::lastChr('some', 'more som text');
- $expected = 'som text';
- $this->assertSame($expected, $res);
- $res = Str::lastChr('xome', 'more som text');
- $expected = 'xt';
- $this->assertSame($expected, $res);
- $res = Str::lastChr('abc', 'more som text');
- $expected = false;
- $this->assertSame($expected, $res);
- $res = Str::lastChr(120, 'more som text');
- $expected = 'xt';
- $this->assertSame($expected, $res);
- }
- }
|