-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractNormalizerTest.php
243 lines (218 loc) · 8.77 KB
/
AbstractNormalizerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
declare(strict_types=1);
namespace KynxTest\Code\Normalizer;
use AssertionError;
use Kynx\Code\Normalizer\AbstractNormalizer;
use Kynx\Code\Normalizer\NormalizerException;
use Kynx\Code\Normalizer\NormalizerInterface;
use Kynx\Code\Normalizer\WordCase;
use PHPUnit\Framework\TestCase;
use function version_compare;
use const PHP_VERSION;
/**
* @uses \Kynx\Code\Normalizer\NormalizerException
* @uses \Kynx\Code\Normalizer\PhpLabel
* @uses \Kynx\Code\Normalizer\WordCase
*
* @covers \Kynx\Code\Normalizer\AbstractNormalizer
*/
final class AbstractNormalizerTest extends TestCase
{
/**
* @dataProvider labelProvider
*/
public function testNormalize(string $label, string $expected): void
{
$actual = $this->getNormalizer()->normalize($label);
self::assertSame($expected, $actual);
}
/**
* @return array<string, list{string, string}>
*/
public static function labelProvider(): array
{
return [
'pet_shop' => ['ペット ショップ', 'PettoShoppu'],
'combining_accent' => ['Маріу́поль', 'Mariupol'],
'trailing_quote' => ["Peoples'", 'PeoplesQuote'],
'mixed_scripts' => ['坏的 سيئ bad', 'HuaiDeSyyBad'],
'symbol' => ['€', 'Euro'],
'low_ascii' => ['Eat@Joes', 'EatAtJoes'],
'high_ascii' => ['£1 Shop', '£1Shop'],
'trim' => [' Foo ', 'Foo'],
'whitespace' => ["Foo \t\nBar", 'FooBar'],
'separators' => ['Foo.Bar-Baz', 'FooBarBaz'],
'underscore' => ['foo_bar', 'FooBar'],
'lead_digits' => ['12Foo', 'OneTwoFoo'],
'single_digit' => ['7', 'Seven'],
'backtick' => ["`cat /etc/passwd`", 'BacktickCatSlashEtcSlashPasswdBacktick'],
'emoticon' => ['I am 😭', 'IAmLoudlyCryingFace'],
'weird' => ["a ͖", 'ACombiningRightArrowheadAndUpArrowheadBelow'],
'reserved' => ['Class', 'ClassReserved'],
];
}
/**
* @dataProvider caseProvider
*/
public function testNormalizeUsesCase(WordCase $case, string $expected): void
{
$actual = $this->getNormalizer($case)->normalize('foo bar');
self::assertSame($expected, $actual);
}
/**
* @return array<string, list{WordCase, string}>
*/
public static function caseProvider(): array
{
return [
'camelCase' => [WordCase::Camel, 'fooBar'],
'PascalCase' => [WordCase::Pascal, 'FooBar'],
'snake_case' => [WordCase::LowerSnake, 'foo_bar'],
'UPPER_SNAKE' => [WordCase::UpperSnake, 'FOO_BAR'],
];
}
/**
* @dataProvider spellOutCaseProvider
*/
public function testSpellOutCase(WordCase $case, string $label, string $expected): void
{
$actual = $this->getNormalizer($case)->normalize($label);
self::assertSame($expected, $actual);
}
/**
* @return array<string, list{WordCase, string, string}>
*/
public static function spellOutCaseProvider(): array
{
return [
'nonascii-before-camelCase' => [WordCase::Camel, '😫foo', 'tiredFaceFoo'],
'nonascii-before-PascalCase' => [WordCase::Pascal, '😫foo', 'TiredFaceFoo'],
'nonascii-before-snake_case' => [WordCase::LowerSnake, '😫foo', 'tired_face_foo'],
'nonascii-before-UPPER_SNAKE' => [WordCase::UpperSnake, '😫foo', 'TIRED_FACE_FOO'],
'nonascii-after-camelCase' => [WordCase::Camel, 'foo😫', 'fooTiredFace'],
'nonascii-after-PascalCase' => [WordCase::Pascal, 'foo😫', 'FooTiredFace'],
'nonascii-after-snake_case' => [WordCase::LowerSnake, 'foo😫', 'foo_tired_face'],
'nonascii-after-UPPER_SNAKE' => [WordCase::UpperSnake, 'foo😫', 'FOO_TIRED_FACE'],
'ascii-before-camelCase' => [WordCase::Camel, '$foo', 'dollarFoo'],
'ascii-before-PascalCase' => [WordCase::Pascal, '$foo', 'DollarFoo'],
'ascii-before-snake_case' => [WordCase::LowerSnake, '$foo', 'dollar_foo'],
'ascii-before-UPPER_SNAKE' => [WordCase::UpperSnake, '$foo', 'DOLLAR_FOO'],
'ascii-after-camelCase' => [WordCase::Camel, 'foo$', 'fooDollar'],
'ascii-after-PascalCase' => [WordCase::Pascal, 'foo$', 'FooDollar'],
'ascii-after-snake_case' => [WordCase::LowerSnake, 'foo$', 'foo_dollar'],
'ascii-after-UPPER_SNAKE' => [WordCase::UpperSnake, 'foo$', 'FOO_DOLLAR'],
'digit-before-camelCase' => [WordCase::Camel, '1foo', 'oneFoo'],
'digit-before-PascalCase' => [WordCase::Pascal, '1foo', 'OneFoo'],
'digit-before-snake_case' => [WordCase::LowerSnake, '1foo', 'one_foo'],
'digit-before-UPPER_SNAKE' => [WordCase::UpperSnake, '1foo', 'ONE_FOO'],
];
}
public function testNormalizeUsesSeparators(): void
{
$expected = 'FooBarBaz';
$actual = $this->getNormalizer(WordCase::Pascal, '|/')
->normalize('foo|bar /baz');
self::assertSame($expected, $actual);
}
public function testPrepareSuffixAllowsNullAndAssertionErrorThrown(): void
{
if (
version_compare(PHP_VERSION, '8.2.0-dev') >= 0
&& version_compare(PHP_VERSION, '8.2.0RC2') < 0
) {
self::markTestSkipped('assert() not throwing exception in 8.2.0-RC1');
}
try {
$normalizer = $this->getNormalizer(
WordCase::Camel,
NormalizerInterface::DEFAULT_SEPARATORS,
null
);
} catch (NormalizerException) {
self::fail("Null suffix threw exception");
}
self::expectException(AssertionError::class);
self::expectExceptionMessage('assert($this->suffix !== null)');
$normalizer->normalize('class');
}
/**
* @dataProvider invalidSuffixProvider
*/
public function testPrepareSuffixInvalidThrowsException(string $suffix): void
{
self::expectException(NormalizerException::class);
self::expectExceptionMessage("Invalid reserved word suffix");
$this->getNormalizer(
WordCase::Camel,
NormalizerInterface::DEFAULT_SEPARATORS,
$suffix
);
}
/**
* @return array<string, list{string}>
*/
public static function invalidSuffixProvider(): array
{
return [
'empty' => [''],
'invalid' => ['$'],
];
}
public function testNormalizeThrowsTransliterationException(): void
{
$invalid = "Iñtërnâtiôn\xE9àlizætiøn";
$expected = "Cannot transliterate '$invalid': "
. "String conversion of string to UTF-16 failed: U_INVALID_CHAR_FOUND";
self::expectException(NormalizerException::class);
self::expectExceptionMessage($expected);
$this->getNormalizer()->normalize($invalid);
}
public function testNormalizeMultiple(): void
{
$labels = [
'มืดมนและมีพายุ',
'اندھیرا اور طوفان',
'काळोख आनी वादळी',
];
$multiple = $this->getNormalizer();
foreach ($labels as $label) {
$expected = $this->getNormalizer()->normalize($label);
$actual = $multiple->normalize($label);
self::assertSame($expected, $actual);
}
}
public function testNormalizeTransliteratesAfterFailure(): void
{
$expected = "DingDong";
$invalid = "Iñtërnâtiôn\xE9àlizætiøn";
$normalizer = $this->getNormalizer();
try {
$normalizer->normalize($invalid);
self::fail("No exception thrown");
} catch (NormalizerException) {
// ignore
}
$actual = $normalizer->normalize('ding ₫');
self::assertSame($expected, $actual);
}
/**
* @psalm-suppress InternalClass
* @psalm-suppress InternalMethod
*/
private function getNormalizer(
WordCase $case = WordCase::Pascal,
string $separators = NormalizerInterface::DEFAULT_SEPARATORS,
string|null $suffix = 'Reserved'
): AbstractNormalizer {
return new class ($suffix, $case, $separators) extends AbstractNormalizer {
public function normalize(string $label): string
{
$ascii = $this->toAscii($label);
$spaced = $this->separatorsToSpace($ascii);
$speltOut = $this->spellOutAscii($spaced);
$cased = $this->toCase($speltOut);
return $this->sanitizeReserved($cased);
}
};
}
}