StringHelperTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php declare(strict_types=1);
  2. /**
  3. * This file is part of toolkit/stdlib.
  4. *
  5. * @author https://github.com/inhere
  6. * @link https://github.com/php-toolkit/stdlib
  7. * @license MIT
  8. */
  9. namespace Toolkit\StdlibTest\Str;
  10. use PHPUnit\Framework\TestCase;
  11. use Toolkit\Stdlib\Str;
  12. /**
  13. * Class StringHelperTest
  14. *
  15. * @package Toolkit\StdlibTest\Str
  16. */
  17. class StringHelperTest extends TestCase
  18. {
  19. public function testBasicStrMethods(): void
  20. {
  21. $this->assertEquals('', Str::wrap('', '"'));
  22. $this->assertEquals('"a"', Str::wrap('a', '"'));
  23. $this->assertEquals(['"a"', '"b"'], Str::wrapList(['a', 'b'], '"'));
  24. $this->assertTrue(Str::isNull('null'));
  25. $this->assertFalse(Str::isNull('abc'));
  26. $this->assertTrue(Str::isAlphaNum('abc'));
  27. $this->assertTrue(Str::isAlphaNum('abc23'));
  28. $this->assertFalse(Str::isAlphaNum('--'));
  29. }
  30. public function testIsBool(): void
  31. {
  32. $this->assertTrue(Str::isBool('true'));
  33. $this->assertTrue(Str::isBool('false'));
  34. $this->assertFalse(Str::isBool('abc'));
  35. }
  36. public function testIsVarName(): void
  37. {
  38. $this->assertTrue(Str::isVarName('true'));
  39. $this->assertTrue(Str::isVarName('abc'));
  40. $this->assertTrue(Str::isVarName('some_name'));
  41. $this->assertFalse(Str::isVarName('some-name'));
  42. $this->assertFalse(Str::isVarName('some_name()'));
  43. }
  44. public function testToTyped(): void
  45. {
  46. $tests = [
  47. ['abc', 'abc'],
  48. ['true', true],
  49. ['23', 23],
  50. ['23.4', 23.4],
  51. ];
  52. foreach ($tests as [$in, $out]) {
  53. $this->assertEquals($out, Str::toTyped($in, true));
  54. }
  55. $this->assertEquals('true', Str::toTyped('true'));
  56. }
  57. public function testParamQuotes(): void
  58. {
  59. $tests = [
  60. ['', "''"],
  61. ['abc', "'abc'"],
  62. ["'abc'", "'abc'"],
  63. ['ab" c', "'ab\" c'"],
  64. ["ab' c", '"ab\' c"'],
  65. ];
  66. foreach ($tests as [$given, $want]) {
  67. self::assertSame($want, Str::paramQuotes($given));
  68. }
  69. }
  70. public function testShellQuote(): void
  71. {
  72. $tests = [
  73. ['', '""'],
  74. ['abc', 'abc'],
  75. ['ab c', '"ab c"'],
  76. ['ab"c', "'ab\"c'"],
  77. ];
  78. foreach ($tests as [$given, $want]) {
  79. $this->assertEquals($want, Str::shellQuote($given));
  80. }
  81. }
  82. public function testStrLen(): void
  83. {
  84. $tests = [
  85. ['abc', 3],
  86. [123, 3],
  87. [123.4, 5],
  88. ['23ab', 4],
  89. ];
  90. foreach ($tests as [$case, $want]) {
  91. $this->assertSame($want, Str::strlen($case));
  92. }
  93. }
  94. public function testHasPrefix(): void
  95. {
  96. self::assertTrue(Str::hasPrefix('abc', 'a'));
  97. self::assertFalse(Str::hasPrefix('abc', 'c'));
  98. self::assertTrue(Str::endWiths('abc', 'c'));
  99. self::assertTrue(Str::hasSuffix('abc', 'bc'));
  100. self::assertFalse(Str::hasSuffix('abc', 'b'));
  101. }
  102. public function testIEndWiths(): void
  103. {
  104. self::assertTrue(Str::endWithIC('abC', 'C'));
  105. self::assertTrue(Str::endWithIC('abC', 'c'));
  106. self::assertFalse(Str::endWithIC('abc', 'b'));
  107. self::assertTrue(Str::hasPrefixIC('abc', 'a'));
  108. self::assertTrue(Str::hasPrefixIC('abc', 'A'));
  109. self::assertFalse(Str::hasPrefixIC('abc', 'b'));
  110. }
  111. public function testHasPrefixIC(): void
  112. {
  113. $tests = [
  114. ['abc', 'a', true],
  115. ['23ab', 'a', false],
  116. ];
  117. foreach ($tests as [$case, $want, $yes]) {
  118. if ($yes) {
  119. $this->assertTrue(Str::hasPrefixIC($case, $want));
  120. $this->assertTrue(Str::startWithIC($case, $want));
  121. $this->assertTrue(Str::isStartWithIC($case, $want));
  122. } else {
  123. $this->assertFalse(Str::hasPrefixIC($case, $want));
  124. }
  125. }
  126. }
  127. public function testStrpos(): void
  128. {
  129. $tests = [
  130. ['abc', 'a'],
  131. ['23ab', 'a'],
  132. ];
  133. foreach ($tests as [$case, $want]) {
  134. $this->assertTrue(Str::has($case, $want));
  135. $this->assertTrue(Str::contains($case, $want));
  136. $this->assertTrue(Str::ihas($case, $want));
  137. $this->assertTrue(Str::icontains($case, $want));
  138. }
  139. self::assertTrue(Str::notContains('abc', 'd'));
  140. self::assertFalse(Str::notContains('abc', 'b'));
  141. }
  142. public function testStrCase_toCamel(): void
  143. {
  144. $tests = [
  145. ['voicePlayTimes', 'voicePlayTimes', 'VoicePlayTimes'],
  146. ['fieldName', 'fieldName', 'FieldName'],
  147. ['the_fieldName', 'theFieldName', 'TheFieldName'],
  148. ['the_field_name', 'theFieldName', 'TheFieldName'],
  149. ['the-field-name', 'theFieldName', 'TheFieldName'],
  150. ['the field name', 'theFieldName', 'TheFieldName'],
  151. ];
  152. foreach ($tests as [$case, $want, $want1]) {
  153. $this->assertEquals(Str::toCamel($case), $want);
  154. $this->assertEquals(Str::toCamel($case, true), $want1);
  155. }
  156. }
  157. public function testToNoEmptyArray(): void
  158. {
  159. $tests = [
  160. ['ab, cd', ',', ['ab', 'cd']],
  161. ['ab / cd', '/', ['ab', 'cd']],
  162. ['ab | cd', '|', ['ab', 'cd']],
  163. [' fieldName some desc', ' ', ['fieldName', 'some', 'desc']],
  164. [' ab 0 cd ', ' ', ['ab', '0', 'cd']],
  165. ];
  166. foreach ($tests as [$given, $sep, $want]) {
  167. $this->assertEquals($want, Str::toNoEmptyArray($given, $sep));
  168. }
  169. }
  170. public function testToArray_no_limit(): void
  171. {
  172. $tests = [
  173. ['34,56,678, 678, 89, ', ',', ['34', '56', '678', '678', '89']],
  174. [' fieldName some desc', ' ', ['fieldName', 'some', 'desc']],
  175. [' ab 0 cd ', ' ', ['ab', '0', 'cd']],
  176. ];
  177. foreach ($tests as [$given, $sep, $want]) {
  178. $this->assertEquals($want, Str::toNoEmptyArray($given, $sep));
  179. $this->assertEquals($want, Str::splitTrimFiltered($given, $sep));
  180. }
  181. }
  182. public function testToArray_limit(): void
  183. {
  184. $tests = [
  185. [
  186. ' fieldName some desc msg ',
  187. ' ',
  188. 2,
  189. ['fieldName', 'some desc msg']
  190. ],
  191. [
  192. ' ab 0 cd ',
  193. ' ',
  194. 2,
  195. ['ab', '0 cd']
  196. ],
  197. ];
  198. foreach ($tests as [$given, $sep, $limit, $want]) {
  199. $this->assertEquals($want, Str::splitTrimFiltered($given, $sep, $limit));
  200. $this->assertEquals($want, Str::toNoEmptyArray($given, $sep, $limit));
  201. }
  202. }
  203. /**
  204. * TIP: recommend use Str::toNoEmptyArray()
  205. */
  206. public function testDiff_splitTrimFiltered_toNoEmptyArray(): void
  207. {
  208. $str = ' fieldName,, some, desc, msg ';
  209. $this->assertEquals(
  210. ['fieldName', 'some', 'desc, msg'], // better
  211. Str::toNoEmptyArray($str, ',', 3)
  212. );
  213. $this->assertEquals(
  214. ['fieldName', 'some, desc, msg'], // only two elem
  215. Str::splitTrimFiltered($str, ',', 3)
  216. );
  217. $sep = ' ';
  218. $str = ' fieldName some desc msg ';
  219. $this->assertEquals(
  220. ['fieldName', 'some', 'desc msg'], // better
  221. Str::toNoEmptyArray($str, $sep, 3)
  222. );
  223. $this->assertEquals(
  224. ['fieldName', 'some desc msg'], // only two elem
  225. Str::splitTrimFiltered($str, $sep, 3)
  226. );
  227. }
  228. public function testSplitTypedList(): void
  229. {
  230. $tests = [
  231. ['34,56,678, 678, 89, ', [34, 56, 678, 678, 89]],
  232. ['a,,34, 3.4 ', ['a', 34, 3.4]],
  233. ['ab,,true ', ['ab', true]],
  234. ];
  235. foreach ($tests as [$given, $want]) {
  236. $this->assertEquals($want, Str::splitTypedList($given));
  237. $this->assertEquals($want, Str::toTypedArray($given));
  238. }
  239. }
  240. public function testRenderVars(): void
  241. {
  242. $vars = [
  243. 'name' => 'inhere',
  244. 'age' => 200,
  245. 'tags' => ['php', 'java'],
  246. 'top' => [
  247. 'key0' => 'val0',
  248. ]
  249. ];
  250. $text = Str::renderVars('hello {{ name }}, age: {{ age}}, tags: {{ tags.0 }}, key0: {{top.key0 }}', $vars);
  251. $this->assertEquals('hello inhere, age: 200, tags: php, key0: val0', $text);
  252. $text = Str::renderVars('hello ${ name }, age: ${ age}, tags: ${ tags.0 }, key0: ${top.key0 }', $vars, '${%s}');
  253. $this->assertEquals('hello inhere, age: 200, tags: php, key0: val0', $text);
  254. $text = Str::renderVars('tags: ${ tags }', $vars, '${%s}');
  255. $this->assertEquals('tags: [php, java]', $text);
  256. $vars = [
  257. 'company' => 'mycompany',
  258. 'namePath' => 'group.demo1',
  259. ];
  260. $text = Str::renderVars('java/com/{company}/{namePath}', $vars, '{%s}');
  261. $this->assertEquals('java/com/mycompany/group.demo1', $text);
  262. }
  263. public function testBeforeAfter(): void
  264. {
  265. $s = 'hello/world';
  266. $this->assertEquals('hello', Str::before($s, '/'));
  267. $this->assertEquals('some', Str::before($s, '+', 'some'));
  268. $this->assertEquals('world', Str::after($s, '/'));
  269. $this->assertEquals('some', Str::after($s, '+', 'some'));
  270. }
  271. }