|
16 | 16 |
|
17 | 17 | class UnicodeStringTest extends AbstractUnicodeTestCase |
18 | 18 | { |
| 19 | + /** |
| 20 | + * @dataProvider provideTrimNormalization |
| 21 | + */ |
| 22 | + public function testTrimPrefixNormalization(string $expected, string $string, $prefix) |
| 23 | + { |
| 24 | + $str = new UnicodeString($string); |
| 25 | + $this->assertSame($expected, $str->trimPrefix($prefix)->toString()); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @dataProvider provideTrimNormalization |
| 30 | + */ |
| 31 | + public function testTrimSuffixNormalization(string $expected, string $string, $suffix) |
| 32 | + { |
| 33 | + $suffixStr = match (true) { |
| 34 | + $suffix instanceof AbstractString => $suffix->toString(), |
| 35 | + \is_array($suffix) => implode('', $suffix), |
| 36 | + $suffix instanceof \Traversable => implode('', iterator_to_array($suffix)), |
| 37 | + default => (string) $suffix, |
| 38 | + }; |
| 39 | + |
| 40 | + $str = new UnicodeString($expected.$suffixStr); |
| 41 | + $this->assertSame($expected, $str->trimSuffix($suffix)->toString()); |
| 42 | + } |
| 43 | + |
| 44 | + public static function provideTrimNormalization(): iterable |
| 45 | + { |
| 46 | + // "é" in NFC (\xC3\xA9) vs NFD (\x65\xCC\x81) |
| 47 | + $nfc = "\xC3\xA9"; |
| 48 | + $nfd = "\x65\xCC\x81"; |
| 49 | + |
| 50 | + yield 'nfc_string_nfd_prefix' => ['abc', $nfc.'abc', $nfd]; |
| 51 | + yield 'nfd_string_nfc_prefix' => ['abc', $nfd.'abc', $nfc]; |
| 52 | + |
| 53 | + yield 'abstract_string' => ['abc', $nfc.'abc', new UnicodeString($nfd)]; |
| 54 | + |
| 55 | + yield 'array' => ['abc', $nfc.'abc', [$nfd]]; |
| 56 | + |
| 57 | + yield 'stringable' => ['abc', $nfc.'abc', new class($nfd) implements \Stringable { |
| 58 | + public function __construct(private string $s) |
| 59 | + { |
| 60 | + } |
| 61 | + |
| 62 | + public function __toString(): string |
| 63 | + { |
| 64 | + return $this->s; |
| 65 | + } |
| 66 | + }]; |
| 67 | + } |
| 68 | + |
19 | 69 | protected static function createFromString(string $string): AbstractString |
20 | 70 | { |
21 | 71 | return new UnicodeString($string); |
|
0 commit comments