StrTest.php 6.2 KB

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