StringsUtilsTest.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Tests\Unit\Service\Utils;
  3. use App\Service\Utils\StringsUtils;
  4. use PHPUnit\Framework\TestCase;
  5. class StringsUtilsTest extends TestCase
  6. {
  7. /**
  8. * @see StringsUtils::unquote()
  9. */
  10. public function testUnquote(): void
  11. {
  12. $this->assertEquals('foo', StringsUtils::unquote("'foo"));
  13. }
  14. /**
  15. * @see StringsUtils::camelToSnake()
  16. */
  17. public function testCamelToSnake(): void
  18. {
  19. $this->assertEquals('foo_bar', StringsUtils::camelToSnake('FooBar'));
  20. $this->assertEquals('foo-bar', StringsUtils::camelToSnake('FooBar', '-'));
  21. $this->assertEquals('foo_bar', StringsUtils::camelToSnake('fooBar'));
  22. }
  23. /**
  24. * @see StringsUtils::convertHtmlToText()
  25. */
  26. public function testConvertHtmlToText(): void
  27. {
  28. $this->assertEquals('Test contenu', (new StringsUtils())->convertHtmlToText('<table><tr><td>Test</td></tr></table> <br /><p>contenu</p>'));
  29. }
  30. /**
  31. * @see StringsUtils::elide()
  32. */
  33. public function testElide(): void
  34. {
  35. // Test normal truncation case - string longer than length
  36. $this->assertEquals('Hello...', StringsUtils::elide('Hello World!', 8));
  37. // Test case where string is shorter than length - no truncation
  38. $this->assertEquals('Hello', StringsUtils::elide('Hello', 10));
  39. // Test edge case with exact length
  40. $this->assertEquals('Hello', StringsUtils::elide('Hello', 6));
  41. // Test with empty string
  42. $this->assertEquals('', StringsUtils::elide('', 10));
  43. }
  44. /**
  45. * @see StringsUtils::elide()
  46. */
  47. public function testElideWithMultibyteCharacters(): void
  48. {
  49. // Test with UTF-8 multibyte characters
  50. $this->assertEquals('Héllo...', StringsUtils::elide('Héllo Wörld!', 8));
  51. $this->assertEquals('测试中...', StringsUtils::elide('测试中文字符串', 6));
  52. $this->assertEquals('🎉🎊🎈🎆🎇', StringsUtils::elide('🎉🎊🎈🎆🎇', 6));
  53. }
  54. /**
  55. * @see StringsUtils::elide()
  56. */
  57. public function testElideWithInvalidLength(): void
  58. {
  59. $this->expectException(\InvalidArgumentException::class);
  60. $this->expectExceptionMessage('Length must be greater than 5');
  61. StringsUtils::elide('Hello World!', 1);
  62. }
  63. /**
  64. * @see StringsUtils::elide()
  65. */
  66. public function testElideWithLongString(): void
  67. {
  68. $longString = str_repeat('A', 1000);
  69. $result = StringsUtils::elide($longString, 50);
  70. $this->assertEquals(50, mb_strlen($result)); // 47 + 3 dots
  71. $this->assertTrue(str_ends_with($result, '...'));
  72. $this->assertEquals(str_repeat('A', 47).'...', $result);
  73. }
  74. }