/vendor/danielstjules/stringy/tests/StringyTest.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 994 lines · 676 code · 92 blank · 226 comment · 1 complexity · 542966ab3b6e922cf2ff5374ffa87ae6 MD5 · raw file

  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 indexOfProvider()
  135. */
  136. public function testIndexOf($expected, $str, $subStr, $offset = 0, $encoding = null)
  137. {
  138. $result = S::create($str, $encoding)->indexOf($subStr, $offset);
  139. $this->assertEquals($expected, $result);
  140. }
  141. /**
  142. * @dataProvider indexOfLastProvider()
  143. */
  144. public function testIndexOfLast($expected, $str, $subStr, $offset = 0, $encoding = null)
  145. {
  146. $result = S::create($str, $encoding)->indexOfLast($subStr, $offset);
  147. $this->assertEquals($expected, $result);
  148. }
  149. /**
  150. * @dataProvider charsProvider()
  151. */
  152. public function testChars($expected, $str, $encoding = null)
  153. {
  154. $result = S::create($str, $encoding)->chars();
  155. $this->assertInternalType('array', $result);
  156. foreach ($result as $char) {
  157. $this->assertInternalType('string', $char);
  158. }
  159. $this->assertEquals($expected, $result);
  160. }
  161. /**
  162. * @dataProvider upperCaseFirstProvider()
  163. */
  164. public function testUpperCaseFirst($expected, $str, $encoding = null)
  165. {
  166. $result = S::create($str, $encoding)->upperCaseFirst();
  167. $this->assertStringy($result);
  168. $this->assertEquals($expected, $result);
  169. }
  170. /**
  171. * @dataProvider lowerCaseFirstProvider()
  172. */
  173. public function testLowerCaseFirst($expected, $str, $encoding = null)
  174. {
  175. $stringy = S::create($str, $encoding);
  176. $result = $stringy->lowerCaseFirst();
  177. $this->assertStringy($result);
  178. $this->assertEquals($expected, $result);
  179. $this->assertEquals($str, $stringy);
  180. }
  181. /**
  182. * @dataProvider camelizeProvider()
  183. */
  184. public function testCamelize($expected, $str, $encoding = null)
  185. {
  186. $stringy = S::create($str, $encoding);
  187. $result = $stringy->camelize();
  188. $this->assertStringy($result);
  189. $this->assertEquals($expected, $result);
  190. $this->assertEquals($str, $stringy);
  191. }
  192. /**
  193. * @dataProvider upperCamelizeProvider()
  194. */
  195. public function testUpperCamelize($expected, $str, $encoding = null)
  196. {
  197. $stringy = S::create($str, $encoding);
  198. $result = $stringy->upperCamelize();
  199. $this->assertStringy($result);
  200. $this->assertEquals($expected, $result);
  201. $this->assertEquals($str, $stringy);
  202. }
  203. /**
  204. * @dataProvider dasherizeProvider()
  205. */
  206. public function testDasherize($expected, $str, $encoding = null)
  207. {
  208. $stringy = S::create($str, $encoding);
  209. $result = $stringy->dasherize();
  210. $this->assertStringy($result);
  211. $this->assertEquals($expected, $result);
  212. $this->assertEquals($str, $stringy);
  213. }
  214. /**
  215. * @dataProvider underscoredProvider()
  216. */
  217. public function testUnderscored($expected, $str, $encoding = null)
  218. {
  219. $stringy = S::create($str, $encoding);
  220. $result = $stringy->underscored();
  221. $this->assertStringy($result);
  222. $this->assertEquals($expected, $result);
  223. $this->assertEquals($str, $stringy);
  224. }
  225. /**
  226. * @dataProvider delimitProvider()
  227. */
  228. public function testDelimit($expected, $str, $delimiter, $encoding = null)
  229. {
  230. $stringy = S::create($str, $encoding);
  231. $result = $stringy->delimit($delimiter);
  232. $this->assertStringy($result);
  233. $this->assertEquals($expected, $result);
  234. $this->assertEquals($str, $stringy);
  235. }
  236. /**
  237. * @dataProvider swapCaseProvider()
  238. */
  239. public function testSwapCase($expected, $str, $encoding = null)
  240. {
  241. $stringy = S::create($str, $encoding);
  242. $result = $stringy->swapCase();
  243. $this->assertStringy($result);
  244. $this->assertEquals($expected, $result);
  245. $this->assertEquals($str, $stringy);
  246. }
  247. /**
  248. * @dataProvider titleizeProvider()
  249. */
  250. public function testTitleize($expected, $str, $ignore = null,
  251. $encoding = null)
  252. {
  253. $stringy = S::create($str, $encoding);
  254. $result = $stringy->titleize($ignore);
  255. $this->assertStringy($result);
  256. $this->assertEquals($expected, $result);
  257. $this->assertEquals($str, $stringy);
  258. }
  259. /**
  260. * @dataProvider humanizeProvider()
  261. */
  262. public function testHumanize($expected, $str, $encoding = null)
  263. {
  264. $stringy = S::create($str, $encoding);
  265. $result = $stringy->humanize();
  266. $this->assertStringy($result);
  267. $this->assertEquals($expected, $result);
  268. $this->assertEquals($str, $stringy);
  269. }
  270. /**
  271. * @dataProvider tidyProvider()
  272. */
  273. public function testTidy($expected, $str)
  274. {
  275. $stringy = S::create($str);
  276. $result = $stringy->tidy();
  277. $this->assertStringy($result);
  278. $this->assertEquals($expected, $result);
  279. $this->assertEquals($str, $stringy);
  280. }
  281. /**
  282. * @dataProvider collapseWhitespaceProvider()
  283. */
  284. public function testCollapseWhitespace($expected, $str, $encoding = null)
  285. {
  286. $stringy = S::create($str, $encoding);
  287. $result = $stringy->collapseWhitespace();
  288. $this->assertStringy($result);
  289. $this->assertEquals($expected, $result);
  290. $this->assertEquals($str, $stringy);
  291. }
  292. /**
  293. * @dataProvider toAsciiProvider()
  294. */
  295. public function testToAscii($expected, $str, $removeUnsupported = true)
  296. {
  297. $stringy = S::create($str);
  298. $result = $stringy->toAscii($removeUnsupported);
  299. $this->assertStringy($result);
  300. $this->assertEquals($expected, $result);
  301. $this->assertEquals($str, $stringy);
  302. }
  303. /**
  304. * @dataProvider padProvider()
  305. */
  306. public function testPad($expected, $str, $length, $padStr = ' ',
  307. $padType = 'right', $encoding = null)
  308. {
  309. $stringy = S::create($str, $encoding);
  310. $result = $stringy->pad($length, $padStr, $padType);
  311. $this->assertStringy($result);
  312. $this->assertEquals($expected, $result);
  313. $this->assertEquals($str, $stringy);
  314. }
  315. /**
  316. * @expectedException \InvalidArgumentException
  317. */
  318. public function testPadException()
  319. {
  320. $stringy = S::create('foo');
  321. $result = $stringy->pad(5, 'foo', 'bar');
  322. }
  323. /**
  324. * @dataProvider padLeftProvider()
  325. */
  326. public function testPadLeft($expected, $str, $length, $padStr = ' ',
  327. $encoding = null)
  328. {
  329. $stringy = S::create($str, $encoding);
  330. $result = $stringy->padLeft($length, $padStr);
  331. $this->assertStringy($result);
  332. $this->assertEquals($expected, $result);
  333. $this->assertEquals($str, $stringy);
  334. }
  335. /**
  336. * @dataProvider padRightProvider()
  337. */
  338. public function testPadRight($expected, $str, $length, $padStr = ' ',
  339. $encoding = null)
  340. {
  341. $stringy = S::create($str, $encoding);
  342. $result = $stringy->padRight($length, $padStr);
  343. $this->assertStringy($result);
  344. $this->assertEquals($expected, $result);
  345. $this->assertEquals($str, $stringy);
  346. }
  347. /**
  348. * @dataProvider padBothProvider()
  349. */
  350. public function testPadBoth($expected, $str, $length, $padStr = ' ',
  351. $encoding = null)
  352. {
  353. $stringy = S::create($str, $encoding);
  354. $result = $stringy->padBoth($length, $padStr);
  355. $this->assertStringy($result);
  356. $this->assertEquals($expected, $result);
  357. $this->assertEquals($str, $stringy);
  358. }
  359. /**
  360. * @dataProvider startsWithProvider()
  361. */
  362. public function testStartsWith($expected, $str, $substring,
  363. $caseSensitive = true, $encoding = null)
  364. {
  365. $stringy = S::create($str, $encoding);
  366. $result = $stringy->startsWith($substring, $caseSensitive);
  367. $this->assertInternalType('boolean', $result);
  368. $this->assertEquals($expected, $result);
  369. $this->assertEquals($str, $stringy);
  370. }
  371. /**
  372. * @dataProvider endsWithProvider()
  373. */
  374. public function testEndsWith($expected, $str, $substring,
  375. $caseSensitive = true, $encoding = null)
  376. {
  377. $stringy = S::create($str, $encoding);
  378. $result = $stringy->endsWith($substring, $caseSensitive);
  379. $this->assertInternalType('boolean', $result);
  380. $this->assertEquals($expected, $result);
  381. $this->assertEquals($str, $stringy);
  382. }
  383. /**
  384. * @dataProvider toSpacesProvider()
  385. */
  386. public function testToSpaces($expected, $str, $tabLength = 4)
  387. {
  388. $stringy = S::create($str);
  389. $result = $stringy->toSpaces($tabLength);
  390. $this->assertStringy($result);
  391. $this->assertEquals($expected, $result);
  392. $this->assertEquals($str, $stringy);
  393. }
  394. /**
  395. * @dataProvider toTabsProvider()
  396. */
  397. public function testToTabs($expected, $str, $tabLength = 4)
  398. {
  399. $stringy = S::create($str);
  400. $result = $stringy->toTabs($tabLength);
  401. $this->assertStringy($result);
  402. $this->assertEquals($expected, $result);
  403. $this->assertEquals($str, $stringy);
  404. }
  405. /**
  406. * @dataProvider toLowerCaseProvider()
  407. */
  408. public function testToLowerCase($expected, $str, $encoding = null)
  409. {
  410. $stringy = S::create($str, $encoding);
  411. $result = $stringy->toLowerCase();
  412. $this->assertStringy($result);
  413. $this->assertEquals($expected, $result);
  414. $this->assertEquals($str, $stringy);
  415. }
  416. /**
  417. * @dataProvider toTitleCaseProvider()
  418. */
  419. public function testToTitleCase($expected, $str, $encoding = null)
  420. {
  421. $stringy = S::create($str, $encoding);
  422. $result = $stringy->toTitleCase();
  423. $this->assertStringy($result);
  424. $this->assertEquals($expected, $result);
  425. $this->assertEquals($str, $stringy);
  426. }
  427. /**
  428. * @dataProvider toUpperCaseProvider()
  429. */
  430. public function testToUpperCase($expected, $str, $encoding = null)
  431. {
  432. $stringy = S::create($str, $encoding);
  433. $result = $stringy->toUpperCase();
  434. $this->assertStringy($result);
  435. $this->assertEquals($expected, $result);
  436. $this->assertEquals($str, $stringy);
  437. }
  438. /**
  439. * @dataProvider slugifyProvider()
  440. */
  441. public function testSlugify($expected, $str, $replacement = '-')
  442. {
  443. $stringy = S::create($str);
  444. $result = $stringy->slugify($replacement);
  445. $this->assertStringy($result);
  446. $this->assertEquals($expected, $result);
  447. $this->assertEquals($str, $stringy);
  448. }
  449. /**
  450. * @dataProvider containsProvider()
  451. */
  452. public function testContains($expected, $haystack, $needle,
  453. $caseSensitive = true, $encoding = null)
  454. {
  455. $stringy = S::create($haystack, $encoding);
  456. $result = $stringy->contains($needle, $caseSensitive);
  457. $this->assertInternalType('boolean', $result);
  458. $this->assertEquals($expected, $result);
  459. $this->assertEquals($haystack, $stringy);
  460. }
  461. /**
  462. * @dataProvider containsAnyProvider()
  463. */
  464. public function testcontainsAny($expected, $haystack, $needles,
  465. $caseSensitive = true, $encoding = null)
  466. {
  467. $stringy = S::create($haystack, $encoding);
  468. $result = $stringy->containsAny($needles, $caseSensitive);
  469. $this->assertInternalType('boolean', $result);
  470. $this->assertEquals($expected, $result);
  471. $this->assertEquals($haystack, $stringy);
  472. }
  473. /**
  474. * @dataProvider containsAllProvider()
  475. */
  476. public function testContainsAll($expected, $haystack, $needles,
  477. $caseSensitive = true, $encoding = null)
  478. {
  479. $stringy = S::create($haystack, $encoding);
  480. $result = $stringy->containsAll($needles, $caseSensitive);
  481. $this->assertInternalType('boolean', $result);
  482. $this->assertEquals($expected, $result);
  483. $this->assertEquals($haystack, $stringy);
  484. }
  485. /**
  486. * @dataProvider surroundProvider()
  487. */
  488. public function testSurround($expected, $str, $substring)
  489. {
  490. $stringy = S::create($str);
  491. $result = $stringy->surround($substring);
  492. $this->assertStringy($result);
  493. $this->assertEquals($expected, $result);
  494. $this->assertEquals($str, $stringy);
  495. }
  496. /**
  497. * @dataProvider insertProvider()
  498. */
  499. public function testInsert($expected, $str, $substring, $index,
  500. $encoding = null)
  501. {
  502. $stringy = S::create($str, $encoding);
  503. $result = $stringy->insert($substring, $index);
  504. $this->assertStringy($result);
  505. $this->assertEquals($expected, $result);
  506. $this->assertEquals($str, $stringy);
  507. }
  508. /**
  509. * @dataProvider truncateProvider()
  510. */
  511. public function testTruncate($expected, $str, $length, $substring = '',
  512. $encoding = null)
  513. {
  514. $stringy = S::create($str, $encoding);
  515. $result = $stringy->truncate($length, $substring);
  516. $this->assertStringy($result);
  517. $this->assertEquals($expected, $result);
  518. $this->assertEquals($str, $stringy);
  519. }
  520. /**
  521. * @dataProvider safeTruncateProvider()
  522. */
  523. public function testSafeTruncate($expected, $str, $length, $substring = '',
  524. $encoding = null)
  525. {
  526. $stringy = S::create($str, $encoding);
  527. $result = $stringy->safeTruncate($length, $substring);
  528. $this->assertStringy($result);
  529. $this->assertEquals($expected, $result);
  530. $this->assertEquals($str, $stringy);
  531. }
  532. /**
  533. * @dataProvider reverseProvider()
  534. */
  535. public function testReverse($expected, $str, $encoding = null)
  536. {
  537. $stringy = S::create($str, $encoding);
  538. $result = $stringy->reverse();
  539. $this->assertStringy($result);
  540. $this->assertEquals($expected, $result);
  541. $this->assertEquals($str, $stringy);
  542. }
  543. /**
  544. * @dataProvider shuffleProvider()
  545. */
  546. public function testShuffle($str, $encoding = null)
  547. {
  548. $stringy = S::create($str, $encoding);
  549. $encoding = $encoding ?: mb_internal_encoding();
  550. $result = $stringy->shuffle();
  551. $this->assertStringy($result);
  552. $this->assertEquals($str, $stringy);
  553. $this->assertEquals(mb_strlen($str, $encoding),
  554. mb_strlen($result, $encoding));
  555. // We'll make sure that the chars are present after shuffle
  556. for ($i = 0; $i < mb_strlen($str, $encoding); $i++) {
  557. $char = mb_substr($str, $i, 1, $encoding);
  558. $countBefore = mb_substr_count($str, $char, $encoding);
  559. $countAfter = mb_substr_count($result, $char, $encoding);
  560. $this->assertEquals($countBefore, $countAfter);
  561. }
  562. }
  563. /**
  564. * @dataProvider trimProvider()
  565. */
  566. public function testTrim($expected, $str, $chars = null, $encoding = null)
  567. {
  568. $stringy = S::create($str, $encoding);
  569. $result = $stringy->trim($chars);
  570. $this->assertStringy($result);
  571. $this->assertEquals($expected, $result);
  572. $this->assertEquals($str, $stringy);
  573. }
  574. /**
  575. * @dataProvider trimLeftProvider()
  576. */
  577. public function testTrimLeft($expected, $str, $chars = null,
  578. $encoding = null)
  579. {
  580. $stringy = S::create($str, $encoding);
  581. $result = $stringy->trimLeft($chars);
  582. $this->assertStringy($result);
  583. $this->assertEquals($expected, $result);
  584. $this->assertEquals($str, $stringy);
  585. }
  586. /**
  587. * @dataProvider trimRightProvider()
  588. */
  589. public function testTrimRight($expected, $str, $chars = null,
  590. $encoding = null)
  591. {
  592. $stringy = S::create($str, $encoding);
  593. $result = $stringy->trimRight($chars);
  594. $this->assertStringy($result);
  595. $this->assertEquals($expected, $result);
  596. $this->assertEquals($str, $stringy);
  597. }
  598. /**
  599. * @dataProvider longestCommonPrefixProvider()
  600. */
  601. public function testLongestCommonPrefix($expected, $str, $otherStr,
  602. $encoding = null)
  603. {
  604. $stringy = S::create($str, $encoding);
  605. $result = $stringy->longestCommonPrefix($otherStr);
  606. $this->assertStringy($result);
  607. $this->assertEquals($expected, $result);
  608. $this->assertEquals($str, $stringy);
  609. }
  610. /**
  611. * @dataProvider longestCommonSuffixProvider()
  612. */
  613. public function testLongestCommonSuffix($expected, $str, $otherStr,
  614. $encoding = null)
  615. {
  616. $stringy = S::create($str, $encoding);
  617. $result = $stringy->longestCommonSuffix($otherStr);
  618. $this->assertStringy($result);
  619. $this->assertEquals($expected, $result);
  620. $this->assertEquals($str, $stringy);
  621. }
  622. /**
  623. * @dataProvider longestCommonSubstringProvider()
  624. */
  625. public function testLongestCommonSubstring($expected, $str, $otherStr,
  626. $encoding = null)
  627. {
  628. $stringy = S::create($str, $encoding);
  629. $result = $stringy->longestCommonSubstring($otherStr);
  630. $this->assertStringy($result);
  631. $this->assertEquals($expected, $result);
  632. $this->assertEquals($str, $stringy);
  633. }
  634. /**
  635. * @dataProvider lengthProvider()
  636. */
  637. public function testLength($expected, $str, $encoding = null)
  638. {
  639. $stringy = S::create($str, $encoding);
  640. $result = $stringy->length();
  641. $this->assertInternalType('int', $result);
  642. $this->assertEquals($expected, $result);
  643. $this->assertEquals($str, $stringy);
  644. }
  645. /**
  646. * @dataProvider substrProvider()
  647. */
  648. public function testSubstr($expected, $str, $start, $length = null,
  649. $encoding = null)
  650. {
  651. $stringy = S::create($str, $encoding);
  652. $result = $stringy->substr($start, $length);
  653. $this->assertStringy($result);
  654. $this->assertEquals($expected, $result);
  655. $this->assertEquals($str, $stringy);
  656. }
  657. /**
  658. * @dataProvider atProvider()
  659. */
  660. public function testAt($expected, $str, $index, $encoding = null)
  661. {
  662. $stringy = S::create($str, $encoding);
  663. $result = $stringy->at($index);
  664. $this->assertStringy($result);
  665. $this->assertEquals($expected, $result);
  666. $this->assertEquals($str, $stringy);
  667. }
  668. /**
  669. * @dataProvider firstProvider()
  670. */
  671. public function testFirst($expected, $str, $n, $encoding = null)
  672. {
  673. $stringy = S::create($str, $encoding);
  674. $result = $stringy->first($n);
  675. $this->assertStringy($result);
  676. $this->assertEquals($expected, $result);
  677. $this->assertEquals($str, $stringy);
  678. }
  679. /**
  680. * @dataProvider lastProvider()
  681. */
  682. public function testLast($expected, $str, $n, $encoding = null)
  683. {
  684. $stringy = S::create($str, $encoding);
  685. $result = $stringy->last($n);
  686. $this->assertStringy($result);
  687. $this->assertEquals($expected, $result);
  688. $this->assertEquals($str, $stringy);
  689. }
  690. /**
  691. * @dataProvider ensureLeftProvider()
  692. */
  693. public function testEnsureLeft($expected, $str, $substring, $encoding = null)
  694. {
  695. $stringy = S::create($str, $encoding);
  696. $result = $stringy->ensureLeft($substring);
  697. $this->assertStringy($result);
  698. $this->assertEquals($expected, $result);
  699. $this->assertEquals($str, $stringy);
  700. }
  701. /**
  702. * @dataProvider ensureRightProvider()
  703. */
  704. public function testEnsureRight($expected, $str, $substring, $encoding = null)
  705. {
  706. $stringy = S::create($str, $encoding);
  707. $result = $stringy->ensureRight($substring);
  708. $this->assertStringy($result);
  709. $this->assertEquals($expected, $result);
  710. $this->assertEquals($str, $stringy);
  711. }
  712. /**
  713. * @dataProvider removeLeftProvider()
  714. */
  715. public function testRemoveLeft($expected, $str, $substring, $encoding = null)
  716. {
  717. $stringy = S::create($str, $encoding);
  718. $result = $stringy->removeLeft($substring);
  719. $this->assertStringy($result);
  720. $this->assertEquals($expected, $result);
  721. $this->assertEquals($str, $stringy);
  722. }
  723. /**
  724. * @dataProvider removeRightProvider()
  725. */
  726. public function testRemoveRight($expected, $str, $substring, $encoding = null)
  727. {
  728. $stringy = S::create($str, $encoding);
  729. $result = $stringy->removeRight($substring);
  730. $this->assertStringy($result);
  731. $this->assertEquals($expected, $result);
  732. $this->assertEquals($str, $stringy);
  733. }
  734. /**
  735. * @dataProvider isAlphaProvider()
  736. */
  737. public function testIsAlpha($expected, $str, $encoding = null)
  738. {
  739. $stringy = S::create($str, $encoding);
  740. $result = $stringy->isAlpha();
  741. $this->assertInternalType('boolean', $result);
  742. $this->assertEquals($expected, $result);
  743. $this->assertEquals($str, $stringy);
  744. }
  745. /**
  746. * @dataProvider isAlphanumericProvider()
  747. */
  748. public function testIsAlphanumeric($expected, $str, $encoding = null)
  749. {
  750. $stringy = S::create($str, $encoding);
  751. $result = $stringy->isAlphanumeric();
  752. $this->assertInternalType('boolean', $result);
  753. $this->assertEquals($expected, $result);
  754. $this->assertEquals($str, $stringy);
  755. }
  756. /**
  757. * @dataProvider isBlankProvider()
  758. */
  759. public function testIsBlank($expected, $str, $encoding = null)
  760. {
  761. $stringy = S::create($str, $encoding);
  762. $result = $stringy->isBlank();
  763. $this->assertInternalType('boolean', $result);
  764. $this->assertEquals($expected, $result);
  765. $this->assertEquals($str, $stringy);
  766. }
  767. /**
  768. * @dataProvider isJsonProvider()
  769. */
  770. public function testIsJson($expected, $str, $encoding = null)
  771. {
  772. $stringy = S::create($str, $encoding);
  773. $result = $stringy->isJson();
  774. $this->assertInternalType('boolean', $result);
  775. $this->assertEquals($expected, $result);
  776. $this->assertEquals($str, $stringy);
  777. }
  778. /**
  779. * @dataProvider isLowerCaseProvider()
  780. */
  781. public function testIsLowerCase($expected, $str, $encoding = null)
  782. {
  783. $stringy = S::create($str, $encoding);
  784. $result = $stringy->isLowerCase();
  785. $this->assertInternalType('boolean', $result);
  786. $this->assertEquals($expected, $result);
  787. $this->assertEquals($str, $stringy);
  788. }
  789. /**
  790. * @dataProvider hasLowerCaseProvider()
  791. */
  792. public function testHasLowerCase($expected, $str, $encoding = null)
  793. {
  794. $stringy = S::create($str, $encoding);
  795. $result = $stringy->hasLowerCase();
  796. $this->assertInternalType('boolean', $result);
  797. $this->assertEquals($expected, $result);
  798. $this->assertEquals($str, $stringy);
  799. }
  800. /**
  801. * @dataProvider isSerializedProvider()
  802. */
  803. public function testIsSerialized($expected, $str, $encoding = null)
  804. {
  805. $stringy = S::create($str, $encoding);
  806. $result = $stringy->isSerialized();
  807. $this->assertInternalType('boolean', $result);
  808. $this->assertEquals($expected, $result);
  809. $this->assertEquals($str, $stringy);
  810. }
  811. /**
  812. * @dataProvider isUpperCaseProvider()
  813. */
  814. public function testIsUpperCase($expected, $str, $encoding = null)
  815. {
  816. $stringy = S::create($str, $encoding);
  817. $result = $stringy->isUpperCase();
  818. $this->assertInternalType('boolean', $result);
  819. $this->assertEquals($expected, $result);
  820. $this->assertEquals($str, $stringy);
  821. }
  822. /**
  823. * @dataProvider hasUpperCaseProvider()
  824. */
  825. public function testHasUpperCase($expected, $str, $encoding = null)
  826. {
  827. $stringy = S::create($str, $encoding);
  828. $result = $stringy->hasUpperCase();
  829. $this->assertInternalType('boolean', $result);
  830. $this->assertEquals($expected, $result);
  831. $this->assertEquals($str, $stringy);
  832. }
  833. /**
  834. * @dataProvider isHexadecimalProvider()
  835. */
  836. public function testIsHexadecimal($expected, $str, $encoding = null)
  837. {
  838. $stringy = S::create($str, $encoding);
  839. $result = $stringy->isHexadecimal();
  840. $this->assertInternalType('boolean', $result);
  841. $this->assertEquals($expected, $result);
  842. $this->assertEquals($str, $stringy);
  843. }
  844. /**
  845. * @dataProvider countSubstrProvider()
  846. */
  847. public function testCountSubstr($expected, $str, $substring,
  848. $caseSensitive = true, $encoding = null)
  849. {
  850. $stringy = S::create($str, $encoding);
  851. $result = $stringy->countSubstr($substring, $caseSensitive);
  852. $this->assertInternalType('int', $result);
  853. $this->assertEquals($expected, $result);
  854. $this->assertEquals($str, $stringy);
  855. }
  856. /**
  857. * @dataProvider replaceProvider()
  858. */
  859. public function testReplace($expected, $str, $search, $replacement,
  860. $encoding = null)
  861. {
  862. $stringy = S::create($str, $encoding);
  863. $result = $stringy->replace($search, $replacement);
  864. $this->assertStringy($result);
  865. $this->assertEquals($expected, $result);
  866. $this->assertEquals($str, $stringy);
  867. }
  868. /**
  869. * @dataProvider regexReplaceProvider()
  870. */
  871. public function testregexReplace($expected, $str, $pattern, $replacement,
  872. $options = 'msr', $encoding = null)
  873. {
  874. $stringy = S::create($str, $encoding);
  875. $result = $stringy->regexReplace($pattern, $replacement, $options);
  876. $this->assertStringy($result);
  877. $this->assertEquals($expected, $result);
  878. $this->assertEquals($str, $stringy);
  879. }
  880. /**
  881. * @dataProvider htmlEncodeProvider()
  882. */
  883. public function testHtmlEncode($expected, $str, $flags = ENT_COMPAT, $encoding = null)
  884. {
  885. $stringy = S::create($str, $encoding);
  886. $result = $stringy->htmlEncode($flags);
  887. $this->assertStringy($result);
  888. $this->assertEquals($expected, $result);
  889. $this->assertEquals($str, $stringy);
  890. }
  891. /**
  892. * @dataProvider htmlDecodeProvider()
  893. */
  894. public function testHtmlDecode($expected, $str, $flags = ENT_COMPAT, $encoding = null)
  895. {
  896. $stringy = S::create($str, $encoding);
  897. $result = $stringy->htmlDecode($flags);
  898. $this->assertStringy($result);
  899. $this->assertEquals($expected, $result);
  900. $this->assertEquals($str, $stringy);
  901. }
  902. }