v1.0 Initial commit of project

This commit is contained in:
2026-01-01 10:54:18 +01:00
commit 768cf78b57
990 changed files with 241213 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,747 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests;
use Symfony\Component\String\Exception\InvalidArgumentException;
abstract class AbstractUnicodeTestCase extends AbstractAsciiTestCase
{
public static function provideWidth(): array
{
return array_merge(
parent::provideWidth(),
[
[14, '<<<END
This is a
multiline text
END'],
]
);
}
public function testCreateFromStringWithInvalidUtf8Input()
{
$this->expectException(InvalidArgumentException::class);
static::createFromString("\xE9");
}
public function testAscii()
{
$s = static::createFromString('Dieser Wert sollte größer oder gleich');
$this->assertSame('Dieser Wert sollte grosser oder gleich', (string) $s->ascii());
$this->assertSame('Dieser Wert sollte groesser oder gleich', (string) $s->ascii(['de-ASCII']));
}
public function testAsciiClosureRule()
{
$rule = fn ($c) => str_replace('ö', 'OE', $c);
$s = static::createFromString('Dieser Wert sollte größer oder gleich');
$this->assertSame('Dieser Wert sollte grOEsser oder gleich', (string) $s->ascii([$rule]));
}
/**
* @dataProvider provideLocaleLower
*
* @requires extension intl
*/
public function testLocaleLower(string $locale, string $expected, string $origin)
{
$instance = static::createFromString($origin)->localeLower($locale);
$this->assertNotSame(static::createFromString($origin), $instance);
$this->assertEquals(static::createFromString($expected), $instance);
$this->assertSame($expected, (string) $instance);
}
/**
* @dataProvider provideLocaleUpper
*
* @requires extension intl
*/
public function testLocaleUpper(string $locale, string $expected, string $origin)
{
$instance = static::createFromString($origin)->localeUpper($locale);
$this->assertNotSame(static::createFromString($origin), $instance);
$this->assertEquals(static::createFromString($expected), $instance);
$this->assertSame($expected, (string) $instance);
}
/**
* @dataProvider provideLocaleTitle
*
* @requires extension intl
*/
public function testLocaleTitle(string $locale, string $expected, string $origin)
{
$instance = static::createFromString($origin)->localeTitle($locale);
$this->assertNotSame(static::createFromString($origin), $instance);
$this->assertEquals(static::createFromString($expected), $instance);
$this->assertSame($expected, (string) $instance);
}
public static function provideCreateFromCodePoint(): array
{
return [
['', []],
['*', [42]],
['AZ', [65, 90]],
['€', [8364]],
['€', [0x20AC]],
['Ʃ', [425]],
['Ʃ', [0x1A9]],
['☢☎❄', [0x2622, 0x260E, 0x2744]],
];
}
public static function provideBytesAt(): array
{
return array_merge(
parent::provideBytesAt(),
[
[[0xC3, 0xA4], 'Späßchen', 2],
[[0xC3, 0x9F], 'Späßchen', -5],
]
);
}
/**
* @dataProvider provideCodePointsAt
*/
public function testCodePointsAt(array $expected, string $string, int $offset, ?int $form = null)
{
if (2 !== grapheme_strlen('च्छे') && 'नमस्ते' === $string) {
$this->markTestSkipped('Skipping due to issue ICU-21661.');
}
$instance = static::createFromString($string);
$instance = $form ? $instance->normalize($form) : $instance;
$this->assertSame($expected, $instance->codePointsAt($offset));
}
public static function provideCodePointsAt(): array
{
$data = [
[[], '', 0],
[[], 'a', 1],
[[0x53], 'Späßchen', 0],
[[0xE4], 'Späßchen', 2],
[[0xDF], 'Späßchen', -5],
];
// Skip this set if we encounter an issue in PCRE2
// @see https://github.com/PCRE2Project/pcre2/issues/361
if (3 === grapheme_strlen('☢☎❄')) {
$data[] = [[0x260E], '☢☎❄', 1];
}
return $data;
}
public static function provideLength(): array
{
return [
[1, 'a'],
[1, 'ß'],
[2, 'is'],
[3, 'PHP'],
[3, '한국어'],
[4, 'Java'],
[7, 'Symfony'],
[10, 'pineapples'],
[22, 'Symfony is super cool!'],
];
}
public static function provideIndexOf(): array
{
return array_merge(
parent::provideIndexOf(),
[
[1, '한국어', '국', 0],
[1, '한국어', '국', 1],
[null, '한국어', '국', 2],
[8, 'der Straße nach Paris', 'ß', 4],
]
);
}
public static function provideIndexOfIgnoreCase(): array
{
return array_merge(
parent::provideIndexOfIgnoreCase(),
[
[3, 'DÉJÀ', 'À', 0],
[3, 'DÉJÀ', 'à', 0],
[1, 'DÉJÀ', 'É', 1],
[1, 'DÉJÀ', 'é', 1],
[1, 'aςσb', 'ΣΣ', 0],
[16, 'der Straße nach Paris', 'Paris', 0],
[8, 'der Straße nach Paris', 'ß', 4],
]
);
}
public static function provideIndexOfLast(): array
{
return array_merge(
parent::provideIndexOfLast(),
[
[null, '한국어', '', 0],
[1, '한국어', '국', 0],
[5, '한국어어어어국국', '어', 0],
// see https://bugs.php.net/bug.php?id=74264
[15, 'abcdéf12é45abcdéf', 'é', 0],
[8, 'abcdéf12é45abcdéf', 'é', -4],
]
);
}
public static function provideIndexOfLastIgnoreCase(): array
{
return array_merge(
parent::provideIndexOfLastIgnoreCase(),
[
[null, '한국어', '', 0],
[3, 'DÉJÀ', 'à', 0],
[3, 'DÉJÀ', 'À', 0],
[6, 'DÉJÀÀÀÀ', 'à', 0],
[6, 'DÉJÀÀÀÀ', 'à', 3],
[5, 'DÉJÀÀÀÀ', 'àà', 0],
[2, 'DÉJÀÀÀÀ', 'jà', 0],
[2, 'DÉJÀÀÀÀ', 'jà', -5],
[6, 'DÉJÀÀÀÀ!', 'à', -2],
// see https://bugs.php.net/bug.php?id=74264
[5, 'DÉJÀÀÀÀ', 'à', -2],
[15, 'abcdéf12é45abcdéf', 'é', 0],
[8, 'abcdéf12é45abcdéf', 'é', -4],
[1, 'aςσb', 'ΣΣ', 0],
]
);
}
public static function provideSplit(): array
{
return array_merge(
parent::provideSplit(),
[
[
'會|意|文|字|/|会|意|文|字',
'|',
[
static::createFromString('會'),
static::createFromString('意'),
static::createFromString('文'),
static::createFromString('字'),
static::createFromString('/'),
static::createFromString('会'),
static::createFromString('意'),
static::createFromString('文'),
static::createFromString('字'),
],
null,
],
[
'會|意|文|字|/|会|意|文|字',
'|',
[
static::createFromString('會'),
static::createFromString('意'),
static::createFromString('文'),
static::createFromString('字'),
static::createFromString('/|会|意|文|字'),
],
5,
],
]
);
}
public static function provideChunk(): array
{
return array_merge(
parent::provideChunk(),
[
[
'déjà',
[
static::createFromString('d'),
static::createFromString('é'),
static::createFromString('j'),
static::createFromString('à'),
],
1,
],
[
'déjà',
[
static::createFromString('dé'),
static::createFromString('jà'),
],
2,
],
]
);
}
public function testTrimWithInvalidUtf8CharList()
{
$this->expectException(InvalidArgumentException::class);
static::createFromString('Symfony')->trim("\xE9");
}
public function testTrimStartWithInvalidUtf8CharList()
{
$this->expectException(InvalidArgumentException::class);
static::createFromString('Symfony')->trimStart("\xE9");
}
public function testTrimEndWithInvalidUtf8CharList()
{
$this->expectException(InvalidArgumentException::class);
static::createFromString('Symfony')->trimEnd("\xE9");
}
public static function provideLower(): array
{
return array_merge(
parent::provideLower(),
[
// French
['garçon', 'garçon'],
['garçon', 'GARÇON'],
["œuvre d'art", "Œuvre d'Art"],
// Spanish
['el niño', 'El Niño'],
// Romanian
['împărat', 'Împărat'],
// Random symbols
['déjà σσς i̇iıi', 'DÉJÀ Σσς İIıi'],
]
);
}
public static function provideLocaleLower(): array
{
return [
// Lithuanian
// Introduce an explicit dot above when lowercasing capital I's and J's
// whenever there are more accents above.
// LATIN CAPITAL LETTER I WITH OGONEK -> LATIN SMALL LETTER I WITH OGONEK
['lt', 'į', 'Į'],
// LATIN CAPITAL LETTER I WITH GRAVE -> LATIN SMALL LETTER I COMBINING DOT ABOVE
['lt', 'i̇̀', 'Ì'],
// LATIN CAPITAL LETTER I WITH ACUTE -> LATIN SMALL LETTER I COMBINING DOT ABOVE COMBINING ACUTE ACCENT
['lt', 'i̇́', 'Í'],
// LATIN CAPITAL LETTER I WITH TILDE -> LATIN SMALL LETTER I COMBINING DOT ABOVE COMBINING TILDE
['lt', 'i̇̃', 'Ĩ'],
// Turkish and Azeri
// When lowercasing, remove dot_above in the sequence I + dot_above, which will turn into 'i'.
// LATIN CAPITAL LETTER I WITH DOT ABOVE -> LATIN SMALL LETTER I
['tr', 'i', 'İ'],
['tr_TR', 'i', 'İ'],
['az', 'i', 'İ'],
// Default casing rules
// LATIN CAPITAL LETTER I WITH DOT ABOVE -> LATIN SMALL LETTER I COMBINING DOT ABOVE
['en_US', 'i̇', 'İ'],
['en', 'i̇', 'İ'],
];
}
public static function provideLocaleUpper(): array
{
return [
// Turkish and Azeri
// When uppercasing, i turns into a dotted capital I
// LATIN SMALL LETTER I -> LATIN CAPITAL LETTER I WITH DOT ABOVE
['tr', 'İ', 'i'],
['tr_TR', 'İ', 'i'],
['az', 'İ', 'i'],
// Greek
// Remove accents when uppercasing
// GREEK SMALL LETTER ALPHA WITH TONOS -> GREEK CAPITAL LETTER ALPHA
['el', 'Α', 'ά'],
['el_GR', 'Α', 'ά'],
// Default casing rules
// GREEK SMALL LETTER ALPHA WITH TONOS -> GREEK CAPITAL LETTER ALPHA WITH TONOS
['en_US', 'Ά', 'ά'],
['en', 'Ά', 'ά'],
];
}
public static function provideLocaleTitle(): array
{
return [
// Greek
// Titlecasing words, should keep the accents on the first letter
['el', 'Άδικος', 'άδικος'],
['el_GR', 'Άδικος', 'άδικος'],
['en', 'Άδικος', 'άδικος'],
// Dutch
// Title casing should treat 'ij' as one character
['nl_NL', 'IJssel', 'ijssel'],
['nl_BE', 'IJssel', 'ijssel'],
['nl', 'IJssel', 'ijssel'],
// Default casing rules
['en', 'Ijssel', 'ijssel'],
];
}
public static function provideUpper(): array
{
return array_merge(
parent::provideUpper(),
[
// French
['GARÇON', 'garçon'],
['GARÇON', 'GARÇON'],
["ŒUVRE D'ART", "Œuvre d'Art"],
// German
['ÄUSSERST', 'äußerst'],
// Spanish
['EL NIÑO', 'El Niño'],
// Romanian
['ÎMPĂRAT', 'Împărat'],
// Random symbols
['DÉJÀ ΣΣΣ İIII', 'Déjà Σσς İIıi'],
]
);
}
public static function provideTitle(): array
{
return array_merge(
parent::provideTitle(),
[
['Deja', 'deja', false],
['Σσς', 'σσς', false],
['DEJa', 'dEJa', false],
['ΣσΣ', 'σσΣ', false],
['Deja Σσς DEJa ΣσΣ', 'deja σσς dEJa σσΣ', true],
// Spanish
['Última prueba', 'última prueba', false],
['ÚLTIMA pRUEBA', 'úLTIMA pRUEBA', false],
['¡Hola spain!', '¡hola spain!', false],
['¡HOLA sPAIN!', '¡hOLA sPAIN!', false],
['¡Hola Spain!', '¡hola spain!', true],
['¡HOLA SPAIN!', '¡hOLA sPAIN!', true],
['Última Prueba', 'última prueba', true],
['ÚLTIMA PRUEBA', 'úLTIMA pRUEBA', true],
]
);
}
public static function provideSlice(): array
{
return array_merge(
parent::provideSlice(),
[
['jà', 'déjà', 2, null],
['jà', 'déjà', 2, null],
['jà', 'déjà', -2, null],
['jà', 'déjà', -2, 3],
['', 'déjà', -1, 0],
['', 'déjà', 1, -4],
['j', 'déjà', -2, -1],
['', 'déjà', -2, -2],
['', 'déjà', 5, 0],
['', 'déjà', -5, 0],
]
);
}
public static function provideAppend(): array
{
return array_merge(
parent::provideAppend(),
[
[
'Déjà Σσς',
['Déjà', ' ', 'Σσς'],
],
[
'Déjà Σσς İIıi',
['Déjà', ' Σσς', ' İIıi'],
],
]
);
}
public function testAppendInvalidUtf8String()
{
$this->expectException(InvalidArgumentException::class);
static::createFromString('Symfony')->append("\xE9");
}
public static function providePrepend(): array
{
return array_merge(
parent::providePrepend(),
[
[
'Σσς Déjà',
['Déjà', 'Σσς '],
],
[
'İIıi Σσς Déjà',
['Déjà', 'Σσς ', 'İIıi '],
],
]
);
}
public function testPrependInvalidUtf8String()
{
$this->expectException(InvalidArgumentException::class);
static::createFromString('Symfony')->prepend("\xE9");
}
public static function provideBeforeAfter(): array
{
return array_merge(
parent::provideBeforeAfter(),
[
['jàdéjà', 'jà', 'déjàdéjà', 0, false],
['dé', 'jà', 'déjàdéjà', 0, true],
]
);
}
public static function provideBeforeAfterIgnoreCase(): array
{
return array_merge(
parent::provideBeforeAfterIgnoreCase(),
[
['jàdéjà', 'JÀ', 'déjàdéjà', 0, false],
['dé', 'jÀ', 'déjàdéjà', 0, true],
['éjàdéjà', 'é', 'déjàdéjà', 0, false],
['d', 'é', 'déjàdéjà', 0, true],
['déjàdéjà', 'Ç', 'déjàdéjà', 0, false],
['déjàdéjà', 'Ç', 'déjàdéjà', 0, true],
]
);
}
public static function provideBeforeAfterLast(): array
{
return array_merge(
parent::provideBeforeAfterLast(),
[
['déjàdéjà', 'Ç', 'déjàdéjà', 0, false],
['déjàdéjà', 'Ç', 'déjàdéjà', 0, true],
['éjà', 'é', 'déjàdéjà', 0, false],
['déjàd', 'é', 'déjàdéjà', 0, true],
]
);
}
public static function provideBeforeAfterLastIgnoreCase(): array
{
return array_merge(
parent::provideBeforeAfterLastIgnoreCase(),
[
['déjàdéjà', 'Ç', 'déjàdéjà', 0, false],
['éjà', 'é', 'déjàdéjà', 0, false],
['éjà', 'É', 'déjàdéjà', 0, false],
]
);
}
public static function provideFolded(): array
{
return array_merge(
parent::provideFolded(),
[
['déjà', 'DéjÀ'],
['σσσ', 'Σσς'],
['iıi̇i', 'Iıİi'],
]
);
}
public static function provideReplace(): array
{
return array_merge(
parent::provideReplace(),
[
['ΣσΣ', 1, 'Σσς', 'ς', 'Σ'],
['漢字はユニコード', 0, '漢字はユニコード', 'foo', 'bar'],
['漢字ーユニコード', 1, '漢字はユニコード', 'は', 'ー'],
['This is a jamais-vu situation!', 1, 'This is a déjà-vu situation!', 'déjà', 'jamais'],
]
);
}
public static function provideReplaceMatches(): array
{
return array_merge(
parent::provideReplaceMatches(),
[
['This is a dj-vu situation!', 'This is a déjà-vu situation!', '/([à-ú])/', ''],
]
);
}
public static function provideReplaceIgnoreCase(): array
{
return array_merge(
parent::provideReplaceIgnoreCase(),
[
// σ and ς are lowercase variants for Σ
['ΣΣΣ', 3, 'σσσ', 'σ', 'Σ'],
['ΣΣΣ', 3, 'σσσ', 'ς', 'Σ'],
['Σσ', 1, 'σσσ', 'σσ', 'Σ'],
['漢字はユニコード', 0, '漢字はユニコード', 'foo', 'bar'],
['漢字ーユニコード', 1, '漢字はユニコード', 'は', 'ー'],
['This is a jamais-vu situation!', 1, 'This is a déjà-vu situation!', 'DÉjÀ', 'jamais'],
]
);
}
public function testReplaceWithInvalidUtf8Pattern()
{
$this->assertEquals('Symfony', static::createFromString('Symfony')->replace("\xE9", 'p'));
}
public function testReplaceWithInvalidUtf8PatternReplacement()
{
$this->expectException(InvalidArgumentException::class);
static::createFromString('Symfony')->replace('f', "\xE9");
}
public static function provideCamel()
{
return array_merge(
parent::provideCamel(),
[
['symfonyIstÄußerstCool', 'symfony_ist_äußerst_cool'],
]
);
}
public static function provideSnake()
{
return array_merge(
parent::provideSnake(),
[
['symfony_ist_äußerst_cool', 'symfonyIstÄußerstCool'],
]
);
}
public static function provideKebab(): array
{
return [
...parent::provideKebab(),
['symfony-ist-äußerst-cool', 'symfonyIstÄußerstCool'],
['symfony-with-emojis', 'Symfony with 😃 emojis'],
];
}
public static function provideEqualsTo()
{
return array_merge(
parent::provideEqualsTo(),
[
[true, 'äußerst', 'äußerst'],
[false, 'BÄR', 'bär'],
[false, 'Bär', 'Bar'],
]
);
}
public static function provideEqualsToIgnoreCase()
{
return array_merge(
parent::provideEqualsToIgnoreCase(),
[
[true, 'Äußerst', 'äußerst'],
[false, 'Bär', 'Bar'],
]
);
}
public static function providePadBoth(): array
{
return array_merge(
parent::providePadBoth(),
[
['äußerst', 'äußerst', 7, '+'],
['+äußerst+', 'äußerst', 9, '+'],
['äö.äöä', '.', 6, 'äö'],
]
);
}
public static function providePadEnd(): array
{
return array_merge(
parent::providePadEnd(),
[
['äußerst', 'äußerst', 7, '+'],
['äußerst+', 'äußerst', 8, '+'],
['.äöä', '.', 4, 'äö'],
]
);
}
public static function providePadStart(): array
{
return array_merge(
parent::providePadStart(),
[
['äußerst', 'äußerst', 7, '+'],
['+äußerst', 'äußerst', 8, '+'],
['äöä.', '.', 4, 'äö'],
]
);
}
public static function provideReverse()
{
return array_merge(
parent::provideReverse(),
[
['äuß⭐erst', 'tsre⭐ßuä'],
['漢字ーユニコードéèΣσς', 'ςσΣèéドーコニユー字漢'],
['नमस्ते', 'तेस्मन'],
]
);
}
}

View File

@@ -0,0 +1,102 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests;
use Symfony\Component\String\AbstractString;
use Symfony\Component\String\ByteString;
use Symfony\Component\String\Exception\InvalidArgumentException;
class ByteStringTest extends AbstractAsciiTestCase
{
protected static function createFromString(string $string): AbstractString
{
return new ByteString($string);
}
public function testFromRandom()
{
$random = ByteString::fromRandom(32);
self::assertSame(32, $random->length());
foreach ($random->chunk() as $char) {
self::assertNotNull((new ByteString('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'))->indexOf($char));
}
}
public function testFromRandomWithSpecificChars()
{
$random = ByteString::fromRandom(32, 'abc');
self::assertSame(32, $random->length());
foreach ($random->chunk() as $char) {
self::assertNotNull((new ByteString('abc'))->indexOf($char));
}
}
public function testFromRandoWithZeroLength()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('A strictly positive length is expected, "0" given.');
self::assertSame('', ByteString::fromRandom(0));
}
public function testFromRandomThrowsForNegativeLength()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('A strictly positive length is expected, "-1" given.');
ByteString::fromRandom(-1);
}
public function testFromRandomAlphabetMin()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The length of the alphabet must in the [2^1, 2^56] range.');
ByteString::fromRandom(32, 'a');
}
public static function provideBytesAt(): array
{
return array_merge(
parent::provideBytesAt(),
[
[[0xC3], 'Späßchen', 2],
[[0x61], "Spa\u{0308}ßchen", 2],
[[0xCC], "Spa\u{0308}ßchen", 3],
[[0xE0], 'नमस्ते', 6],
]
);
}
public static function provideLength(): array
{
return array_merge(
parent::provideLength(),
[
[2, 'ä'],
]
);
}
public static function provideWidth(): array
{
return array_merge(
parent::provideWidth(),
[
[10, "f\u{001b}[0moo\x80bar\xfe\xfe1"], // foo?bar??1
[13, "f\u{001b}[0moo\x80bar\xfe\xfe1", false], // f[0moo?bar??1
]
);
}
}

View File

@@ -0,0 +1,58 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests;
use Symfony\Component\String\AbstractString;
use Symfony\Component\String\CodePointString;
class CodePointStringTest extends AbstractUnicodeTestCase
{
protected static function createFromString(string $string): AbstractString
{
return new CodePointString($string);
}
public static function provideLength(): array
{
return array_merge(
parent::provideLength(),
[
// 8 instead of 5 if it were processed as a grapheme cluster
[8, 'अनुच्छेद'],
]
);
}
public static function provideBytesAt(): array
{
return array_merge(
parent::provideBytesAt(),
[
[[0x61], "Spa\u{0308}ßchen", 2],
[[0xCC, 0x88], "Spa\u{0308}ßchen", 3],
[[0xE0, 0xA5, 0x8D], 'नमस्ते', 3],
]
);
}
public static function provideCodePointsAt(): array
{
return array_merge(
parent::provideCodePointsAt(),
[
[[0x61], "Spa\u{0308}ßchen", 2],
[[0x0308], "Spa\u{0308}ßchen", 3],
[[0x094D], 'नमस्ते', 3],
]
);
}
}

View File

@@ -0,0 +1,80 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\String\AbstractString;
use Symfony\Component\String\ByteString;
use Symfony\Component\String\UnicodeString;
use function Symfony\Component\String\b;
use function Symfony\Component\String\s;
use function Symfony\Component\String\u;
final class FunctionsTest extends TestCase
{
/**
* @dataProvider provideSStrings
*/
public function testS(AbstractString $expected, ?string $input)
{
$this->assertEquals($expected, s($input));
}
public static function provideSStrings(): array
{
return [
[new UnicodeString(''), ''],
[new UnicodeString(''), null],
[new UnicodeString('foo'), 'foo'],
[new UnicodeString('अनुच्छेद'), 'अनुच्छेद'],
[new ByteString("b\x80ar"), "b\x80ar"],
[new ByteString("\xfe\xff"), "\xfe\xff"],
];
}
/**
* @dataProvider provideUStrings
*/
public function testU(UnicodeString $expected, ?string $input)
{
$this->assertEquals($expected, u($input));
}
public static function provideUStrings(): array
{
return [
[new UnicodeString(''), ''],
[new UnicodeString(''), null],
[new UnicodeString('foo'), 'foo'],
[new UnicodeString('अनुच्छेद'), 'अनुच्छेद'],
];
}
/**
* @dataProvider provideBStrings
*/
public function testB(ByteString $expected, ?string $input)
{
$this->assertEquals($expected, b($input));
}
public static function provideBStrings(): array
{
return [
[new ByteString(''), ''],
[new ByteString(''), null],
[new ByteString("b\x80ar"), "b\x80ar"],
[new ByteString("\xfe\xff"), "\xfe\xff"],
];
}
}

View File

@@ -0,0 +1,363 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests\Inflector;
use PHPUnit\Framework\TestCase;
use Symfony\Component\String\Inflector\EnglishInflector;
class EnglishInflectorTest extends TestCase
{
public static function singularizeProvider()
{
// see http://english-zone.com/spelling/plurals.html
// see http://www.scribd.com/doc/3271143/List-of-100-Irregular-Plural-Nouns-in-English
return [
['accesses', 'access'],
['addresses', 'address'],
['agendas', 'agenda'],
['albums', 'album'],
['alumnae', 'alumna'],
['alumni', 'alumnus'],
['analyses', ['analys', 'analyse', 'analysis']],
['ankles', 'ankle'],
['antennae', 'antenna'],
['antennas', 'antenna'],
['appendices', ['appendex', 'appendix', 'appendice']],
['arches', ['arch', 'arche']],
['articles', 'article'],
['atlases', ['atlas', 'atlase', 'atlasis']],
['axes', ['ax', 'axe', 'axis']],
['babies', 'baby'],
['bacteria', 'bacterium'],
['bases', ['bas', 'base', 'basis']],
['batches', ['batch', 'batche']],
['beaux', 'beau'],
['bees', 'bee'],
['boxes', 'box'],
['boys', 'boy'],
['bureaus', 'bureau'],
['bureaux', 'bureau'],
['buses', ['bus', 'buse', 'busis']],
['bushes', ['bush', 'bushe']],
['buttons', 'button'],
['calves', ['calf', 'calve', 'calff']],
['cars', 'car'],
['cassettes', ['cassett', 'cassette']],
['caves', ['caf', 'cave', 'caff']],
['chateaux', 'chateau'],
['cheeses', ['chees', 'cheese', 'cheesis']],
['children', 'child'],
['circuses', ['circus', 'circuse', 'circusis']],
['cliffs', 'cliff'],
['codes', 'code'],
['committee', 'committee'],
['corpora', 'corpus'],
['coupons', 'coupon'],
['crises', ['cris', 'crise', 'crisis']],
['criteria', 'criterion'],
['cups', 'cup'],
['curricula', 'curriculum'],
['data', 'data'],
['days', 'day'],
['discos', 'disco'],
['devices', ['devex', 'devix', 'device']],
['drives', 'drive'],
['drivers', 'driver'],
['dwarves', ['dwarf', 'dwarve', 'dwarff']],
['echoes', ['echo', 'echoe']],
['edges', 'edge'],
['elves', ['elf', 'elve', 'elff']],
['emphases', ['emphas', 'emphase', 'emphasis']],
['employees', 'employee'],
['faxes', 'fax'],
['fees', 'fee'],
['feet', 'foot'],
['feedback', 'feedback'],
['foci', 'focus'],
['focuses', ['focus', 'focuse', 'focusis']],
['formulae', 'formula'],
['formulas', 'formula'],
['conspectuses', 'conspectus'],
['fungi', 'fungus'],
['funguses', ['fungus', 'funguse', 'fungusis']],
['garages', ['garag', 'garage']],
['geese', 'goose'],
['genera', 'genus'],
['halves', ['half', 'halve', 'halff']],
['hats', 'hat'],
['heroes', ['hero', 'heroe']],
['hippopotamuses', ['hippopotamus', 'hippopotamuse', 'hippopotamusis']], // hippopotami
['hoaxes', 'hoax'],
['hooves', ['hoof', 'hoove', 'hooff']],
['houses', ['hous', 'house', 'housis']],
['indexes', 'index'],
['indices', ['index', 'indix', 'indice']],
['ions', 'ion'],
['irises', ['iris', 'irise', 'irisis']],
['kisses', 'kiss'],
['knives', 'knife'],
['lamps', 'lamp'],
['lessons', 'lesson'],
['leaves', ['leaf', 'leave', 'leaff']],
['lice', 'louse'],
['lives', 'life'],
['matrices', ['matrex', 'matrix', 'matrice']],
['matrixes', 'matrix'],
['media', 'medium'],
['memoranda', 'memorandum'],
['men', 'man'],
['mice', 'mouse'],
['moves', 'move'],
['movies', 'movie'],
['names', 'name'],
['nebulae', 'nebula'],
['neuroses', ['neuros', 'neurose', 'neurosis']],
['news', 'news'],
['oases', ['oas', 'oase', 'oasis']],
['objectives', 'objective'],
['oxen', 'ox'],
['parties', 'party'],
['people', 'person'],
['persons', 'person'],
['phenomena', 'phenomenon'],
['photos', 'photo'],
['pianos', 'piano'],
['plateaux', 'plateau'],
['poisons', 'poison'],
['poppies', 'poppy'],
['prices', ['prex', 'prix', 'price']],
['quizzes', 'quiz'],
['quora', 'quorum'],
['quorums', 'quorum'],
['radii', 'radius'],
['roofs', 'roof'],
['roses', ['ros', 'rose', 'rosis']],
['sandwiches', ['sandwich', 'sandwiche']],
['scarves', ['scarf', 'scarve', 'scarff']],
['schemas', 'schema'], // schemata
['seasons', 'season'],
['selfies', 'selfie'],
['series', 'series'],
['services', 'service'],
['sheriffs', 'sheriff'],
['shoes', ['sho', 'shoe']],
['species', 'species'],
['spies', 'spy'],
['staves', ['staf', 'stave', 'staff']],
['status', 'status'],
['statuses', 'status'],
['stories', 'story'],
['strata', 'stratum'],
['suitcases', ['suitcas', 'suitcase', 'suitcasis']],
['syllabi', 'syllabus'],
['tags', 'tag'],
['teeth', 'tooth'],
['theses', ['thes', 'these', 'thesis']],
['thieves', ['thief', 'thieve', 'thieff']],
['treasons', 'treason'],
['trees', 'tree'],
['waltzes', ['waltz', 'waltze']],
['wives', 'wife'],
['zombies', 'zombie'],
// test casing: if the first letter was uppercase, it should remain so
['Men', 'Man'],
['GrandChildren', 'GrandChild'],
['SubTrees', 'SubTree'],
// Known issues
// ['insignia', 'insigne'],
// ['insignias', 'insigne'],
// ['rattles', 'rattle'],
];
}
public static function pluralizeProvider()
{
// see http://english-zone.com/spelling/plurals.html
// see http://www.scribd.com/doc/3271143/List-of-100-Irregular-Plural-Nouns-in-English
return [
['access', 'accesses'],
['address', 'addresses'],
['agenda', 'agendas'],
['aircraft', 'aircraft'],
['album', 'albums'],
['alumnus', 'alumni'],
['analysis', 'analyses'],
['ankle', 'ankles'],
['antenna', 'antennas'], // antennae
['appendix', ['appendices', 'appendixes']],
['arch', 'arches'],
['article', 'articles'],
['atlas', 'atlases'],
['axe', 'axes'],
['axis', 'axes'],
['baby', 'babies'],
['bacterium', 'bacteria'],
['base', 'bases'],
['batch', 'batches'],
['beau', ['beaus', 'beaux']],
['bee', 'bees'],
['box', 'boxes'],
['boy', 'boys'],
['bureau', ['bureaus', 'bureaux']],
['bus', 'buses'],
['bush', 'bushes'],
['button', 'buttons'],
['calf', ['calfs', 'calves']],
['campus', 'campuses'],
['car', 'cars'],
['cassette', 'cassettes'],
['cave', 'caves'],
['chateau', ['chateaus', 'chateaux']],
['cheese', 'cheeses'],
['child', 'children'],
['circus', 'circuses'],
['cliff', 'cliffs'],
['committee', 'committees'],
['coupon', 'coupons'],
['crisis', 'crises'],
['criterion', 'criteria'],
['cup', 'cups'],
['curriculum', 'curricula'],
['data', 'data'],
['day', 'days'],
['disco', 'discos'],
['device', 'devices'],
['drive', 'drives'],
['driver', 'drivers'],
['dwarf', ['dwarfs', 'dwarves']],
['echo', 'echoes'],
['edge', 'edges'],
['elf', ['elfs', 'elves']],
['emphasis', 'emphases'],
['fax', ['faxes', 'faxxes']],
['feedback', 'feedback'],
['focus', 'focuses'],
['foot', 'feet'],
['formula', 'formulas'], // formulae
['conspectus', 'conspectuses'],
['fungus', 'fungi'],
['garage', 'garages'],
['goose', 'geese'],
['half', ['halfs', 'halves']],
['hat', 'hats'],
['hero', 'heroes'],
['hippocampus', 'hippocampi'],
['hippopotamus', 'hippopotami'], // hippopotamuses
['hoax', 'hoaxes'],
['hoof', ['hoofs', 'hooves']],
['house', 'houses'],
['icon', 'icons'],
['index', ['indicies', 'indexes']],
['ion', 'ions'],
['iris', 'irises'],
['kiss', 'kisses'],
['knife', 'knives'],
['lamp', 'lamps'],
['leaf', ['leafs', 'leaves']],
['lesson', 'lessons'],
['life', 'lives'],
['louse', 'lice'],
['man', 'men'],
['matrix', ['matrices', 'matrixes']],
['medium', 'media'],
['memorandum', 'memoranda'],
['mouse', 'mice'],
['move', 'moves'],
['movie', 'movies'],
['nebula', 'nebulae'],
['neurosis', 'neuroses'],
['news', 'news'],
['oasis', 'oases'],
['objective', 'objectives'],
['ox', 'oxen'],
['party', 'parties'],
['person', ['persons', 'people']],
['phenomenon', 'phenomena'],
['photo', 'photos'],
['piano', 'pianos'],
['plateau', ['plateaus', 'plateaux']],
['poison', 'poisons'],
['poppy', 'poppies'],
['price', 'prices'],
['quiz', 'quizzes'],
['quorum', ['quora', 'quorums']],
['radius', 'radii'],
['roof', ['roofs', 'rooves']],
['rose', 'roses'],
['sandwich', 'sandwiches'],
['scarf', ['scarfs', 'scarves']],
['schema', 'schemas'], // schemata
['season', 'seasons'],
['selfie', 'selfies'],
['series', 'series'],
['service', 'services'],
['sheriff', 'sheriffs'],
['shoe', 'shoes'],
['species', 'species'],
['status', ['status', 'statuses']],
['stratum', 'strata'],
['spy', 'spies'],
['staff', 'staves'],
['story', 'stories'],
['stratum', 'strata'],
['suitcase', 'suitcases'],
['syllabus', 'syllabi'],
['tag', 'tags'],
['thief', ['thiefs', 'thieves']],
['tooth', 'teeth'],
['treason', 'treasons'],
['tree', 'trees'],
['waltz', 'waltzes'],
['wife', 'wives'],
['icon', 'icons'],
['hippocampus', 'hippocampi'],
['campus', 'campuses'],
['hardware', 'hardware'],
['alias', 'aliases'],
// test casing: if the first letter was uppercase, it should remain so
['Man', 'Men'],
['GrandChild', 'GrandChildren'],
['SubTree', 'SubTrees'],
];
}
/**
* @dataProvider singularizeProvider
*/
public function testSingularize(string $plural, $singular)
{
$this->assertSame(\is_array($singular) ? $singular : [$singular], (new EnglishInflector())->singularize($plural));
}
/**
* @dataProvider pluralizeProvider
*/
public function testPluralize(string $singular, $plural)
{
$this->assertSame(\is_array($plural) ? $plural : [$plural], (new EnglishInflector())->pluralize($singular));
}
public function testPluralizeEmptyString()
{
$plural = (new EnglishInflector())->pluralize('');
$this->assertSame([''], $plural);
}
public function testSingularizeEmptyString()
{
$singular = (new EnglishInflector())->singularize('');
$this->assertSame([''], $singular);
}
}

View File

@@ -0,0 +1,149 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests\Inflector;
use PHPUnit\Framework\TestCase;
use Symfony\Component\String\Inflector\FrenchInflector;
class FrenchInflectorTest extends TestCase
{
public static function pluralizeProvider()
{
return [
// Le pluriel par défaut
['voiture', 'voitures'],
// special characters
['œuf', 'œufs'],
['oeuf', 'oeufs'],
// Les mots finissant par s, x, z sont invariables en nombre
['bois', 'bois'],
['fils', 'fils'],
['héros', 'héros'],
['nez', 'nez'],
['rictus', 'rictus'],
['sans', 'sans'],
['souris', 'souris'],
['tas', 'tas'],
['toux', 'toux'],
// Les mots finissant en eau prennent tous un x au pluriel
['eau', 'eaux'],
['sceau', 'sceaux'],
// Les mots finissant en au prennent tous un x au pluriel sauf landau
['noyau', 'noyaux'],
['landau', 'landaus'],
// Les mots finissant en eu prennent un x au pluriel sauf pneu, bleu et émeu
['pneu', 'pneus'],
['bleu', 'bleus'],
['émeu', 'émeus'],
['cheveu', 'cheveux'],
// Les mots finissant en al se terminent en aux au pluriel
['amiral', 'amiraux'],
['animal', 'animaux'],
['arsenal', 'arsenaux'],
['bocal', 'bocaux'],
['canal', 'canaux'],
['capital', 'capitaux'],
['caporal', 'caporaux'],
['cheval', 'chevaux'],
['cristal', 'cristaux'],
['général', 'généraux'],
['hopital', 'hopitaux'],
['hôpital', 'hôpitaux'],
['idéal', 'idéaux'],
['journal', 'journaux'],
['littoral', 'littoraux'],
['local', 'locaux'],
['mal', 'maux'],
['métal', 'métaux'],
['minéral', 'minéraux'],
['principal', 'principaux'],
['radical', 'radicaux'],
['terminal', 'terminaux'],
// sauf bal, carnaval, caracal, chacal, choral, corral, étal, festival, récital et val
['bal', 'bals'],
['carnaval', 'carnavals'],
['caracal', 'caracals'],
['chacal', 'chacals'],
['choral', 'chorals'],
['corral', 'corrals'],
['étal', 'étals'],
['festival', 'festivals'],
['récital', 'récitals'],
['val', 'vals'],
// Les noms terminés en -ail prennent un s au pluriel.
['portail', 'portails'],
['rail', 'rails'],
// SAUF aspirail, bail, corail, émail, fermail, soupirail, travail, vantail et vitrail qui font leur pluriel en -aux
['aspirail', 'aspiraux'],
['bail', 'baux'],
['corail', 'coraux'],
['émail', 'émaux'],
['fermail', 'fermaux'],
['soupirail', 'soupiraux'],
['travail', 'travaux'],
['vantail', 'vantaux'],
['vitrail', 'vitraux'],
// Les noms terminés en -ou prennent un s au pluriel.
['trou', 'trous'],
['fou', 'fous'],
// SAUF Bijou, caillou, chou, genou, hibou, joujou et pou qui prennent un x au pluriel
['bijou', 'bijoux'],
['caillou', 'cailloux'],
['chou', 'choux'],
['genou', 'genoux'],
['hibou', 'hiboux'],
['joujou', 'joujoux'],
['pou', 'poux'],
// Inflected word
['cinquante', 'cinquante'],
['soixante', 'soixante'],
['mille', 'mille'],
// Titles
['monsieur', 'messieurs'],
['madame', 'mesdames'],
['mademoiselle', 'mesdemoiselles'],
['monseigneur', 'messeigneurs'],
];
}
/**
* @dataProvider pluralizeProvider
*/
public function testSingularize(string $singular, string $plural)
{
$this->assertSame([$singular], (new FrenchInflector())->singularize($plural));
// test casing: if the first letter was uppercase, it should remain so
$this->assertSame([ucfirst($singular)], (new FrenchInflector())->singularize(ucfirst($plural)));
}
/**
* @dataProvider pluralizeProvider
*/
public function testPluralize(string $singular, string $plural)
{
$this->assertSame([$plural], (new FrenchInflector())->pluralize($singular));
// test casing: if the first letter was uppercase, it should remain so
$this->assertSame([ucfirst($plural)], (new FrenchInflector())->pluralize(ucfirst($singular)));
}
}

View File

@@ -0,0 +1,158 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests\Inflector;
use PHPUnit\Framework\TestCase;
use Symfony\Component\String\Inflector\SpanishInflector;
class SpanishInflectorTest extends TestCase
{
public static function singularizeProvider(): array
{
return [
// vowels (RAE 3.2a, 3.2c)
['peras', 'pera'],
['especies', 'especie'],
['álcalis', 'álcali'],
['códigos', 'código'],
['espíritus', 'espíritu'],
// accented (RAE 3.2a, 3.2c)
['papás', 'papá'],
['cafés', 'café'],
['isrealís', 'isrealí'],
['burós', 'buró'],
['tisús', 'tisú'],
// ending in -ión
['aviones', 'avión'],
['camiones', 'camión'],
// ending in some letters (RAE 3.2k)
['amores', 'amor'],
['antifaces', 'antifaz'],
['atriles', 'atril'],
['fácsimiles', 'fácsimil'],
['vides', 'vid'],
['reyes', 'rey'],
['relojes', 'reloj'],
['faxes', 'fax'],
['sándwiches', 'sándwich'],
['cánones', 'cánon'],
// (RAE 3.2n)
['adioses', 'adiós'],
['aguarrases', 'aguarrás'],
['arneses', 'arnés'],
['autobuses', 'autobús'],
['kermeses', 'kermés'],
['palmareses', 'palmarés'],
['toses', 'tos'],
// Special
['síes', 'sí'],
['noes', 'no'],
];
}
public static function pluralizeProvider(): array
{
return [
// vowels (RAE 3.2a, 3.2c)
['pera', 'peras'],
['especie', 'especies'],
['álcali', 'álcalis'],
['código', 'códigos'],
['espíritu', 'espíritus'],
// accented (RAE 3.2a, 3.2c)
['papá', 'papás'],
['café', 'cafés'],
['isrealí', 'isrealís'],
['buró', 'burós'],
['tisú', 'tisús'],
// ending in -ión
['avión', 'aviones'],
['camión', 'camiones'],
// ending in some letters (RAE 3.2k)
['amor', 'amores'],
['antifaz', 'antifaces'],
['atril', 'atriles'],
['fácsimil', 'fácsimiles'],
['vid', 'vides'],
['rey', 'reyes'],
['reloj', 'relojes'],
['fax', 'faxes'],
['sándwich', 'sándwiches'],
['cánon', 'cánones'],
// (RAE 3.2n)
['adiós', 'adioses'],
['aguarrás', 'aguarrases'],
['arnés', 'arneses'],
['autobús', 'autobuses'],
['kermés', 'kermeses'],
['palmarés', 'palmareses'],
['tos', 'toses'],
// Specials
['sí', 'síes'],
['no', 'noes'],
];
}
public static function uninflectedProvider(): array
{
return [
['lunes'],
['rodapiés'],
['reposapiés'],
['miércoles'],
['pies'],
];
}
/**
* @dataProvider singularizeProvider
*/
public function testSingularize(string $plural, $singular)
{
$this->assertSame(
\is_array($singular) ? $singular : [$singular],
(new SpanishInflector())->singularize($plural)
);
}
/**
* @dataProvider pluralizeProvider
*/
public function testPluralize(string $singular, $plural)
{
$this->assertSame(
\is_array($plural) ? $plural : [$plural],
(new SpanishInflector())->pluralize($singular)
);
}
/**
* @dataProvider uninflectedProvider
*/
public function testUninflected(string $word)
{
$this->assertSame(
[$word],
(new SpanishInflector())->pluralize($word)
);
}
}

View File

@@ -0,0 +1,111 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\ErrorHandler\ErrorHandler;
use Symfony\Component\String\LazyString;
class LazyStringTest extends TestCase
{
public function testLazyString()
{
$count = 0;
$s = LazyString::fromCallable(function () use (&$count) {
return ++$count;
});
$this->assertSame(0, $count);
$this->assertSame('1', (string) $s);
$this->assertSame(1, $count);
}
/**
* @runInSeparateProcess
*/
public function testReturnTypeError()
{
ErrorHandler::register();
$s = LazyString::fromCallable(fn () => []);
$this->expectException(\TypeError::class);
$this->expectExceptionMessageMatches('{^Return value of .*\{closure.*\}\(\) passed to '.preg_quote(LazyString::class).'::fromCallable\(\) must be of the type string, array returned\.$}');
(string) $s;
}
public function testLazyCallable()
{
$count = 0;
$s = LazyString::fromCallable([function () use (&$count) {
return new class($count) {
private int $count;
public function __construct(int &$count)
{
$this->count = &$count;
}
public function __invoke()
{
return ++$this->count;
}
};
}]);
$this->assertSame(0, $count);
$this->assertSame('1', (string) $s);
$this->assertSame(1, $count);
$this->assertSame('1', (string) $s); // ensure the value is memoized
$this->assertSame(1, $count);
}
public function testFromStringable()
{
$this->assertInstanceOf(LazyString::class, LazyString::fromStringable('abc'));
$this->assertSame('abc', (string) LazyString::fromStringable('abc'));
$this->assertSame('1', (string) LazyString::fromStringable(true));
$this->assertSame('', (string) LazyString::fromStringable(false));
$this->assertSame('123', (string) LazyString::fromStringable(123));
$this->assertSame('123.456', (string) LazyString::fromStringable(123.456));
$this->assertStringContainsString('hello', (string) LazyString::fromStringable(new \Exception('hello')));
}
public function testResolve()
{
$this->assertSame('abc', LazyString::resolve('abc'));
$this->assertSame('1', LazyString::resolve(true));
$this->assertSame('', LazyString::resolve(false));
$this->assertSame('123', LazyString::resolve(123));
$this->assertSame('123.456', LazyString::resolve(123.456));
$this->assertStringContainsString('hello', LazyString::resolve(new \Exception('hello')));
}
public function testIsStringable()
{
$this->assertTrue(LazyString::isStringable('abc'));
$this->assertTrue(LazyString::isStringable(true));
$this->assertTrue(LazyString::isStringable(false));
$this->assertTrue(LazyString::isStringable(123));
$this->assertTrue(LazyString::isStringable(123.456));
$this->assertTrue(LazyString::isStringable(new \Exception('hello')));
}
public function testIsNotStringable()
{
$this->assertFalse(LazyString::isStringable(null));
$this->assertFalse(LazyString::isStringable([]));
$this->assertFalse(LazyString::isStringable(\STDIN));
$this->assertFalse(LazyString::isStringable(new \stdClass()));
}
}

View File

@@ -0,0 +1,109 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests\Slugger;
use PHPUnit\Framework\TestCase;
use Symfony\Component\String\Slugger\AsciiSlugger;
class AsciiSluggerTest extends TestCase
{
/**
* @dataProvider provideSlugTests
*/
public function testSlug(string $expected, string $string, string $separator = '-', ?string $locale = null)
{
$slugger = new AsciiSlugger();
$this->assertSame($expected, (string) $slugger->slug($string, $separator, $locale));
}
public static function provideSlugTests(): iterable
{
yield ['', ''];
yield ['foo', ' foo '];
yield ['foo-bar', 'foo bar'];
yield ['foo-bar', 'foo@bar', '-'];
yield ['foo-at-bar', 'foo@bar', '-', 'en'];
yield ['e-a', 'é$!à'];
yield ['e_a', 'é$!à', '_'];
yield ['a', 'ä'];
yield ['a', 'ä', '-', 'fr'];
yield ['ae', 'ä', '-', 'de'];
yield ['ae', 'ä', '-', 'de_fr']; // Ensure we get the parent locale
yield [\function_exists('transliterator_transliterate') ? 'g' : '', 'ғ', '-'];
yield [\function_exists('transliterator_transliterate') ? 'gh' : '', 'ғ', '-', 'uz'];
yield [\function_exists('transliterator_transliterate') ? 'gh' : '', 'ғ', '-', 'uz_fr']; // Ensure we get the parent locale
}
/**
* @dataProvider provideSlugEmojiTests
*
* @requires extension intl
*/
public function testSlugEmoji(string $expected, string $string, ?string $locale, string|bool $emoji = true)
{
$slugger = new AsciiSlugger();
$slugger = $slugger->withEmoji($emoji);
$this->assertSame($expected, (string) $slugger->slug($string, '-', $locale));
}
public static function provideSlugEmojiTests(): iterable
{
yield [
'un-chat-qui-sourit-chat-noir-et-un-tete-de-lion-vont-au-parc-national',
'un 😺, 🐈‍⬛, et un 🦁 vont au 🏞️',
'fr',
];
yield [
'a-grinning-cat-black-cat-and-a-lion-go-to-national-park-smiling-face-with-heart-eyes-party-popper-yellow-heart',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
'en',
];
yield [
'a-and-a-go-to',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
null,
];
yield [
'a-smiley-cat-black-cat-and-a-lion-face-go-to-national-park-heart-eyes-tada-yellow-heart',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
null,
'slack',
];
yield [
'a-smiley-cat-black-cat-and-a-lion-go-to-national-park-heart-eyes-tada-yellow-heart',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
null,
'github',
];
yield [
'a-smiley-cat-black-cat-and-a-lion-go-to-national-park-heart-eyes-tada-yellow-heart',
'a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛',
'en',
'github',
];
yield [
'un-chat-qui-sourit-chat-noir-et-un-tete-de-lion-vont-au-parc-national',
'un 😺, 🐈‍⬛, et un 🦁 vont au 🏞️',
'fr_XX', // Fallback on parent locale
];
yield [
'un-et-un-vont-au',
'un 😺, 🐈‍⬛, et un 🦁 vont au 🏞️',
'undefined_locale', // Behaves the same as if emoji support is disabled
];
}
}

View File

@@ -0,0 +1,110 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests;
use PHPUnit\Framework\TestCase;
use Symfony\Component\String\Slugger\AsciiSlugger;
class SluggerTest extends TestCase
{
/**
* @requires extension intl
*
* @dataProvider provideSlug
*/
public function testSlug(string $string, string $locale, string $expectedSlug)
{
$slugger = new AsciiSlugger($locale);
$this->assertSame($expectedSlug, (string) $slugger->slug($string));
}
public static function provideSlug(): array
{
return [
['Стойността трябва да бъде лъжа', 'bg', 'Stoinostta-tryabva-da-bude-luzha'],
['You & I', 'en', 'You-and-I'],
['symfony@symfony.com', 'en', 'symfony-at-symfony-com'],
['Dieser Wert sollte größer oder gleich', 'de', 'Dieser-Wert-sollte-groesser-oder-gleich'],
['Dieser Wert sollte größer oder gleich', 'de_AT', 'Dieser-Wert-sollte-groesser-oder-gleich'],
['Αυτή η τιμή πρέπει να είναι ψευδής', 'el', 'Avti-i-timi-prepi-na-inai-psevdhis'],
['该变量的值应为', 'zh', 'gai-bian-liang-de-zhi-ying-wei'],
['該變數的值應為', 'zh_TW', 'gai-bian-shu-de-zhi-ying-wei'],
['Wôrķšƥáçè ~~sèťtïñğš~~', 'C', 'Workspace-settings'],
];
}
public function testSeparatorWithoutLocale()
{
$slugger = new AsciiSlugger();
$this->assertSame('hello-world', (string) $slugger->slug('hello world'));
$this->assertSame('hello_world', (string) $slugger->slug('hello world', '_'));
}
public function testSlugCharReplacementLocaleConstruct()
{
$slugger = new AsciiSlugger('fr', ['fr' => ['&' => 'et', '@' => 'chez']]);
$slug = (string) $slugger->slug('toi & moi avec cette adresse slug@test.fr', '_');
$this->assertSame('toi_et_moi_avec_cette_adresse_slug_chez_test_fr', $slug);
}
public function testSlugCharReplacementLocaleMethod()
{
$slugger = new AsciiSlugger(null, ['es' => ['&' => 'y', '@' => 'en senal']]);
$slug = (string) $slugger->slug('yo & tu a esta dirección slug@test.es', '_', 'es');
$this->assertSame('yo_y_tu_a_esta_direccion_slug_en_senal_test_es', $slug);
}
public function testSlugCharReplacementLocaleConstructWithoutSymbolsMap()
{
$slugger = new AsciiSlugger('en');
$slug = (string) $slugger->slug('you & me with this address slug@test.uk', '_');
$this->assertSame('you_and_me_with_this_address_slug_at_test_uk', $slug);
}
public function testSlugCharReplacementParentLocaleConstructWithoutSymbolsMap()
{
$slugger = new AsciiSlugger('en_GB');
$slug = (string) $slugger->slug('you & me with this address slug@test.uk', '_');
$this->assertSame('you_and_me_with_this_address_slug_at_test_uk', $slug);
}
public function testSlugCharReplacementParentLocaleConstruct()
{
$slugger = new AsciiSlugger('fr_FR', ['fr' => ['&' => 'et', '@' => 'chez']]);
$slug = (string) $slugger->slug('toi & moi avec cette adresse slug@test.fr', '_');
$this->assertSame('toi_et_moi_avec_cette_adresse_slug_chez_test_fr', $slug);
}
public function testSlugCharReplacementParentLocaleMethod()
{
$slugger = new AsciiSlugger(null, ['es' => ['&' => 'y', '@' => 'en senal']]);
$slug = (string) $slugger->slug('yo & tu a esta dirección slug@test.es', '_', 'es_ES');
$this->assertSame('yo_y_tu_a_esta_direccion_slug_en_senal_test_es', $slug);
}
public function testSlugClosure()
{
$slugger = new AsciiSlugger(null, function ($s, $locale) {
$this->assertSame('foo', $locale);
return str_replace('❤️', 'love', $s);
});
$this->assertSame('love', (string) $slugger->slug('❤️', '-', 'foo'));
}
}

View File

@@ -0,0 +1,266 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\String\Tests;
use Symfony\Component\String\AbstractString;
use Symfony\Component\String\UnicodeString;
class UnicodeStringTest extends AbstractUnicodeTestCase
{
protected static function createFromString(string $string): AbstractString
{
return new UnicodeString($string);
}
public static function provideWrap(): array
{
return array_merge(
parent::provideWrap(),
[
[
['Käse' => static::createFromString('köstlich'), 'fromage' => static::createFromString('délicieux')],
["Ka\u{0308}se" => "ko\u{0308}stlich", 'fromage' => 'délicieux'],
],
[
['a' => 1, 'ä' => ['ö' => 2, 'ü' => 3]],
['a' => 1, "a\u{0308}" => ["o\u{0308}" => 2, 'ü' => 3]],
],
]
);
}
public static function provideLength(): array
{
return array_merge(
parent::provideLength(),
[
// 5 letters + 3 combining marks
[5, 'अनुच्छेद'],
]
);
}
public static function provideSplit(): array
{
return array_merge(
parent::provideSplit(),
[
[
'अ.नु.च्.छे.द',
'.',
[
static::createFromString('अ'),
static::createFromString('नु'),
static::createFromString('च्'),
static::createFromString('छे'),
static::createFromString('द'),
],
null,
],
]
);
}
public static function provideChunk(): array
{
return array_merge(
parent::provideChunk(),
[
[
'अनुच्छेद',
[
static::createFromString('अ'),
static::createFromString('नु'),
static::createFromString('च्'),
static::createFromString('छे'),
static::createFromString('द'),
],
1,
],
]
);
}
public static function provideBytesAt(): array
{
return array_merge(
parent::provideBytesAt(),
[
[[0xC3, 0xA4], "Spa\u{0308}ßchen", 2],
[[0x61, 0xCC, 0x88], "Spa\u{0308}ßchen", 2, UnicodeString::NFD],
[[0xE0, 0xA4, 0xB8, 0xE0, 0xA5, 0x8D], 'नमस्ते', 2],
]
);
}
public static function provideCodePointsAt(): array
{
return array_merge(
parent::provideCodePointsAt(),
[
[[0xE4], "Spa\u{0308}ßchen", 2],
[[0x61, 0x0308], "Spa\u{0308}ßchen", 2, UnicodeString::NFD],
[[0x0938, 0x094D], 'नमस्ते', 2],
]
);
}
public static function provideLower(): array
{
return array_merge(
parent::provideLower(),
[
// Hindi
['अनुच्छेद', 'अनुच्छेद'],
]
);
}
public static function provideUpper(): array
{
return array_merge(
parent::provideUpper(),
[
// Hindi
['अनुच्छेद', 'अनुच्छेद'],
]
);
}
public static function provideAppend(): array
{
return array_merge(
parent::provideAppend(),
[
[
'तद्भव देशज',
['तद्भव', ' ', 'देशज'],
],
[
'तद्भव देशज विदेशी',
['तद्भव', ' देशज', ' विदेशी'],
],
]
);
}
public static function providePrepend(): array
{
return array_merge(
parent::providePrepend(),
[
[
'देशज तद्भव',
['तद्भव', 'देशज '],
],
[
'विदेशी देशज तद्भव',
['तद्भव', 'देशज ', 'विदेशी '],
],
]
);
}
public static function provideBeforeAfter(): array
{
return array_merge(
parent::provideBeforeAfter(),
[
['द foo अनुच्छेद', 'द', 'अनुच्छेद foo अनुच्छेद', 0, false],
['अनुच्छे', 'द', 'अनुच्छेद foo अनुच्छेद', 0, true],
]
);
}
public static function provideBeforeAfterIgnoreCase(): array
{
return array_merge(
parent::provideBeforeAfterIgnoreCase(),
[
['दछेच्नुअ', 'छेछे', 'दछेच्नुअ', 0, false],
['दछेच्नुअ', 'छेछे', 'दछेच्नुअ', 0, true],
['छेच्नुअ', 'छे', 'दछेच्नुअ', 0, false],
['द', 'छे', 'दछेच्नुअ', 0, true],
]
);
}
public static function provideBeforeAfterLast(): array
{
return array_merge(
parent::provideBeforeAfterLast(),
[
['दछेच्नुअ-दछेच्नु-अदछेच्नु', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, false],
['दछेच्नुअ-दछेच्नु-अदछेच्नु', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, true],
['-दछेच्नु', '-द', 'दछेच्नुअ-दछेच्नु-अद-दछेच्नु', 0, false],
['दछेच्नुअ-दछेच्नु-अद', '-द', 'दछेच्नुअ-दछेच्नु-अद-दछेच्नु', 0, true],
]
);
}
public static function provideBeforeAfterLastIgnoreCase(): array
{
return array_merge(
parent::provideBeforeAfterLastIgnoreCase(),
[
['दछेच्नुअ-दछेच्नु-अदछेच्नु', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, false],
['दछेच्नुअ-दछेच्नु-अदछेच्नु', 'छेछे', 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 0, true],
['-दछेच्नु', '-द', 'दछेच्नुअ-दछेच्नु-अद-दछेच्नु', 0, false],
['दछेच्नुअ-दछेच्नु-अद', '-द', 'दछेच्नुअ-दछेच्नु-अद-दछेच्नु', 0, true],
]
);
}
public static function provideReplace(): array
{
return array_merge(
parent::provideReplace(),
[
['Das Innenministerium', 1, 'Das Außenministerium', 'Auß', 'Inn'],
['दछेच्नुद-दछेच्नु-ददछेच्नु', 2, 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 'अ', 'द'],
]
);
}
public static function provideReplaceIgnoreCase(): array
{
return array_merge(
parent::provideReplaceIgnoreCase(),
[
['Das Aussenministerium', 1, 'Das Außenministerium', 'auß', 'Auss'],
['दछेच्नुद-दछेच्नु-ददछेच्नु', 2, 'दछेच्नुअ-दछेच्नु-अदछेच्नु', 'अ', 'द'],
]
);
}
public static function provideStartsWith()
{
return array_merge(
parent::provideStartsWith(),
[
[false, "cle\u{0301} prive\u{0301}e", 'cle', UnicodeString::NFD],
[true, "cle\u{0301} prive\u{0301}e", 'clé', UnicodeString::NFD],
]
);
}
public static function provideEndsWith()
{
return array_merge(
parent::provideEndsWith(),
[
[false, "cle\u{0301} prive\u{0301}e", 'ee', UnicodeString::NFD],
[true, "cle\u{0301} prive\u{0301}e", 'ée', UnicodeString::NFD],
]
);
}
}