StrTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * Draft 0.2 for PHP argument order fix
  4. */
  5. App::uses('Str', 'Tools.Utility');
  6. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  7. /**
  8. * @see https://bugs.php.net/bug.php?id=44794
  9. */
  10. class StrTest extends MyCakeTestCase {
  11. public function testDebugInfo() {
  12. $this->skipIf(php_sapi_name() === 'cli', 'Only for webtest runner');
  13. $functionLists = array(
  14. 'Array' => array(
  15. 'array_change_key_case',
  16. 'array_chunk',
  17. 'array_combine',
  18. 'array_count_values',
  19. 'array_diff_assoc',
  20. 'array_diff_key',
  21. 'array_diff_uassoc',
  22. 'array_diff_ukey',
  23. 'array_diff',
  24. 'array_fill_keys',
  25. 'array_fill',
  26. 'array_filter',
  27. 'array_flip',
  28. 'array_intersect_assoc',
  29. 'array_intersect_key',
  30. 'array_intersect_uassoc',
  31. 'array_intersect_ukey',
  32. 'array_intersect',
  33. 'array_key_exists',
  34. 'array_keys',
  35. 'array_map',
  36. 'array_merge_recursive',
  37. 'array_merge',
  38. 'array_multisort',
  39. 'array_pad',
  40. 'array_pop',
  41. 'array_product',
  42. 'array_push',
  43. 'array_rand',
  44. 'array_reduce',
  45. 'array_replace_recursive',
  46. 'array_replace',
  47. 'array_reverse',
  48. 'array_search',
  49. 'array_shift',
  50. 'array_slice',
  51. 'array_splice',
  52. 'array_sum',
  53. 'array_udiff_assoc',
  54. 'array_udiff_uassoc',
  55. 'array_udiff',
  56. 'array_uintersect_assoc',
  57. 'array_uintersect_uassoc',
  58. 'array_uintersect',
  59. 'array_unique',
  60. 'array_unshift',
  61. 'array_values',
  62. 'array_walk_recursive',
  63. 'array_walk',
  64. 'array',
  65. 'arsort',
  66. 'asort',
  67. 'compact',
  68. 'count',
  69. 'current',
  70. 'each',
  71. 'end',
  72. 'extract',
  73. 'in_array',
  74. 'key',
  75. 'krsort',
  76. 'ksort',
  77. 'list',
  78. 'natcasesort',
  79. 'natsort',
  80. 'next',
  81. 'pos',
  82. 'prev',
  83. 'range',
  84. 'reset',
  85. 'rsort',
  86. 'shuffle',
  87. 'sizeof',
  88. 'sort',
  89. 'uasort',
  90. 'uksort',
  91. 'usort'),
  92. 'String' => array(
  93. 'addcslashes',
  94. 'addslashes',
  95. 'bin2hex',
  96. 'chop',
  97. 'chr',
  98. 'chunk_split',
  99. 'convert_cyr_string',
  100. 'convert_uudecode',
  101. 'convert_uuencode',
  102. 'count_chars',
  103. 'crc32',
  104. 'crypt',
  105. 'echo',
  106. 'explode',
  107. 'fprintf',
  108. 'get_html_translation_table',
  109. 'hebrev',
  110. 'hebrevc',
  111. 'hex2bin',
  112. 'html_entity_decode',
  113. 'htmlentities',
  114. 'htmlspecialchars_decode',
  115. 'htmlspecialchars',
  116. 'implode',
  117. 'join',
  118. 'lcfirst',
  119. 'levenshtein',
  120. 'localeconv',
  121. 'ltrim',
  122. 'md5_file',
  123. 'md5',
  124. 'metaphone',
  125. 'money_format',
  126. 'nl_langinfo',
  127. 'nl2br',
  128. 'number_format',
  129. 'ord',
  130. 'parse_str',
  131. 'print',
  132. 'printf',
  133. 'quoted_printable_decode',
  134. 'quoted_printable_encode',
  135. 'quotemeta',
  136. 'rtrim',
  137. 'setlocale',
  138. 'sha1_file',
  139. 'sha1',
  140. 'similar_text',
  141. 'soundex',
  142. 'sprintf',
  143. 'sscanf',
  144. 'str_getcsv',
  145. 'str_ireplace',
  146. 'str_pad',
  147. 'str_repeat',
  148. 'str_replace',
  149. 'str_rot13',
  150. 'str_shuffle',
  151. 'str_split',
  152. 'str_word_count',
  153. 'strcasecmp',
  154. 'strchr',
  155. 'strcmp',
  156. 'strcoll',
  157. 'strcspn',
  158. 'strip_tags',
  159. 'stripcslashes',
  160. 'stripos',
  161. 'stripslashes',
  162. 'stristr',
  163. 'strlen',
  164. 'strnatcasecmp',
  165. 'strnatcmp',
  166. 'strncasecmp',
  167. 'strncmp',
  168. 'strpbrk',
  169. 'strpos',
  170. 'strrchr',
  171. 'strrev',
  172. 'strripos',
  173. 'strrpos',
  174. 'strspn',
  175. 'strstr',
  176. 'strtok',
  177. 'strtolower',
  178. 'strtoupper',
  179. 'strtr',
  180. 'substr_compare',
  181. 'substr_count',
  182. 'substr_replace',
  183. 'substr',
  184. 'trim',
  185. 'ucfirst',
  186. 'ucwords',
  187. 'vfprintf',
  188. 'vprintf',
  189. 'vsprintf',
  190. 'wordwrap'),
  191. );
  192. $res = '';
  193. foreach ($functionLists as $type => $functions) {
  194. $res .= "$type functions:\n";
  195. foreach ($functions as $function) {
  196. try {
  197. $needle = new ReflectionParameter($function, "needle");
  198. $haystack = new ReflectionParameter($function, "haystack");
  199. $order = ($needle->getPosition() < $haystack->getPosition() ? '$needle, $haystack' : '$haystack, $needle');
  200. $res .= sprintf("%20s %s\n", $function, $order);
  201. }
  202. catch (ReflectionException $e) {
  203. continue;
  204. }
  205. }
  206. $res .= "\n";
  207. }
  208. $this->debug($res);
  209. }
  210. /**
  211. * Fixed
  212. * - documented return type (mixed)
  213. * - argument order
  214. * - missing underscore
  215. */
  216. public function testStrStr() {
  217. $res = Str::str('some', 'more some text');
  218. $expected = 'some text';
  219. $this->assertSame($expected, $res);
  220. $res = Str::str('some', 'more som text');
  221. $expected = false;
  222. $this->assertSame($expected, $res);
  223. }
  224. /**
  225. * No changes
  226. *
  227. * @return void
  228. */
  229. public function testStrReplace() {
  230. $res = Str::replace('some', 'more', 'in some text');
  231. $expected = 'in more text';
  232. $this->assertSame($expected, $res);
  233. $count = 0;
  234. $res = Str::replace('some', 'more', 'in some text', $count);
  235. $this->assertSame($expected, $res);
  236. $this->assertSame(1, $count);
  237. }
  238. /**
  239. * No changes
  240. *
  241. * @return void
  242. */
  243. public function testSubstrReplace() {
  244. $res = Str::substrReplace('some', 'more', 0, 0);
  245. $expected = 'moresome';
  246. $this->assertSame($expected, $res);
  247. $res = Str::substrReplace('some', 'more', 1, 0);
  248. $expected = 'smoreome';
  249. $this->assertSame($expected, $res);
  250. }
  251. /**
  252. * No changes
  253. *
  254. * @return void
  255. */
  256. public function testCount() {
  257. $res = Str::count('more', 'some more and more text');
  258. $this->assertSame(2, $res);
  259. $res = Str::count('more', 'some text');
  260. $this->assertSame(0, $res);
  261. $res = Str::count('more', 'some more and more text and even more text', 10, 20);
  262. $this->assertSame(1, $res);
  263. }
  264. /**
  265. * Very strange method
  266. *
  267. * fixed
  268. * - documented return type (mixed)
  269. * - argument order
  270. * - missing underscore
  271. * - naming scheme
  272. *
  273. * @return void
  274. */
  275. public function testStrLastChr() {
  276. $res = Str::lastChr('some', 'more some text');
  277. $expected = 'some text';
  278. $this->assertSame($expected, $res);
  279. // WTF?
  280. $res = Str::lastChr('some', 'more som text');
  281. $expected = 'som text';
  282. $this->assertSame($expected, $res);
  283. $res = Str::lastChr('xome', 'more som text');
  284. $expected = 'xt';
  285. $this->assertSame($expected, $res);
  286. $res = Str::lastChr('abc', 'more som text');
  287. $expected = false;
  288. $this->assertSame($expected, $res);
  289. $res = Str::lastChr(120, 'more som text');
  290. $expected = 'xt';
  291. $this->assertSame($expected, $res);
  292. }
  293. }