SupportStrTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. <?php
  2. namespace Illuminate\Tests\Support;
  3. use Illuminate\Support\Str;
  4. use PHPUnit\Framework\TestCase;
  5. use Ramsey\Uuid\UuidInterface;
  6. use ReflectionClass;
  7. class SupportStrTest extends TestCase
  8. {
  9. public function testStringCanBeLimitedByWords()
  10. {
  11. $this->assertSame('Taylor...', Str::words('Taylor Otwell', 1));
  12. $this->assertSame('Taylor___', Str::words('Taylor Otwell', 1, '___'));
  13. $this->assertSame('Taylor Otwell', Str::words('Taylor Otwell', 3));
  14. }
  15. public function testStringCanBeLimitedByWordsNonAscii()
  16. {
  17. $this->assertSame('这是...', Str::words('这是 段中文', 1));
  18. $this->assertSame('这是___', Str::words('这是 段中文', 1, '___'));
  19. $this->assertSame('这是-段中文', Str::words('这是-段中文', 3, '___'));
  20. $this->assertSame('这是___', Str::words('这是 段中文', 1, '___'));
  21. }
  22. public function testStringTrimmedOnlyWhereNecessary()
  23. {
  24. $this->assertSame(' Taylor Otwell ', Str::words(' Taylor Otwell ', 3));
  25. $this->assertSame(' Taylor...', Str::words(' Taylor Otwell ', 1));
  26. }
  27. public function testStringTitle()
  28. {
  29. $this->assertSame('Jefferson Costella', Str::title('jefferson costella'));
  30. $this->assertSame('Jefferson Costella', Str::title('jefFErson coSTella'));
  31. }
  32. public function testStringHeadline()
  33. {
  34. $this->assertSame('Jefferson Costella', Str::headline('jefferson costella'));
  35. $this->assertSame('Jefferson Costella', Str::headline('jefFErson coSTella'));
  36. $this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses-_Laravel'));
  37. $this->assertSame('Jefferson Costella Uses Laravel', Str::headline('jefferson_costella uses__Laravel'));
  38. $this->assertSame('Laravel P H P Framework', Str::headline('laravel_p_h_p_framework'));
  39. $this->assertSame('Laravel P H P Framework', Str::headline('laravel _p _h _p _framework'));
  40. $this->assertSame('Laravel Php Framework', Str::headline('laravel_php_framework'));
  41. $this->assertSame('Laravel Ph P Framework', Str::headline('laravel-phP-framework'));
  42. $this->assertSame('Laravel Php Framework', Str::headline('laravel -_- php -_- framework '));
  43. $this->assertSame('Foo Bar', Str::headline('fooBar'));
  44. $this->assertSame('Foo Bar', Str::headline('foo_bar'));
  45. $this->assertSame('Foo Bar Baz', Str::headline('foo-barBaz'));
  46. $this->assertSame('Foo Bar Baz', Str::headline('foo-bar_baz'));
  47. $this->assertSame('Öffentliche Überraschungen', Str::headline('öffentliche-überraschungen'));
  48. $this->assertSame('Öffentliche Überraschungen', Str::headline('-_öffentliche_überraschungen_-'));
  49. $this->assertSame('Öffentliche Überraschungen', Str::headline('-öffentliche überraschungen'));
  50. $this->assertSame('Sind Öde Und So', Str::headline('sindÖdeUndSo'));
  51. $this->assertSame('Orwell 1984', Str::headline('orwell 1984'));
  52. $this->assertSame('Orwell 1984', Str::headline('orwell 1984'));
  53. $this->assertSame('Orwell 1984', Str::headline('-orwell-1984 -'));
  54. $this->assertSame('Orwell 1984', Str::headline(' orwell_- 1984 '));
  55. }
  56. public function testStringWithoutWordsDoesntProduceError()
  57. {
  58. $nbsp = chr(0xC2).chr(0xA0);
  59. $this->assertSame(' ', Str::words(' '));
  60. $this->assertEquals($nbsp, Str::words($nbsp));
  61. }
  62. public function testStringAscii()
  63. {
  64. $this->assertSame('@', Str::ascii('@'));
  65. $this->assertSame('u', Str::ascii('ü'));
  66. }
  67. public function testStringAsciiWithSpecificLocale()
  68. {
  69. $this->assertSame('h H sht Sht a A ia yo', Str::ascii('х Х щ Щ ъ Ъ иа йо', 'bg'));
  70. $this->assertSame('ae oe ue Ae Oe Ue', Str::ascii('ä ö ü Ä Ö Ü', 'de'));
  71. }
  72. public function testStartsWith()
  73. {
  74. $this->assertTrue(Str::startsWith('jason', 'jas'));
  75. $this->assertTrue(Str::startsWith('jason', 'jason'));
  76. $this->assertTrue(Str::startsWith('jason', ['jas']));
  77. $this->assertTrue(Str::startsWith('jason', ['day', 'jas']));
  78. $this->assertFalse(Str::startsWith('jason', 'day'));
  79. $this->assertFalse(Str::startsWith('jason', ['day']));
  80. $this->assertFalse(Str::startsWith('jason', null));
  81. $this->assertFalse(Str::startsWith('jason', [null]));
  82. $this->assertFalse(Str::startsWith('0123', [null]));
  83. $this->assertTrue(Str::startsWith('0123', 0));
  84. $this->assertFalse(Str::startsWith('jason', 'J'));
  85. $this->assertFalse(Str::startsWith('jason', ''));
  86. $this->assertFalse(Str::startsWith('', ''));
  87. $this->assertFalse(Str::startsWith('7', ' 7'));
  88. $this->assertTrue(Str::startsWith('7a', '7'));
  89. $this->assertTrue(Str::startsWith('7a', 7));
  90. $this->assertTrue(Str::startsWith('7.12a', 7.12));
  91. $this->assertFalse(Str::startsWith('7.12a', 7.13));
  92. $this->assertTrue(Str::startsWith(7.123, '7'));
  93. $this->assertTrue(Str::startsWith(7.123, '7.12'));
  94. $this->assertFalse(Str::startsWith(7.123, '7.13'));
  95. // Test for multibyte string support
  96. $this->assertTrue(Str::startsWith('Jönköping', 'Jö'));
  97. $this->assertTrue(Str::startsWith('Malmö', 'Malmö'));
  98. $this->assertFalse(Str::startsWith('Jönköping', 'Jonko'));
  99. $this->assertFalse(Str::startsWith('Malmö', 'Malmo'));
  100. $this->assertTrue(Str::startsWith('你好', '你'));
  101. $this->assertFalse(Str::startsWith('你好', '好'));
  102. $this->assertFalse(Str::startsWith('你好', 'a'));
  103. }
  104. public function testEndsWith()
  105. {
  106. $this->assertTrue(Str::endsWith('jason', 'on'));
  107. $this->assertTrue(Str::endsWith('jason', 'jason'));
  108. $this->assertTrue(Str::endsWith('jason', ['on']));
  109. $this->assertTrue(Str::endsWith('jason', ['no', 'on']));
  110. $this->assertFalse(Str::endsWith('jason', 'no'));
  111. $this->assertFalse(Str::endsWith('jason', ['no']));
  112. $this->assertFalse(Str::endsWith('jason', ''));
  113. $this->assertFalse(Str::endsWith('', ''));
  114. $this->assertFalse(Str::endsWith('jason', [null]));
  115. $this->assertFalse(Str::endsWith('jason', null));
  116. $this->assertFalse(Str::endsWith('jason', 'N'));
  117. $this->assertFalse(Str::endsWith('7', ' 7'));
  118. $this->assertTrue(Str::endsWith('a7', '7'));
  119. $this->assertTrue(Str::endsWith('a7', 7));
  120. $this->assertTrue(Str::endsWith('a7.12', 7.12));
  121. $this->assertFalse(Str::endsWith('a7.12', 7.13));
  122. $this->assertTrue(Str::endsWith(0.27, '7'));
  123. $this->assertTrue(Str::endsWith(0.27, '0.27'));
  124. $this->assertFalse(Str::endsWith(0.27, '8'));
  125. // Test for multibyte string support
  126. $this->assertTrue(Str::endsWith('Jönköping', 'öping'));
  127. $this->assertTrue(Str::endsWith('Malmö', 'mö'));
  128. $this->assertFalse(Str::endsWith('Jönköping', 'oping'));
  129. $this->assertFalse(Str::endsWith('Malmö', 'mo'));
  130. $this->assertTrue(Str::endsWith('你好', '好'));
  131. $this->assertFalse(Str::endsWith('你好', '你'));
  132. $this->assertFalse(Str::endsWith('你好', 'a'));
  133. }
  134. public function testStrBefore()
  135. {
  136. $this->assertSame('han', Str::before('hannah', 'nah'));
  137. $this->assertSame('ha', Str::before('hannah', 'n'));
  138. $this->assertSame('ééé ', Str::before('ééé hannah', 'han'));
  139. $this->assertSame('hannah', Str::before('hannah', 'xxxx'));
  140. $this->assertSame('hannah', Str::before('hannah', ''));
  141. $this->assertSame('han', Str::before('han0nah', '0'));
  142. $this->assertSame('han', Str::before('han0nah', 0));
  143. $this->assertSame('han', Str::before('han2nah', 2));
  144. }
  145. public function testStrBeforeLast()
  146. {
  147. $this->assertSame('yve', Str::beforeLast('yvette', 'tte'));
  148. $this->assertSame('yvet', Str::beforeLast('yvette', 't'));
  149. $this->assertSame('ééé ', Str::beforeLast('ééé yvette', 'yve'));
  150. $this->assertSame('', Str::beforeLast('yvette', 'yve'));
  151. $this->assertSame('yvette', Str::beforeLast('yvette', 'xxxx'));
  152. $this->assertSame('yvette', Str::beforeLast('yvette', ''));
  153. $this->assertSame('yv0et', Str::beforeLast('yv0et0te', '0'));
  154. $this->assertSame('yv0et', Str::beforeLast('yv0et0te', 0));
  155. $this->assertSame('yv2et', Str::beforeLast('yv2et2te', 2));
  156. }
  157. public function testStrBetween()
  158. {
  159. $this->assertSame('abc', Str::between('abc', '', 'c'));
  160. $this->assertSame('abc', Str::between('abc', 'a', ''));
  161. $this->assertSame('abc', Str::between('abc', '', ''));
  162. $this->assertSame('b', Str::between('abc', 'a', 'c'));
  163. $this->assertSame('b', Str::between('dddabc', 'a', 'c'));
  164. $this->assertSame('b', Str::between('abcddd', 'a', 'c'));
  165. $this->assertSame('b', Str::between('dddabcddd', 'a', 'c'));
  166. $this->assertSame('nn', Str::between('hannah', 'ha', 'ah'));
  167. $this->assertSame('a]ab[b', Str::between('[a]ab[b]', '[', ']'));
  168. $this->assertSame('foo', Str::between('foofoobar', 'foo', 'bar'));
  169. $this->assertSame('bar', Str::between('foobarbar', 'foo', 'bar'));
  170. }
  171. public function testStrAfter()
  172. {
  173. $this->assertSame('nah', Str::after('hannah', 'han'));
  174. $this->assertSame('nah', Str::after('hannah', 'n'));
  175. $this->assertSame('nah', Str::after('ééé hannah', 'han'));
  176. $this->assertSame('hannah', Str::after('hannah', 'xxxx'));
  177. $this->assertSame('hannah', Str::after('hannah', ''));
  178. $this->assertSame('nah', Str::after('han0nah', '0'));
  179. $this->assertSame('nah', Str::after('han0nah', 0));
  180. $this->assertSame('nah', Str::after('han2nah', 2));
  181. }
  182. public function testStrAfterLast()
  183. {
  184. $this->assertSame('tte', Str::afterLast('yvette', 'yve'));
  185. $this->assertSame('e', Str::afterLast('yvette', 't'));
  186. $this->assertSame('e', Str::afterLast('ééé yvette', 't'));
  187. $this->assertSame('', Str::afterLast('yvette', 'tte'));
  188. $this->assertSame('yvette', Str::afterLast('yvette', 'xxxx'));
  189. $this->assertSame('yvette', Str::afterLast('yvette', ''));
  190. $this->assertSame('te', Str::afterLast('yv0et0te', '0'));
  191. $this->assertSame('te', Str::afterLast('yv0et0te', 0));
  192. $this->assertSame('te', Str::afterLast('yv2et2te', 2));
  193. $this->assertSame('foo', Str::afterLast('----foo', '---'));
  194. }
  195. public function testStrContains()
  196. {
  197. $this->assertTrue(Str::contains('taylor', 'ylo'));
  198. $this->assertTrue(Str::contains('taylor', 'taylor'));
  199. $this->assertTrue(Str::contains('taylor', ['ylo']));
  200. $this->assertTrue(Str::contains('taylor', ['xxx', 'ylo']));
  201. $this->assertFalse(Str::contains('taylor', 'xxx'));
  202. $this->assertFalse(Str::contains('taylor', ['xxx']));
  203. $this->assertFalse(Str::contains('taylor', ''));
  204. $this->assertFalse(Str::contains('', ''));
  205. }
  206. public function testStrContainsAll()
  207. {
  208. $this->assertTrue(Str::containsAll('taylor otwell', ['taylor', 'otwell']));
  209. $this->assertTrue(Str::containsAll('taylor otwell', ['taylor']));
  210. $this->assertFalse(Str::containsAll('taylor otwell', ['taylor', 'xxx']));
  211. }
  212. public function testParseCallback()
  213. {
  214. $this->assertEquals(['Class', 'method'], Str::parseCallback('Class@method', 'foo'));
  215. $this->assertEquals(['Class', 'foo'], Str::parseCallback('Class', 'foo'));
  216. $this->assertEquals(['Class', null], Str::parseCallback('Class'));
  217. }
  218. public function testSlug()
  219. {
  220. $this->assertSame('hello-world', Str::slug('hello world'));
  221. $this->assertSame('hello-world', Str::slug('hello-world'));
  222. $this->assertSame('hello-world', Str::slug('hello_world'));
  223. $this->assertSame('hello_world', Str::slug('hello_world', '_'));
  224. $this->assertSame('user-at-host', Str::slug('user@host'));
  225. $this->assertSame('سلام-دنیا', Str::slug('سلام دنیا', '-', null));
  226. $this->assertSame('sometext', Str::slug('some text', ''));
  227. $this->assertSame('', Str::slug('', ''));
  228. $this->assertSame('', Str::slug(''));
  229. }
  230. public function testStrStart()
  231. {
  232. $this->assertSame('/test/string', Str::start('test/string', '/'));
  233. $this->assertSame('/test/string', Str::start('/test/string', '/'));
  234. $this->assertSame('/test/string', Str::start('//test/string', '/'));
  235. }
  236. public function testFlushCache()
  237. {
  238. $reflection = new ReflectionClass(Str::class);
  239. $property = $reflection->getProperty('snakeCache');
  240. $property->setAccessible(true);
  241. Str::flushCache();
  242. $this->assertEmpty($property->getValue());
  243. Str::snake('Taylor Otwell');
  244. $this->assertNotEmpty($property->getValue());
  245. Str::flushCache();
  246. $this->assertEmpty($property->getValue());
  247. }
  248. public function testFinish()
  249. {
  250. $this->assertSame('abbc', Str::finish('ab', 'bc'));
  251. $this->assertSame('abbc', Str::finish('abbcbc', 'bc'));
  252. $this->assertSame('abcbbc', Str::finish('abcbbcbc', 'bc'));
  253. }
  254. public function testIs()
  255. {
  256. $this->assertTrue(Str::is('/', '/'));
  257. $this->assertFalse(Str::is('/', ' /'));
  258. $this->assertFalse(Str::is('/', '/a'));
  259. $this->assertTrue(Str::is('foo/*', 'foo/bar/baz'));
  260. $this->assertTrue(Str::is('*@*', 'App\Class@method'));
  261. $this->assertTrue(Str::is('*@*', 'app\Class@'));
  262. $this->assertTrue(Str::is('*@*', '@method'));
  263. // is case sensitive
  264. $this->assertFalse(Str::is('*BAZ*', 'foo/bar/baz'));
  265. $this->assertFalse(Str::is('*FOO*', 'foo/bar/baz'));
  266. $this->assertFalse(Str::is('A', 'a'));
  267. // Accepts array of patterns
  268. $this->assertTrue(Str::is(['a*', 'b*'], 'a/'));
  269. $this->assertTrue(Str::is(['a*', 'b*'], 'b/'));
  270. $this->assertFalse(Str::is(['a*', 'b*'], 'f/'));
  271. // numeric values and patterns
  272. $this->assertFalse(Str::is(['a*', 'b*'], 123));
  273. $this->assertTrue(Str::is(['*2*', 'b*'], 11211));
  274. $this->assertTrue(Str::is('*/foo', 'blah/baz/foo'));
  275. $valueObject = new StringableObjectStub('foo/bar/baz');
  276. $patternObject = new StringableObjectStub('foo/*');
  277. $this->assertTrue(Str::is('foo/bar/baz', $valueObject));
  278. $this->assertTrue(Str::is($patternObject, $valueObject));
  279. // empty patterns
  280. $this->assertFalse(Str::is([], 'test'));
  281. $this->assertFalse(Str::is('', 0));
  282. $this->assertFalse(Str::is([null], 0));
  283. $this->assertTrue(Str::is([null], null));
  284. }
  285. /**
  286. * @dataProvider validUuidList
  287. */
  288. public function testIsUuidWithValidUuid($uuid)
  289. {
  290. $this->assertTrue(Str::isUuid($uuid));
  291. }
  292. /**
  293. * @dataProvider invalidUuidList
  294. */
  295. public function testIsUuidWithInvalidUuid($uuid)
  296. {
  297. $this->assertFalse(Str::isUuid($uuid));
  298. }
  299. public function testKebab()
  300. {
  301. $this->assertSame('laravel-php-framework', Str::kebab('LaravelPhpFramework'));
  302. }
  303. public function testLower()
  304. {
  305. $this->assertSame('foo bar baz', Str::lower('FOO BAR BAZ'));
  306. $this->assertSame('foo bar baz', Str::lower('fOo Bar bAz'));
  307. }
  308. public function testUpper()
  309. {
  310. $this->assertSame('FOO BAR BAZ', Str::upper('foo bar baz'));
  311. $this->assertSame('FOO BAR BAZ', Str::upper('foO bAr BaZ'));
  312. }
  313. public function testLimit()
  314. {
  315. $this->assertSame('Laravel is...', Str::limit('Laravel is a free, open source PHP web application framework.', 10));
  316. $this->assertSame('这是一...', Str::limit('这是一段中文', 6));
  317. $string = 'The PHP framework for web artisans.';
  318. $this->assertSame('The PHP...', Str::limit($string, 7));
  319. $this->assertSame('The PHP', Str::limit($string, 7, ''));
  320. $this->assertSame('The PHP framework for web artisans.', Str::limit($string, 100));
  321. $nonAsciiString = '这是一段中文';
  322. $this->assertSame('这是一...', Str::limit($nonAsciiString, 6));
  323. $this->assertSame('这是一', Str::limit($nonAsciiString, 6, ''));
  324. }
  325. public function testLength()
  326. {
  327. $this->assertEquals(11, Str::length('foo bar baz'));
  328. $this->assertEquals(11, Str::length('foo bar baz', 'UTF-8'));
  329. }
  330. public function testRandom()
  331. {
  332. $this->assertEquals(16, strlen(Str::random()));
  333. $randomInteger = random_int(1, 100);
  334. $this->assertEquals($randomInteger, strlen(Str::random($randomInteger)));
  335. $this->assertIsString(Str::random());
  336. }
  337. public function testReplace()
  338. {
  339. $this->assertSame('foo bar laravel', Str::replace('baz', 'laravel', 'foo bar baz'));
  340. $this->assertSame('foo bar baz 8.x', Str::replace('?', '8.x', 'foo bar baz ?'));
  341. $this->assertSame('foo/bar/baz', Str::replace(' ', '/', 'foo bar baz'));
  342. $this->assertSame('foo bar baz', Str::replace(['?1', '?2', '?3'], ['foo', 'bar', 'baz'], '?1 ?2 ?3'));
  343. }
  344. public function testReplaceArray()
  345. {
  346. $this->assertSame('foo/bar/baz', Str::replaceArray('?', ['foo', 'bar', 'baz'], '?/?/?'));
  347. $this->assertSame('foo/bar/baz/?', Str::replaceArray('?', ['foo', 'bar', 'baz'], '?/?/?/?'));
  348. $this->assertSame('foo/bar', Str::replaceArray('?', ['foo', 'bar', 'baz'], '?/?'));
  349. $this->assertSame('?/?/?', Str::replaceArray('x', ['foo', 'bar', 'baz'], '?/?/?'));
  350. // Ensure recursive replacements are avoided
  351. $this->assertSame('foo?/bar/baz', Str::replaceArray('?', ['foo?', 'bar', 'baz'], '?/?/?'));
  352. // Test for associative array support
  353. $this->assertSame('foo/bar', Str::replaceArray('?', [1 => 'foo', 2 => 'bar'], '?/?'));
  354. $this->assertSame('foo/bar', Str::replaceArray('?', ['x' => 'foo', 'y' => 'bar'], '?/?'));
  355. }
  356. public function testReplaceFirst()
  357. {
  358. $this->assertSame('fooqux foobar', Str::replaceFirst('bar', 'qux', 'foobar foobar'));
  359. $this->assertSame('foo/qux? foo/bar?', Str::replaceFirst('bar?', 'qux?', 'foo/bar? foo/bar?'));
  360. $this->assertSame('foo foobar', Str::replaceFirst('bar', '', 'foobar foobar'));
  361. $this->assertSame('foobar foobar', Str::replaceFirst('xxx', 'yyy', 'foobar foobar'));
  362. $this->assertSame('foobar foobar', Str::replaceFirst('', 'yyy', 'foobar foobar'));
  363. // Test for multibyte string support
  364. $this->assertSame('Jxxxnköping Malmö', Str::replaceFirst('ö', 'xxx', 'Jönköping Malmö'));
  365. $this->assertSame('Jönköping Malmö', Str::replaceFirst('', 'yyy', 'Jönköping Malmö'));
  366. }
  367. public function testReplaceLast()
  368. {
  369. $this->assertSame('foobar fooqux', Str::replaceLast('bar', 'qux', 'foobar foobar'));
  370. $this->assertSame('foo/bar? foo/qux?', Str::replaceLast('bar?', 'qux?', 'foo/bar? foo/bar?'));
  371. $this->assertSame('foobar foo', Str::replaceLast('bar', '', 'foobar foobar'));
  372. $this->assertSame('foobar foobar', Str::replaceLast('xxx', 'yyy', 'foobar foobar'));
  373. $this->assertSame('foobar foobar', Str::replaceLast('', 'yyy', 'foobar foobar'));
  374. // Test for multibyte string support
  375. $this->assertSame('Malmö Jönkxxxping', Str::replaceLast('ö', 'xxx', 'Malmö Jönköping'));
  376. $this->assertSame('Malmö Jönköping', Str::replaceLast('', 'yyy', 'Malmö Jönköping'));
  377. }
  378. public function testRemove()
  379. {
  380. $this->assertSame('Fbar', Str::remove('o', 'Foobar'));
  381. $this->assertSame('Foo', Str::remove('bar', 'Foobar'));
  382. $this->assertSame('oobar', Str::remove('F', 'Foobar'));
  383. $this->assertSame('Foobar', Str::remove('f', 'Foobar'));
  384. $this->assertSame('oobar', Str::remove('f', 'Foobar', false));
  385. $this->assertSame('Fbr', Str::remove(['o', 'a'], 'Foobar'));
  386. $this->assertSame('Fooar', Str::remove(['f', 'b'], 'Foobar'));
  387. $this->assertSame('ooar', Str::remove(['f', 'b'], 'Foobar', false));
  388. $this->assertSame('Foobar', Str::remove(['f', '|'], 'Foo|bar'));
  389. }
  390. public function testReverse()
  391. {
  392. $this->assertSame('FooBar', Str::reverse('raBooF'));
  393. $this->assertSame('Teniszütő', Str::reverse('őtüzsineT'));
  394. $this->assertSame('❤MultiByte☆', Str::reverse('☆etyBitluM❤'));
  395. }
  396. public function testSnake()
  397. {
  398. $this->assertSame('laravel_p_h_p_framework', Str::snake('LaravelPHPFramework'));
  399. $this->assertSame('laravel_php_framework', Str::snake('LaravelPhpFramework'));
  400. $this->assertSame('laravel php framework', Str::snake('LaravelPhpFramework', ' '));
  401. $this->assertSame('laravel_php_framework', Str::snake('Laravel Php Framework'));
  402. $this->assertSame('laravel_php_framework', Str::snake('Laravel Php Framework '));
  403. // ensure cache keys don't overlap
  404. $this->assertSame('laravel__php__framework', Str::snake('LaravelPhpFramework', '__'));
  405. $this->assertSame('laravel_php_framework_', Str::snake('LaravelPhpFramework_', '_'));
  406. $this->assertSame('laravel_php_framework', Str::snake('laravel php Framework'));
  407. $this->assertSame('laravel_php_frame_work', Str::snake('laravel php FrameWork'));
  408. // prevent breaking changes
  409. $this->assertSame('foo-bar', Str::snake('foo-bar'));
  410. $this->assertSame('foo-_bar', Str::snake('Foo-Bar'));
  411. $this->assertSame('foo__bar', Str::snake('Foo_Bar'));
  412. $this->assertSame('żółtałódka', Str::snake('ŻółtaŁódka'));
  413. }
  414. public function testStudly()
  415. {
  416. $this->assertSame('LaravelPHPFramework', Str::studly('laravel_p_h_p_framework'));
  417. $this->assertSame('LaravelPhpFramework', Str::studly('laravel_php_framework'));
  418. $this->assertSame('LaravelPhPFramework', Str::studly('laravel-phP-framework'));
  419. $this->assertSame('LaravelPhpFramework', Str::studly('laravel -_- php -_- framework '));
  420. $this->assertSame('FooBar', Str::studly('fooBar'));
  421. $this->assertSame('FooBar', Str::studly('foo_bar'));
  422. $this->assertSame('FooBar', Str::studly('foo_bar')); // test cache
  423. $this->assertSame('FooBarBaz', Str::studly('foo-barBaz'));
  424. $this->assertSame('FooBarBaz', Str::studly('foo-bar_baz'));
  425. $this->assertSame('ÖffentlicheÜberraschungen', Str::studly('öffentliche-überraschungen'));
  426. }
  427. public function testMask()
  428. {
  429. $this->assertSame('tay*************', Str::mask('taylor@email.com', '*', 3));
  430. $this->assertSame('******@email.com', Str::mask('taylor@email.com', '*', 0, 6));
  431. $this->assertSame('tay*************', Str::mask('taylor@email.com', '*', -13));
  432. $this->assertSame('tay***@email.com', Str::mask('taylor@email.com', '*', -13, 3));
  433. $this->assertSame('****************', Str::mask('taylor@email.com', '*', -17));
  434. $this->assertSame('*****r@email.com', Str::mask('taylor@email.com', '*', -99, 5));
  435. $this->assertSame('taylor@email.com', Str::mask('taylor@email.com', '*', 16));
  436. $this->assertSame('taylor@email.com', Str::mask('taylor@email.com', '*', 16, 99));
  437. $this->assertSame('taylor@email.com', Str::mask('taylor@email.com', '', 3));
  438. $this->assertSame('taysssssssssssss', Str::mask('taylor@email.com', 'something', 3));
  439. $this->assertSame('taysssssssssssss', Str::mask('taylor@email.com', Str::of('something'), 3));
  440. $this->assertSame('这是一***', Str::mask('这是一段中文', '*', 3));
  441. $this->assertSame('**一段中文', Str::mask('这是一段中文', '*', 0, 2));
  442. $this->assertSame('ma*n@email.com', Str::mask('maan@email.com', '*', 2, 1));
  443. $this->assertSame('ma***email.com', Str::mask('maan@email.com', '*', 2, 3));
  444. $this->assertSame('ma************', Str::mask('maan@email.com', '*', 2));
  445. $this->assertSame('mari*@email.com', Str::mask('maria@email.com', '*', 4, 1));
  446. $this->assertSame('tamar*@email.com', Str::mask('tamara@email.com', '*', 5, 1));
  447. $this->assertSame('*aria@email.com', Str::mask('maria@email.com', '*', 0, 1));
  448. $this->assertSame('maria@email.co*', Str::mask('maria@email.com', '*', -1, 1));
  449. $this->assertSame('maria@email.co*', Str::mask('maria@email.com', '*', -1));
  450. $this->assertSame('***************', Str::mask('maria@email.com', '*', -15));
  451. $this->assertSame('***************', Str::mask('maria@email.com', '*', 0));
  452. }
  453. public function testMatch()
  454. {
  455. $this->assertSame('bar', Str::match('/bar/', 'foo bar'));
  456. $this->assertSame('bar', Str::match('/foo (.*)/', 'foo bar'));
  457. $this->assertEmpty(Str::match('/nothing/', 'foo bar'));
  458. $this->assertEquals(['bar', 'bar'], Str::matchAll('/bar/', 'bar foo bar')->all());
  459. $this->assertEquals(['un', 'ly'], Str::matchAll('/f(\w*)/', 'bar fun bar fly')->all());
  460. $this->assertEmpty(Str::matchAll('/nothing/', 'bar fun bar fly'));
  461. }
  462. public function testCamel()
  463. {
  464. $this->assertSame('laravelPHPFramework', Str::camel('Laravel_p_h_p_framework'));
  465. $this->assertSame('laravelPhpFramework', Str::camel('Laravel_php_framework'));
  466. $this->assertSame('laravelPhPFramework', Str::camel('Laravel-phP-framework'));
  467. $this->assertSame('laravelPhpFramework', Str::camel('Laravel -_- php -_- framework '));
  468. $this->assertSame('fooBar', Str::camel('FooBar'));
  469. $this->assertSame('fooBar', Str::camel('foo_bar'));
  470. $this->assertSame('fooBar', Str::camel('foo_bar')); // test cache
  471. $this->assertSame('fooBarBaz', Str::camel('Foo-barBaz'));
  472. $this->assertSame('fooBarBaz', Str::camel('foo-bar_baz'));
  473. }
  474. public function testSubstr()
  475. {
  476. $this->assertSame('Ё', Str::substr('БГДЖИЛЁ', -1));
  477. $this->assertSame('ЛЁ', Str::substr('БГДЖИЛЁ', -2));
  478. $this->assertSame('И', Str::substr('БГДЖИЛЁ', -3, 1));
  479. $this->assertSame('ДЖИЛ', Str::substr('БГДЖИЛЁ', 2, -1));
  480. $this->assertEmpty(Str::substr('БГДЖИЛЁ', 4, -4));
  481. $this->assertSame('ИЛ', Str::substr('БГДЖИЛЁ', -3, -1));
  482. $this->assertSame('ГДЖИЛЁ', Str::substr('БГДЖИЛЁ', 1));
  483. $this->assertSame('ГДЖ', Str::substr('БГДЖИЛЁ', 1, 3));
  484. $this->assertSame('БГДЖ', Str::substr('БГДЖИЛЁ', 0, 4));
  485. $this->assertSame('Ё', Str::substr('БГДЖИЛЁ', -1, 1));
  486. $this->assertEmpty(Str::substr('Б', 2));
  487. }
  488. public function testSubstrCount()
  489. {
  490. $this->assertSame(3, Str::substrCount('laravelPHPFramework', 'a'));
  491. $this->assertSame(0, Str::substrCount('laravelPHPFramework', 'z'));
  492. $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'l', 2));
  493. $this->assertSame(0, Str::substrCount('laravelPHPFramework', 'z', 2));
  494. $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'k', -1));
  495. $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'k', -1));
  496. $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', 1, 2));
  497. $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', 1, 2));
  498. $this->assertSame(3, Str::substrCount('laravelPHPFramework', 'a', 1, -2));
  499. $this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', -10, -3));
  500. }
  501. public function testSubstrReplace()
  502. {
  503. $this->assertSame('12:00', Str::substrReplace('1200', ':', 2, 0));
  504. $this->assertSame('The Laravel Framework', Str::substrReplace('The Framework', 'Laravel ', 4, 0));
  505. $this->assertSame('Laravel – The PHP Framework for Web Artisans', Str::substrReplace('Laravel Framework', '– The PHP Framework for Web Artisans', 8));
  506. }
  507. public function testUcfirst()
  508. {
  509. $this->assertSame('Laravel', Str::ucfirst('laravel'));
  510. $this->assertSame('Laravel framework', Str::ucfirst('laravel framework'));
  511. $this->assertSame('Мама', Str::ucfirst('мама'));
  512. $this->assertSame('Мама мыла раму', Str::ucfirst('мама мыла раму'));
  513. }
  514. public function testUcsplit()
  515. {
  516. $this->assertSame(['Laravel_p_h_p_framework'], Str::ucsplit('Laravel_p_h_p_framework'));
  517. $this->assertSame(['Laravel_', 'P_h_p_framework'], Str::ucsplit('Laravel_P_h_p_framework'));
  518. $this->assertSame(['laravel', 'P', 'H', 'P', 'Framework'], Str::ucsplit('laravelPHPFramework'));
  519. $this->assertSame(['Laravel-ph', 'P-framework'], Str::ucsplit('Laravel-phP-framework'));
  520. $this->assertSame(['Żółta', 'Łódka'], Str::ucsplit('ŻółtaŁódka'));
  521. $this->assertSame(['sind', 'Öde', 'Und', 'So'], Str::ucsplit('sindÖdeUndSo'));
  522. $this->assertSame(['Öffentliche', 'Überraschungen'], Str::ucsplit('ÖffentlicheÜberraschungen'));
  523. }
  524. public function testUuid()
  525. {
  526. $this->assertInstanceOf(UuidInterface::class, Str::uuid());
  527. $this->assertInstanceOf(UuidInterface::class, Str::orderedUuid());
  528. }
  529. public function testAsciiNull()
  530. {
  531. $this->assertSame('', Str::ascii(null));
  532. $this->assertTrue(Str::isAscii(null));
  533. $this->assertSame('', Str::slug(null));
  534. }
  535. public function testPadBoth()
  536. {
  537. $this->assertSame('__Alien___', Str::padBoth('Alien', 10, '_'));
  538. $this->assertSame(' Alien ', Str::padBoth('Alien', 10));
  539. $this->assertSame(' ❤MultiByte☆ ', Str::padBoth('❤MultiByte☆', 16));
  540. }
  541. public function testPadLeft()
  542. {
  543. $this->assertSame('-=-=-Alien', Str::padLeft('Alien', 10, '-='));
  544. $this->assertSame(' Alien', Str::padLeft('Alien', 10));
  545. $this->assertSame(' ❤MultiByte☆', Str::padLeft('❤MultiByte☆', 16));
  546. }
  547. public function testPadRight()
  548. {
  549. $this->assertSame('Alien-----', Str::padRight('Alien', 10, '-'));
  550. $this->assertSame('Alien ', Str::padRight('Alien', 10));
  551. $this->assertSame('❤MultiByte☆ ', Str::padRight('❤MultiByte☆', 16));
  552. }
  553. public function testSwapKeywords(): void
  554. {
  555. $this->assertSame(
  556. 'PHP 8 is fantastic',
  557. Str::swap([
  558. 'PHP' => 'PHP 8',
  559. 'awesome' => 'fantastic',
  560. ], 'PHP is awesome')
  561. );
  562. $this->assertSame(
  563. 'foo bar baz',
  564. Str::swap([
  565. 'ⓐⓑ' => 'baz',
  566. ], 'foo bar ⓐⓑ')
  567. );
  568. }
  569. public function testWordCount()
  570. {
  571. $this->assertEquals(2, Str::wordCount('Hello, world!'));
  572. $this->assertEquals(10, Str::wordCount('Hi, this is my first contribution to the Laravel framework.'));
  573. }
  574. public function validUuidList()
  575. {
  576. return [
  577. ['a0a2a2d2-0b87-4a18-83f2-2529882be2de'],
  578. ['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
  579. ['00000000-0000-0000-0000-000000000000'],
  580. ['e60d3f48-95d7-4d8d-aad0-856f29a27da2'],
  581. ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'],
  582. ['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'],
  583. ['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'],
  584. ['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'],
  585. ['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'],
  586. ['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'],
  587. ];
  588. }
  589. public function invalidUuidList()
  590. {
  591. return [
  592. ['not a valid uuid so we can test this'],
  593. ['zf6f8cb0-c57d-11e1-9b21-0800200c9a66'],
  594. ['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'.PHP_EOL],
  595. ['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1 '],
  596. [' 145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
  597. ['145a1e72-d11d-11e8-a8d5-f2z01f1b9fd1'],
  598. ['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'],
  599. ['af6f8cb-c57d-11e1-9b21-0800200c9a66'],
  600. ['af6f8cb0c57d11e19b210800200c9a66'],
  601. ['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'],
  602. ];
  603. }
  604. public function testMarkdown()
  605. {
  606. $this->assertSame("<p><em>hello world</em></p>\n", Str::markdown('*hello world*'));
  607. $this->assertSame("<h1>hello world</h1>\n", Str::markdown('# hello world'));
  608. }
  609. public function testRepeat()
  610. {
  611. $this->assertSame('aaaaa', Str::repeat('a', 5));
  612. $this->assertSame('', Str::repeat('', 5));
  613. }
  614. /**
  615. * @dataProvider specialCharacterProvider
  616. */
  617. public function testTransliterate(string $value, string $expected): void
  618. {
  619. $this->assertSame($expected, Str::transliterate($value));
  620. }
  621. public function specialCharacterProvider(): array
  622. {
  623. return [
  624. ['ⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ', 'abcdefghijklmnopqrstuvwxyz'],
  625. ['⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳', '01234567891011121314151617181920'],
  626. ['⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾', '12345678910'],
  627. ['⓿⓫⓬⓭⓮⓯⓰⓱⓲⓳⓴', '011121314151617181920'],
  628. ['ⓣⓔⓢⓣ@ⓛⓐⓡⓐⓥⓔⓛ.ⓒⓞⓜ', 'test@laravel.com'],
  629. ['🎂', '?'],
  630. ['abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'],
  631. ['0123456789', '0123456789'],
  632. ];
  633. }
  634. public function testTransliterateOverrideUnknown(): void
  635. {
  636. $this->assertSame('HHH', Str::transliterate('🎂🚧🏆', 'H'));
  637. $this->assertSame('Hello', Str::transliterate('🎂', 'Hello'));
  638. }
  639. /**
  640. * @dataProvider specialCharacterProvider
  641. */
  642. public function testTransliterateStrict(string $value, string $expected): void
  643. {
  644. $this->assertSame($expected, Str::transliterate($value, '?', true));
  645. }
  646. }
  647. class StringableObjectStub
  648. {
  649. private $value;
  650. public function __construct($value)
  651. {
  652. $this->value = $value;
  653. }
  654. public function __toString()
  655. {
  656. return $this->value;
  657. }
  658. }