104 lines
3.5 KiB
PHP
104 lines
3.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace Tests\Unit\Shared\Helpers;
|
||
|
|
|
||
|
|
use Shared\Helpers\StringHelper;
|
||
|
|
|
||
|
|
covers(StringHelper::class);
|
||
|
|
|
||
|
|
it('transliterates French accented characters to ASCII', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$input = 'Été crème brûlée';
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$result = StringHelper::transliterate($input);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($result)->toBe('Ete creme brulee');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('slugifies strings with accents, punctuation, and custom separators', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$input = ' Été : crème brûlée & café! ';
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$default = StringHelper::slugify($input);
|
||
|
|
$custom = StringHelper::slugify($input, '_');
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($default)->toBe('ete-creme-brulee-cafe')
|
||
|
|
->and($custom)->toBe('ete_creme_brulee_cafe');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('converts strings to snakeCase, kebabCase, pascalCase, and camelCase', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$cases = [
|
||
|
|
'helloWorld' => ['hello_world', 'hello-world', 'HelloWorld', 'helloWorld'],
|
||
|
|
'HTTPResponseCode' => ['http_response_code', 'http-response-code', 'HttpResponseCode', 'httpResponseCode'],
|
||
|
|
'hello-world foo' => ['hello_world_foo', 'hello-world-foo', 'HelloWorldFoo', 'helloWorldFoo'],
|
||
|
|
'' => ['', '', '', ''],
|
||
|
|
];
|
||
|
|
|
||
|
|
// WHEN / THEN
|
||
|
|
foreach ($cases as $input => [$snake, $kebab, $pascal, $camel]) {
|
||
|
|
expect(StringHelper::snakeCase($input))->toBe($snake)
|
||
|
|
->and(StringHelper::kebabCase($input))->toBe($kebab)
|
||
|
|
->and(StringHelper::pascalCase($input))->toBe($pascal)
|
||
|
|
->and(StringHelper::camelCase($input))->toBe($camel);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
it('converts strings to titleCase', function (): void {
|
||
|
|
// GIVEN / WHEN / THEN
|
||
|
|
expect(StringHelper::titleCase(''))->toBe('')
|
||
|
|
->and(StringHelper::titleCase(' '))->toBe('')
|
||
|
|
->and(StringHelper::titleCase('jean-luc PICARD'))->toBe('Jean-Luc Picard');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('truncates words with default or custom suffix and handles multibyte accents', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$short = 'Un deux trois';
|
||
|
|
$long = 'Été crème brûlée café théâtre';
|
||
|
|
|
||
|
|
// WHEN / THEN
|
||
|
|
expect(StringHelper::words($short, 5))->toBe('Un deux trois')
|
||
|
|
->and(StringHelper::words($long, 3))->toBe('Été crème brûlée ...')
|
||
|
|
->and(StringHelper::words($long, 3, '…'))->toBe('Été crème brûlée…');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('tokenizes strings with multibyte delimiters using mbStrtok', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$delimiters = 'é,; ';
|
||
|
|
$input = 'unédeux,trois; quatre';
|
||
|
|
|
||
|
|
// WHEN
|
||
|
|
$t1 = StringHelper::mbStrtok($delimiters, $input);
|
||
|
|
$t2 = StringHelper::mbStrtok($delimiters);
|
||
|
|
$t3 = StringHelper::mbStrtok($delimiters);
|
||
|
|
$t4 = StringHelper::mbStrtok($delimiters);
|
||
|
|
$t5 = StringHelper::mbStrtok($delimiters);
|
||
|
|
|
||
|
|
// THEN
|
||
|
|
expect($t1)->toBe('un')
|
||
|
|
->and($t2)->toBe('deux')
|
||
|
|
->and($t3)->toBe('trois')
|
||
|
|
->and($t4)->toBe('quatre')
|
||
|
|
->and($t5)->toBeFalse();
|
||
|
|
});
|
||
|
|
|
||
|
|
it('sanitizes null, empty, HTML, newlines, control bytes, 4-byte emoji, and Windows-1252 bytes', function (): void {
|
||
|
|
// GIVEN
|
||
|
|
$html = "<p>Bonjour & \t\r\n<b>monde</b>\r!</p>";
|
||
|
|
$withEmoji = "Test😀\x00\x7F\xC2\xA0\xC2\xADok";
|
||
|
|
$win1252 = "\x80\xE9";
|
||
|
|
|
||
|
|
// WHEN / THEN
|
||
|
|
expect(StringHelper::sanitize(null))->toBeNull()
|
||
|
|
->and(StringHelper::sanitize(''))->toBe('')
|
||
|
|
->and(StringHelper::sanitize($html))->toBe("Bonjour & \t\nmonde\n!")
|
||
|
|
->and(StringHelper::sanitize($withEmoji))->toBe('Testok')
|
||
|
|
->and(StringHelper::sanitize($win1252))->toBe('ێ');
|
||
|
|
});
|