Aucune description

StringyTest.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. <?php
  2. require __DIR__ . '/../src/Stringy.php';
  3. use Stringy\Stringy as S;
  4. class StringyTestCase extends CommonTest
  5. {
  6. public function testConstruct()
  7. {
  8. $stringy = new S('foo bar', 'UTF-8');
  9. $this->assertStringy($stringy);
  10. $this->assertEquals('foo bar', (string) $stringy);
  11. $this->assertEquals('UTF-8', $stringy->getEncoding());
  12. }
  13. /**
  14. * @expectedException InvalidArgumentException
  15. */
  16. public function testConstructWithArray()
  17. {
  18. (string) new S(array());
  19. $this->fail('Expecting exception when the constructor is passed an array');
  20. }
  21. /**
  22. * @expectedException InvalidArgumentException
  23. */
  24. public function testMissingToString()
  25. {
  26. (string) new S(new stdClass());
  27. $this->fail('Expecting exception when the constructor is passed an ' .
  28. 'object without a __toString method');
  29. }
  30. /**
  31. * @dataProvider toStringProvider()
  32. */
  33. public function testToString($expected, $str)
  34. {
  35. $this->assertEquals($expected, (string) new S($str));
  36. }
  37. public function toStringProvider()
  38. {
  39. return array(
  40. array('', null),
  41. array('', false),
  42. array('1', true),
  43. array('-9', -9),
  44. array('1.18', 1.18),
  45. array(' string ', ' string ')
  46. );
  47. }
  48. public function testCreate()
  49. {
  50. $stringy = S::create('foo bar', 'UTF-8');
  51. $this->assertStringy($stringy);
  52. $this->assertEquals('foo bar', (string) $stringy);
  53. $this->assertEquals('UTF-8', $stringy->getEncoding());
  54. }
  55. public function testChaining()
  56. {
  57. $stringy = S::create("Fòô Bàř", 'UTF-8');
  58. $this->assertStringy($stringy);
  59. $result = $stringy->collapseWhitespace()->swapCase()->upperCaseFirst();
  60. $this->assertEquals('FÒÔ bÀŘ', $result);
  61. }
  62. public function testCount()
  63. {
  64. $stringy = S::create('Fòô', 'UTF-8');
  65. $this->assertEquals(3, $stringy->count());
  66. $this->assertEquals(3, count($stringy));
  67. }
  68. public function testGetIterator()
  69. {
  70. $stringy = S::create('Fòô Bàř', 'UTF-8');
  71. $valResult = array();
  72. foreach ($stringy as $char) {
  73. $valResult[] = $char;
  74. }
  75. $keyValResult = array();
  76. foreach ($stringy as $pos => $char) {
  77. $keyValResult[$pos] = $char;
  78. }
  79. $this->assertEquals(array('F', 'ò', 'ô', ' ', 'B', 'à', 'ř'), $valResult);
  80. $this->assertEquals(array('F', 'ò', 'ô', ' ', 'B', 'à', 'ř'), $keyValResult);
  81. }
  82. /**
  83. * @dataProvider offsetExistsProvider()
  84. */
  85. public function testOffsetExists($expected, $offset)
  86. {
  87. $stringy = S::create('fòô', 'UTF-8');
  88. $this->assertEquals($expected, $stringy->offsetExists($offset));
  89. $this->assertEquals($expected, isset($stringy[$offset]));
  90. }
  91. public function offsetExistsProvider()
  92. {
  93. return array(
  94. array(true, 0),
  95. array(true, 2),
  96. array(false, 3),
  97. array(true, -1),
  98. array(true, -3),
  99. array(false, -4)
  100. );
  101. }
  102. public function testOffsetGet()
  103. {
  104. $stringy = S::create('fòô', 'UTF-8');
  105. $this->assertEquals('f', $stringy->offsetGet(0));
  106. $this->assertEquals('ô', $stringy->offsetGet(2));
  107. $this->assertEquals('ô', $stringy[2]);
  108. }
  109. /**
  110. * @expectedException \OutOfBoundsException
  111. */
  112. public function testOffsetGetOutOfBounds()
  113. {
  114. $stringy = S::create('fòô', 'UTF-8');
  115. $test = $stringy[3];
  116. }
  117. /**
  118. * @expectedException \Exception
  119. */
  120. public function testOffsetSet()
  121. {
  122. $stringy = S::create('fòô', 'UTF-8');
  123. $stringy[1] = 'invalid';
  124. }
  125. /**
  126. * @expectedException \Exception
  127. */
  128. public function testOffsetUnset()
  129. {
  130. $stringy = S::create('fòô', 'UTF-8');
  131. unset($stringy[1]);
  132. }
  133. /**
  134. * @dataProvider charsProvider()
  135. */
  136. public function testChars($expected, $str, $encoding = null)
  137. {
  138. $result = S::create($str, $encoding)->chars();
  139. $this->assertInternalType('array', $result);
  140. foreach ($result as $char) {
  141. $this->assertInternalType('string', $char);
  142. }
  143. $this->assertEquals($expected, $result);
  144. }
  145. /**
  146. * @dataProvider upperCaseFirstProvider()
  147. */
  148. public function testUpperCaseFirst($expected, $str, $encoding = null)
  149. {
  150. $result = S::create($str, $encoding)->upperCaseFirst();
  151. $this->assertStringy($result);
  152. $this->assertEquals($expected, $result);
  153. }
  154. /**
  155. * @dataProvider lowerCaseFirstProvider()
  156. */
  157. public function testLowerCaseFirst($expected, $str, $encoding = null)
  158. {
  159. $stringy = S::create($str, $encoding);
  160. $result = $stringy->lowerCaseFirst();
  161. $this->assertStringy($result);
  162. $this->assertEquals($expected, $result);
  163. $this->assertEquals($str, $stringy);
  164. }
  165. /**
  166. * @dataProvider camelizeProvider()
  167. */
  168. public function testCamelize($expected, $str, $encoding = null)
  169. {
  170. $stringy = S::create($str, $encoding);
  171. $result = $stringy->camelize();
  172. $this->assertStringy($result);
  173. $this->assertEquals($expected, $result);
  174. $this->assertEquals($str, $stringy);
  175. }
  176. /**
  177. * @dataProvider upperCamelizeProvider()
  178. */
  179. public function testUpperCamelize($expected, $str, $encoding = null)
  180. {
  181. $stringy = S::create($str, $encoding);
  182. $result = $stringy->upperCamelize();
  183. $this->assertStringy($result);
  184. $this->assertEquals($expected, $result);
  185. $this->assertEquals($str, $stringy);
  186. }
  187. /**
  188. * @dataProvider dasherizeProvider()
  189. */
  190. public function testDasherize($expected, $str, $encoding = null)
  191. {
  192. $stringy = S::create($str, $encoding);
  193. $result = $stringy->dasherize();
  194. $this->assertStringy($result);
  195. $this->assertEquals($expected, $result);
  196. $this->assertEquals($str, $stringy);
  197. }
  198. /**
  199. * @dataProvider underscoredProvider()
  200. */
  201. public function testUnderscored($expected, $str, $encoding = null)
  202. {
  203. $stringy = S::create($str, $encoding);
  204. $result = $stringy->underscored();
  205. $this->assertStringy($result);
  206. $this->assertEquals($expected, $result);
  207. $this->assertEquals($str, $stringy);
  208. }
  209. /**
  210. * @dataProvider swapCaseProvider()
  211. */
  212. public function testSwapCase($expected, $str, $encoding = null)
  213. {
  214. $stringy = S::create($str, $encoding);
  215. $result = $stringy->swapCase();
  216. $this->assertStringy($result);
  217. $this->assertEquals($expected, $result);
  218. $this->assertEquals($str, $stringy);
  219. }
  220. /**
  221. * @dataProvider titleizeProvider()
  222. */
  223. public function testTitleize($expected, $str, $ignore = null,
  224. $encoding = null)
  225. {
  226. $stringy = S::create($str, $encoding);
  227. $result = $stringy->titleize($ignore);
  228. $this->assertStringy($result);
  229. $this->assertEquals($expected, $result);
  230. $this->assertEquals($str, $stringy);
  231. }
  232. /**
  233. * @dataProvider humanizeProvider()
  234. */
  235. public function testHumanize($expected, $str, $encoding = null)
  236. {
  237. $stringy = S::create($str, $encoding);
  238. $result = $stringy->humanize();
  239. $this->assertStringy($result);
  240. $this->assertEquals($expected, $result);
  241. $this->assertEquals($str, $stringy);
  242. }
  243. /**
  244. * @dataProvider tidyProvider()
  245. */
  246. public function testTidy($expected, $str)
  247. {
  248. $stringy = S::create($str);
  249. $result = $stringy->tidy();
  250. $this->assertStringy($result);
  251. $this->assertEquals($expected, $result);
  252. $this->assertEquals($str, $stringy);
  253. }
  254. /**
  255. * @dataProvider collapseWhitespaceProvider()
  256. */
  257. public function testCollapseWhitespace($expected, $str, $encoding = null)
  258. {
  259. $stringy = S::create($str, $encoding);
  260. $result = $stringy->collapseWhitespace();
  261. $this->assertStringy($result);
  262. $this->assertEquals($expected, $result);
  263. $this->assertEquals($str, $stringy);
  264. }
  265. /**
  266. * @dataProvider toAsciiProvider()
  267. */
  268. public function testToAscii($expected, $str, $removeUnsupported = true)
  269. {
  270. $stringy = S::create($str);
  271. $result = $stringy->toAscii($removeUnsupported);
  272. $this->assertStringy($result);
  273. $this->assertEquals($expected, $result);
  274. $this->assertEquals($str, $stringy);
  275. }
  276. /**
  277. * @dataProvider padProvider()
  278. */
  279. public function testPad($expected, $str, $length, $padStr = ' ',
  280. $padType = 'right', $encoding = null)
  281. {
  282. $stringy = S::create($str, $encoding);
  283. $result = $stringy->pad($length, $padStr, $padType);
  284. $this->assertStringy($result);
  285. $this->assertEquals($expected, $result);
  286. $this->assertEquals($str, $stringy);
  287. }
  288. /**
  289. * @expectedException \InvalidArgumentException
  290. */
  291. public function testPadException()
  292. {
  293. $stringy = S::create('foo');
  294. $result = $stringy->pad(5, 'foo', 'bar');
  295. }
  296. /**
  297. * @dataProvider padLeftProvider()
  298. */
  299. public function testPadLeft($expected, $str, $length, $padStr = ' ',
  300. $encoding = null)
  301. {
  302. $stringy = S::create($str, $encoding);
  303. $result = $stringy->padLeft($length, $padStr);
  304. $this->assertStringy($result);
  305. $this->assertEquals($expected, $result);
  306. $this->assertEquals($str, $stringy);
  307. }
  308. /**
  309. * @dataProvider padRightProvider()
  310. */
  311. public function testPadRight($expected, $str, $length, $padStr = ' ',
  312. $encoding = null)
  313. {
  314. $stringy = S::create($str, $encoding);
  315. $result = $stringy->padRight($length, $padStr);
  316. $this->assertStringy($result);
  317. $this->assertEquals($expected, $result);
  318. $this->assertEquals($str, $stringy);
  319. }
  320. /**
  321. * @dataProvider padBothProvider()
  322. */
  323. public function testPadBoth($expected, $str, $length, $padStr = ' ',
  324. $encoding = null)
  325. {
  326. $stringy = S::create($str, $encoding);
  327. $result = $stringy->padBoth($length, $padStr);
  328. $this->assertStringy($result);
  329. $this->assertEquals($expected, $result);
  330. $this->assertEquals($str, $stringy);
  331. }
  332. /**
  333. * @dataProvider startsWithProvider()
  334. */
  335. public function testStartsWith($expected, $str, $substring,
  336. $caseSensitive = true, $encoding = null)
  337. {
  338. $stringy = S::create($str, $encoding);
  339. $result = $stringy->startsWith($substring, $caseSensitive);
  340. $this->assertInternalType('boolean', $result);
  341. $this->assertEquals($expected, $result);
  342. $this->assertEquals($str, $stringy);
  343. }
  344. /**
  345. * @dataProvider endsWithProvider()
  346. */
  347. public function testEndsWith($expected, $str, $substring,
  348. $caseSensitive = true, $encoding = null)
  349. {
  350. $stringy = S::create($str, $encoding);
  351. $result = $stringy->endsWith($substring, $caseSensitive);
  352. $this->assertInternalType('boolean', $result);
  353. $this->assertEquals($expected, $result);
  354. $this->assertEquals($str, $stringy);
  355. }
  356. /**
  357. * @dataProvider toSpacesProvider()
  358. */
  359. public function testToSpaces($expected, $str, $tabLength = 4)
  360. {
  361. $stringy = S::create($str);
  362. $result = $stringy->toSpaces($tabLength);
  363. $this->assertStringy($result);
  364. $this->assertEquals($expected, $result);
  365. $this->assertEquals($str, $stringy);
  366. }
  367. /**
  368. * @dataProvider toTabsProvider()
  369. */
  370. public function testToTabs($expected, $str, $tabLength = 4)
  371. {
  372. $stringy = S::create($str);
  373. $result = $stringy->toTabs($tabLength);
  374. $this->assertStringy($result);
  375. $this->assertEquals($expected, $result);
  376. $this->assertEquals($str, $stringy);
  377. }
  378. /**
  379. * @dataProvider toLowerCaseProvider()
  380. */
  381. public function testToLowerCase($expected, $str, $encoding = null)
  382. {
  383. $stringy = S::create($str, $encoding);
  384. $result = $stringy->toLowerCase();
  385. $this->assertStringy($result);
  386. $this->assertEquals($expected, $result);
  387. $this->assertEquals($str, $stringy);
  388. }
  389. /**
  390. * @dataProvider toTitleCaseProvider()
  391. */
  392. public function testToTitleCase($expected, $str, $encoding = null)
  393. {
  394. $stringy = S::create($str, $encoding);
  395. $result = $stringy->toTitleCase();
  396. $this->assertStringy($result);
  397. $this->assertEquals($expected, $result);
  398. $this->assertEquals($str, $stringy);
  399. }
  400. /**
  401. * @dataProvider toUpperCaseProvider()
  402. */
  403. public function testToUpperCase($expected, $str, $encoding = null)
  404. {
  405. $stringy = S::create($str, $encoding);
  406. $result = $stringy->toUpperCase();
  407. $this->assertStringy($result);
  408. $this->assertEquals($expected, $result);
  409. $this->assertEquals($str, $stringy);
  410. }
  411. /**
  412. * @dataProvider slugifyProvider()
  413. */
  414. public function testSlugify($expected, $str, $replacement = '-')
  415. {
  416. $stringy = S::create($str);
  417. $result = $stringy->slugify($replacement);
  418. $this->assertStringy($result);
  419. $this->assertEquals($expected, $result);
  420. $this->assertEquals($str, $stringy);
  421. }
  422. /**
  423. * @dataProvider containsProvider()
  424. */
  425. public function testContains($expected, $haystack, $needle,
  426. $caseSensitive = true, $encoding = null)
  427. {
  428. $stringy = S::create($haystack, $encoding);
  429. $result = $stringy->contains($needle, $caseSensitive);
  430. $this->assertInternalType('boolean', $result);
  431. $this->assertEquals($expected, $result);
  432. $this->assertEquals($haystack, $stringy);
  433. }
  434. /**
  435. * @dataProvider containsAnyProvider()
  436. */
  437. public function testcontainsAny($expected, $haystack, $needles,
  438. $caseSensitive = true, $encoding = null)
  439. {
  440. $stringy = S::create($haystack, $encoding);
  441. $result = $stringy->containsAny($needles, $caseSensitive);
  442. $this->assertInternalType('boolean', $result);
  443. $this->assertEquals($expected, $result);
  444. $this->assertEquals($haystack, $stringy);
  445. }
  446. /**
  447. * @dataProvider containsAllProvider()
  448. */
  449. public function testContainsAll($expected, $haystack, $needles,
  450. $caseSensitive = true, $encoding = null)
  451. {
  452. $stringy = S::create($haystack, $encoding);
  453. $result = $stringy->containsAll($needles, $caseSensitive);
  454. $this->assertInternalType('boolean', $result);
  455. $this->assertEquals($expected, $result);
  456. $this->assertEquals($haystack, $stringy);
  457. }
  458. /**
  459. * @dataProvider surroundProvider()
  460. */
  461. public function testSurround($expected, $str, $substring)
  462. {
  463. $stringy = S::create($str);
  464. $result = $stringy->surround($substring);
  465. $this->assertStringy($result);
  466. $this->assertEquals($expected, $result);
  467. $this->assertEquals($str, $stringy);
  468. }
  469. /**
  470. * @dataProvider insertProvider()
  471. */
  472. public function testInsert($expected, $str, $substring, $index,
  473. $encoding = null)
  474. {
  475. $stringy = S::create($str, $encoding);
  476. $result = $stringy->insert($substring, $index);
  477. $this->assertStringy($result);
  478. $this->assertEquals($expected, $result);
  479. $this->assertEquals($str, $stringy);
  480. }
  481. /**
  482. * @dataProvider truncateProvider()
  483. */
  484. public function testTruncate($expected, $str, $length, $substring = '',
  485. $encoding = null)
  486. {
  487. $stringy = S::create($str, $encoding);
  488. $result = $stringy->truncate($length, $substring);
  489. $this->assertStringy($result);
  490. $this->assertEquals($expected, $result);
  491. $this->assertEquals($str, $stringy);
  492. }
  493. /**
  494. * @dataProvider safeTruncateProvider()
  495. */
  496. public function testSafeTruncate($expected, $str, $length, $substring = '',
  497. $encoding = null)
  498. {
  499. $stringy = S::create($str, $encoding);
  500. $result = $stringy->safeTruncate($length, $substring);
  501. $this->assertStringy($result);
  502. $this->assertEquals($expected, $result);
  503. $this->assertEquals($str, $stringy);
  504. }
  505. /**
  506. * @dataProvider reverseProvider()
  507. */
  508. public function testReverse($expected, $str, $encoding = null)
  509. {
  510. $stringy = S::create($str, $encoding);
  511. $result = $stringy->reverse();
  512. $this->assertStringy($result);
  513. $this->assertEquals($expected, $result);
  514. $this->assertEquals($str, $stringy);
  515. }
  516. /**
  517. * @dataProvider shuffleProvider()
  518. */
  519. public function testShuffle($str, $encoding = null)
  520. {
  521. $stringy = S::create($str, $encoding);
  522. $encoding = $encoding ?: mb_internal_encoding();
  523. $result = $stringy->shuffle();
  524. $this->assertStringy($result);
  525. $this->assertEquals($str, $stringy);
  526. $this->assertEquals(mb_strlen($str, $encoding),
  527. mb_strlen($result, $encoding));
  528. // We'll make sure that the chars are present after shuffle
  529. for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
  530. $char = mb_substr($str, $i, 1, $encoding);
  531. $countBefore = mb_substr_count($str, $char, $encoding);
  532. $countAfter = mb_substr_count($result, $char, $encoding);
  533. $this->assertEquals($countBefore, $countAfter);
  534. }
  535. }
  536. /**
  537. * @dataProvider trimProvider()
  538. */
  539. public function testTrim($expected, $str)
  540. {
  541. $stringy = S::create($str);
  542. $result = $stringy->trim();
  543. $this->assertStringy($result);
  544. $this->assertEquals($expected, $result);
  545. $this->assertEquals($str, $stringy);
  546. }
  547. /**
  548. * @dataProvider longestCommonPrefixProvider()
  549. */
  550. public function testLongestCommonPrefix($expected, $str, $otherStr,
  551. $encoding = null)
  552. {
  553. $stringy = S::create($str, $encoding);
  554. $result = $stringy->longestCommonPrefix($otherStr);
  555. $this->assertStringy($result);
  556. $this->assertEquals($expected, $result);
  557. $this->assertEquals($str, $stringy);
  558. }
  559. /**
  560. * @dataProvider longestCommonSuffixProvider()
  561. */
  562. public function testLongestCommonSuffix($expected, $str, $otherStr,
  563. $encoding = null)
  564. {
  565. $stringy = S::create($str, $encoding);
  566. $result = $stringy->longestCommonSuffix($otherStr);
  567. $this->assertStringy($result);
  568. $this->assertEquals($expected, $result);
  569. $this->assertEquals($str, $stringy);
  570. }
  571. /**
  572. * @dataProvider longestCommonSubstringProvider()
  573. */
  574. public function testLongestCommonSubstring($expected, $str, $otherStr,
  575. $encoding = null)
  576. {
  577. $stringy = S::create($str, $encoding);
  578. $result = $stringy->longestCommonSubstring($otherStr);
  579. $this->assertStringy($result);
  580. $this->assertEquals($expected, $result);
  581. $this->assertEquals($str, $stringy);
  582. }
  583. /**
  584. * @dataProvider lengthProvider()
  585. */
  586. public function testLength($expected, $str, $encoding = null)
  587. {
  588. $stringy = S::create($str, $encoding);
  589. $result = $stringy->length();
  590. $this->assertInternalType('int', $result);
  591. $this->assertEquals($expected, $result);
  592. $this->assertEquals($str, $stringy);
  593. }
  594. /**
  595. * @dataProvider substrProvider()
  596. */
  597. public function testSubstr($expected, $str, $start, $length = null,
  598. $encoding = null)
  599. {
  600. $stringy = S::create($str, $encoding);
  601. $result = $stringy->substr($start, $length);
  602. $this->assertStringy($result);
  603. $this->assertEquals($expected, $result);
  604. $this->assertEquals($str, $stringy);
  605. }
  606. /**
  607. * @dataProvider atProvider()
  608. */
  609. public function testAt($expected, $str, $index, $encoding = null)
  610. {
  611. $stringy = S::create($str, $encoding);
  612. $result = $stringy->at($index);
  613. $this->assertStringy($result);
  614. $this->assertEquals($expected, $result);
  615. $this->assertEquals($str, $stringy);
  616. }
  617. /**
  618. * @dataProvider firstProvider()
  619. */
  620. public function testFirst($expected, $str, $n, $encoding = null)
  621. {
  622. $stringy = S::create($str, $encoding);
  623. $result = $stringy->first($n);
  624. $this->assertStringy($result);
  625. $this->assertEquals($expected, $result);
  626. $this->assertEquals($str, $stringy);
  627. }
  628. /**
  629. * @dataProvider lastProvider()
  630. */
  631. public function testLast($expected, $str, $n, $encoding = null)
  632. {
  633. $stringy = S::create($str, $encoding);
  634. $result = $stringy->last($n);
  635. $this->assertStringy($result);
  636. $this->assertEquals($expected, $result);
  637. $this->assertEquals($str, $stringy);
  638. }
  639. /**
  640. * @dataProvider ensureLeftProvider()
  641. */
  642. public function testEnsureLeft($expected, $str, $substring, $encoding = null)
  643. {
  644. $stringy = S::create($str, $encoding);
  645. $result = $stringy->ensureLeft($substring);
  646. $this->assertStringy($result);
  647. $this->assertEquals($expected, $result);
  648. $this->assertEquals($str, $stringy);
  649. }
  650. /**
  651. * @dataProvider ensureRightProvider()
  652. */
  653. public function testEnsureRight($expected, $str, $substring, $encoding = null)
  654. {
  655. $stringy = S::create($str, $encoding);
  656. $result = $stringy->ensureRight($substring);
  657. $this->assertStringy($result);
  658. $this->assertEquals($expected, $result);
  659. $this->assertEquals($str, $stringy);
  660. }
  661. /**
  662. * @dataProvider removeLeftProvider()
  663. */
  664. public function testRemoveLeft($expected, $str, $substring, $encoding = null)
  665. {
  666. $stringy = S::create($str, $encoding);
  667. $result = $stringy->removeLeft($substring);
  668. $this->assertStringy($result);
  669. $this->assertEquals($expected, $result);
  670. $this->assertEquals($str, $stringy);
  671. }
  672. /**
  673. * @dataProvider removeRightProvider()
  674. */
  675. public function testRemoveRight($expected, $str, $substring, $encoding = null)
  676. {
  677. $stringy = S::create($str, $encoding);
  678. $result = $stringy->removeRight($substring);
  679. $this->assertStringy($result);
  680. $this->assertEquals($expected, $result);
  681. $this->assertEquals($str, $stringy);
  682. }
  683. /**
  684. * @dataProvider isAlphaProvider()
  685. */
  686. public function testIsAlpha($expected, $str, $encoding = null)
  687. {
  688. $stringy = S::create($str, $encoding);
  689. $result = $stringy->isAlpha();
  690. $this->assertInternalType('boolean', $result);
  691. $this->assertEquals($expected, $result);
  692. $this->assertEquals($str, $stringy);
  693. }
  694. /**
  695. * @dataProvider isAlphanumericProvider()
  696. */
  697. public function testIsAlphanumeric($expected, $str, $encoding = null)
  698. {
  699. $stringy = S::create($str, $encoding);
  700. $result = $stringy->isAlphanumeric();
  701. $this->assertInternalType('boolean', $result);
  702. $this->assertEquals($expected, $result);
  703. $this->assertEquals($str, $stringy);
  704. }
  705. /**
  706. * @dataProvider isBlankProvider()
  707. */
  708. public function testIsBlank($expected, $str, $encoding = null)
  709. {
  710. $stringy = S::create($str, $encoding);
  711. $result = $stringy->isBlank();
  712. $this->assertInternalType('boolean', $result);
  713. $this->assertEquals($expected, $result);
  714. $this->assertEquals($str, $stringy);
  715. }
  716. /**
  717. * @dataProvider isJsonProvider()
  718. */
  719. public function testIsJson($expected, $str, $encoding = null)
  720. {
  721. $stringy = S::create($str, $encoding);
  722. $result = $stringy->isJson();
  723. $this->assertInternalType('boolean', $result);
  724. $this->assertEquals($expected, $result);
  725. $this->assertEquals($str, $stringy);
  726. }
  727. /**
  728. * @dataProvider isLowerCaseProvider()
  729. */
  730. public function testIsLowerCase($expected, $str, $encoding = null)
  731. {
  732. $stringy = S::create($str, $encoding);
  733. $result = $stringy->isLowerCase();
  734. $this->assertInternalType('boolean', $result);
  735. $this->assertEquals($expected, $result);
  736. $this->assertEquals($str, $stringy);
  737. }
  738. /**
  739. * @dataProvider hasLowerCaseProvider()
  740. */
  741. public function testHasLowerCase($expected, $str, $encoding = null)
  742. {
  743. $stringy = S::create($str, $encoding);
  744. $result = $stringy->hasLowerCase();
  745. $this->assertInternalType('boolean', $result);
  746. $this->assertEquals($expected, $result);
  747. $this->assertEquals($str, $stringy);
  748. }
  749. /**
  750. * @dataProvider isSerializedProvider()
  751. */
  752. public function testIsSerialized($expected, $str, $encoding = null)
  753. {
  754. $stringy = S::create($str, $encoding);
  755. $result = $stringy->isSerialized();
  756. $this->assertInternalType('boolean', $result);
  757. $this->assertEquals($expected, $result);
  758. $this->assertEquals($str, $stringy);
  759. }
  760. /**
  761. * @dataProvider isUpperCaseProvider()
  762. */
  763. public function testIsUpperCase($expected, $str, $encoding = null)
  764. {
  765. $stringy = S::create($str, $encoding);
  766. $result = $stringy->isUpperCase();
  767. $this->assertInternalType('boolean', $result);
  768. $this->assertEquals($expected, $result);
  769. $this->assertEquals($str, $stringy);
  770. }
  771. /**
  772. * @dataProvider hasUpperCaseProvider()
  773. */
  774. public function testHasUpperCase($expected, $str, $encoding = null)
  775. {
  776. $stringy = S::create($str, $encoding);
  777. $result = $stringy->hasUpperCase();
  778. $this->assertInternalType('boolean', $result);
  779. $this->assertEquals($expected, $result);
  780. $this->assertEquals($str, $stringy);
  781. }
  782. /**
  783. * @dataProvider isHexadecimalProvider()
  784. */
  785. public function testIsHexadecimal($expected, $str, $encoding = null)
  786. {
  787. $stringy = S::create($str, $encoding);
  788. $result = $stringy->isHexadecimal();
  789. $this->assertInternalType('boolean', $result);
  790. $this->assertEquals($expected, $result);
  791. $this->assertEquals($str, $stringy);
  792. }
  793. /**
  794. * @dataProvider countSubstrProvider()
  795. */
  796. public function testCountSubstr($expected, $str, $substring,
  797. $caseSensitive = true, $encoding = null)
  798. {
  799. $stringy = S::create($str, $encoding);
  800. $result = $stringy->countSubstr($substring, $caseSensitive);
  801. $this->assertInternalType('int', $result);
  802. $this->assertEquals($expected, $result);
  803. $this->assertEquals($str, $stringy);
  804. }
  805. /**
  806. * @dataProvider replaceProvider()
  807. */
  808. public function testReplace($expected, $str, $search, $replacement,
  809. $encoding = null)
  810. {
  811. $stringy = S::create($str, $encoding);
  812. $result = $stringy->replace($search, $replacement);
  813. $this->assertStringy($result);
  814. $this->assertEquals($expected, $result);
  815. $this->assertEquals($str, $stringy);
  816. }
  817. /**
  818. * @dataProvider regexReplaceProvider()
  819. */
  820. public function testregexReplace($expected, $str, $pattern, $replacement,
  821. $options = 'msr', $encoding = null)
  822. {
  823. $stringy = S::create($str, $encoding);
  824. $result = $stringy->regexReplace($pattern, $replacement, $options);
  825. $this->assertStringy($result);
  826. $this->assertEquals($expected, $result);
  827. $this->assertEquals($str, $stringy);
  828. }
  829. }