|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * SPDX-FileCopyrightText: (c) Respect Project Contributors |
| 5 | + * SPDX-License-Identifier: ISC |
| 6 | + * SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace Respect\StringFormatter\Test\Unit; |
| 12 | + |
| 13 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 15 | +use PHPUnit\Framework\Attributes\Test; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Respect\StringFormatter\InvalidFormatterException; |
| 18 | +use Respect\StringFormatter\TrimFormatter; |
| 19 | + |
| 20 | +#[CoversClass(TrimFormatter::class)] |
| 21 | +final class TrimFormatterTest extends TestCase |
| 22 | +{ |
| 23 | + #[Test] |
| 24 | + #[DataProvider('providerForWhitespace')] |
| 25 | + #[DataProvider('providerForSides')] |
| 26 | + #[DataProvider('providerForCustomMask')] |
| 27 | + #[DataProvider('providerForSpecialChars')] |
| 28 | + #[DataProvider('providerForUnicode')] |
| 29 | + #[DataProvider('providerForEmoji')] |
| 30 | + #[DataProvider('providerForMultiByte')] |
| 31 | + #[DataProvider('providerForEdgeCases')] |
| 32 | + public function itShouldTrimString(string $input, string $expected, string $side, string|null $mask = null): void |
| 33 | + { |
| 34 | + // @phpstan-ignore argument.type |
| 35 | + $formatter = new TrimFormatter($side, $mask); |
| 36 | + |
| 37 | + $actual = $formatter->format($input); |
| 38 | + |
| 39 | + self::assertSame($expected, $actual); |
| 40 | + } |
| 41 | + |
| 42 | + #[Test] |
| 43 | + public function itShouldThrowExceptionForInvalidSide(): void |
| 44 | + { |
| 45 | + $this->expectException(InvalidFormatterException::class); |
| 46 | + $this->expectExceptionMessage('Invalid side "middle"'); |
| 47 | + |
| 48 | + // @phpstan-ignore argument.type |
| 49 | + new TrimFormatter('middle'); |
| 50 | + } |
| 51 | + |
| 52 | + /** @return array<string, array{0: string, 1: string, 2: string}> */ |
| 53 | + public static function providerForWhitespace(): array |
| 54 | + { |
| 55 | + return [ |
| 56 | + 'whitespace both sides' => [' hello ', 'hello', 'both'], |
| 57 | + 'tab both sides' => ["\thello\t", 'hello', 'both'], |
| 58 | + 'newline both sides' => ["\nhello\n", 'hello', 'both'], |
| 59 | + 'mixed whitespace both' => [" \t\n hello \t\n", 'hello', 'both'], |
| 60 | + 'already trimmed both' => ['hello', 'hello', 'both'], |
| 61 | + 'only spaces both' => [' ', '', 'both'], |
| 62 | + 'ideographic space both' => ["\u{3000}hello\u{3000}", 'hello', 'both'], |
| 63 | + 'em space both' => ["\u{2003}hello\u{2003}", 'hello', 'both'], |
| 64 | + 'no-break space both' => ["\u{00A0}hello\u{00A0}", 'hello', 'both'], |
| 65 | + 'thin space both' => ["\u{2009}hello\u{2009}", 'hello', 'both'], |
| 66 | + 'mixed unicode whitespace both' => ["\u{3000}\u{2003} hello \u{00A0}\u{2009}", 'hello', 'both'], |
| 67 | + 'narrow no-break space both' => ["\u{202F}hello \u{202F}", 'hello', 'both'], |
| 68 | + ]; |
| 69 | + } |
| 70 | + |
| 71 | + /** @return array<string, array{0: string, 1: string, 2: string}> */ |
| 72 | + public static function providerForSides(): array |
| 73 | + { |
| 74 | + return [ |
| 75 | + 'spaces left' => [' hello', 'hello', 'left'], |
| 76 | + 'spaces right not trimmed left' => ['hello ', 'hello ', 'left'], |
| 77 | + 'spaces left and right left' => [' hello ', 'hello ', 'left'], |
| 78 | + 'tabs left' => ["\thello\t", "hello\t", 'left'], |
| 79 | + 'mixed whitespace left' => ["\t\n hello world", 'hello world', 'left'], |
| 80 | + 'spaces right' => ['hello ', 'hello', 'right'], |
| 81 | + 'spaces left not trimmed right' => [' hello', ' hello', 'right'], |
| 82 | + 'spaces left and right right' => [' hello ', ' hello', 'right'], |
| 83 | + 'tabs right' => ["\thello\t", "\thello", 'right'], |
| 84 | + 'mixed whitespace right' => ["hello world \t", 'hello world', 'right'], |
| 85 | + ]; |
| 86 | + } |
| 87 | + |
| 88 | + /** @return array<string, array{0: string, 1: string, 2: string, 3: string}> */ |
| 89 | + public static function providerForCustomMask(): array |
| 90 | + { |
| 91 | + return [ |
| 92 | + 'custom characters both' => ['---hello---', 'hello', 'both', '-'], |
| 93 | + 'multiple custom chars both' => ['-._hello-._', 'hello', 'both', '_.-'], |
| 94 | + 'dots both' => ['...hello...', 'hello', 'both', '.'], |
| 95 | + 'underscores both' => ['___hello___', 'hello', 'both', '_'], |
| 96 | + 'mixed custom both' => ['*-+hello+-*', 'hello', 'both', '+-*'], |
| 97 | + 'dash left' => ['--hello--', 'hello--', 'left', '-'], |
| 98 | + 'dash right' => ['--hello--', '--hello', 'right', '-'], |
| 99 | + 'all characters to trim both' => [' !!! ', '!!!', 'both', ' '], |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + /** @return array<string, array{0: string, 1: string, 2: string, 3: string}> */ |
| 104 | + public static function providerForSpecialChars(): array |
| 105 | + { |
| 106 | + return [ |
| 107 | + 'asterisk both' => ['**hello**', 'hello', 'both', '*'], |
| 108 | + 'dollar sign both' => ['$$hello$$', 'hello', 'both', '$'], |
| 109 | + 'caret both' => ['^^hello^^', 'hello', 'both', '^'], |
| 110 | + 'pipe both' => ['||hello||', 'hello', 'both', '|'], |
| 111 | + 'question mark both' => ['??hello??', 'hello', 'both', '?'], |
| 112 | + 'multiple special both' => ['@#$hello$#@', 'hello', 'both', '@#$'], |
| 113 | + ]; |
| 114 | + } |
| 115 | + |
| 116 | + /** @return array<string, array{0: string, 1: string, 2: string, 3: string}> */ |
| 117 | + public static function providerForUnicode(): array |
| 118 | + { |
| 119 | + return [ |
| 120 | + 'latin accented chars both' => ['éééhelloééé', 'hello', 'both', 'é'], |
| 121 | + 'greek letters both' => ['αααhelloααα', 'hello', 'both', 'α'], |
| 122 | + 'cyrillic letters both' => ['бббhelloббб', 'hello', 'both', 'б'], |
| 123 | + 'chinese characters both' => ['中中hello中中', 'hello', 'both', '中'], |
| 124 | + 'japanese hiragana both' => ['あああhelloあああ', 'hello', 'both', 'あ'], |
| 125 | + ]; |
| 126 | + } |
| 127 | + |
| 128 | + /** @return array<string, array{0: string, 1: string, 2: string, 3: string}> */ |
| 129 | + public static function providerForEmoji(): array |
| 130 | + { |
| 131 | + return [ |
| 132 | + 'smiley faces both' => ['😊😊hello😊😊', 'hello', 'both', '😊'], |
| 133 | + 'mixed emoji both' => ['👋👋hi👋👋', 'hi', 'both', '👋'], |
| 134 | + 'hearts both' => ['❤️❤️love❤️❤️', 'love', 'both', '❤️'], |
| 135 | + ]; |
| 136 | + } |
| 137 | + |
| 138 | + /** @return array<string, array{0: string, 1: string, 2: string, 3?: string}> */ |
| 139 | + public static function providerForMultiByte(): array |
| 140 | + { |
| 141 | + return [ |
| 142 | + 'chinese with ideographic space both' => [' 你好 ', '你好', 'both'], |
| 143 | + 'japanese with ideographic space both' => [' こんにちは ', 'こんにちは', 'both'], |
| 144 | + 'korean with ideographic space both' => [' 안녕하세요 ', '안녕하세요', 'both'], |
| 145 | + 'fullwidth letters with custom mask both' => ['aaahelloaaa', 'hello', 'both', 'a'], |
| 146 | + 'mixed cjk and ascii both' => [' hello 你好 ', 'hello 你好', 'both'], |
| 147 | + ]; |
| 148 | + } |
| 149 | + |
| 150 | + /** @return array<string, array{0: string, 1: string, 2: string, 3?: string}> */ |
| 151 | + public static function providerForEdgeCases(): array |
| 152 | + { |
| 153 | + return [ |
| 154 | + 'empty string both' => ['', '', 'both', ' '], |
| 155 | + 'string shorter than mask both' => ['a', '', 'both', 'abcdef'], |
| 156 | + 'all characters trimmed both' => ['--', '', 'both', '-'], |
| 157 | + 'only one side trimmed left' => ['--a', 'a', 'left', '-'], |
| 158 | + 'only one side trimmed right' => ['a--', 'a', 'right', '-'], |
| 159 | + 'no characters to trim both' => ['hello', 'hello', 'both', 'xyz'], |
| 160 | + 'mask longer than string both' => ['hello', 'hello', 'both', 'abcdefgzij'], |
| 161 | + 'empty mask both' => ['hello', 'hello', 'both', ''], |
| 162 | + 'repeated characters both' => ['aaaaahelloaaaaa', 'hello', 'both', 'a'], |
| 163 | + 'interleaved characters both' => ['ababhelloabab', 'hello', 'both', 'ab'], |
| 164 | + ]; |
| 165 | + } |
| 166 | +} |
0 commit comments