| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Tests\Unit\Service\Utils;
- use App\Service\Utils\StringsUtils;
- use PHPUnit\Framework\TestCase;
- class StringsUtilsTest extends TestCase
- {
- /**
- * @see StringsUtils::unquote()
- */
- public function testUnquote(): void
- {
- $this->assertEquals('foo', StringsUtils::unquote("'foo"));
- }
- /**
- * @see StringsUtils::camelToSnake()
- */
- public function testCamelToSnake(): void
- {
- $this->assertEquals('foo_bar', StringsUtils::camelToSnake('FooBar'));
- $this->assertEquals('foo-bar', StringsUtils::camelToSnake('FooBar', '-'));
- $this->assertEquals('foo_bar', StringsUtils::camelToSnake('fooBar'));
- }
- /**
- * @see StringsUtils::convertHtmlToText()
- */
- public function testConvertHtmlToText(): void
- {
- $this->assertEquals('Test contenu', (new StringsUtils())->convertHtmlToText('<table><tr><td>Test</td></tr></table> <br /><p>contenu</p>'));
- }
- /**
- * @see StringsUtils::elide()
- */
- public function testElide(): void
- {
- // Test normal truncation case - string longer than length
- $this->assertEquals('Hello...', StringsUtils::elide('Hello World!', 8));
- // Test case where string is shorter than length - no truncation
- $this->assertEquals('Hello', StringsUtils::elide('Hello', 10));
- // Test edge case with exact length
- $this->assertEquals('Hello', StringsUtils::elide('Hello', 6));
- // Test with empty string
- $this->assertEquals('', StringsUtils::elide('', 10));
- }
- /**
- * @see StringsUtils::elide()
- */
- public function testElideWithMultibyteCharacters(): void
- {
- // Test with UTF-8 multibyte characters
- $this->assertEquals('Héllo...', StringsUtils::elide('Héllo Wörld!', 8));
- $this->assertEquals('测试中...', StringsUtils::elide('测试中文字符串', 6));
- $this->assertEquals('🎉🎊🎈🎆🎇', StringsUtils::elide('🎉🎊🎈🎆🎇', 6));
- }
- /**
- * @see StringsUtils::elide()
- */
- public function testElideWithInvalidLength(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Length must be greater than 5');
- StringsUtils::elide('Hello World!', 1);
- }
- /**
- * @see StringsUtils::elide()
- */
- public function testElideWithLongString(): void
- {
- $longString = str_repeat('A', 1000);
- $result = StringsUtils::elide($longString, 50);
- $this->assertEquals(50, mb_strlen($result)); // 47 + 3 dots
- $this->assertTrue(str_ends_with($result, '...'));
- $this->assertEquals(str_repeat('A', 47).'...', $result);
- }
- }
|