FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
formatphp/tests/Intl/MessageFormatTest.php at main · formatphp/formatphp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
formatphp
/
formatphp
Public
forked from
Skillshare/formatphp
Notifications
You must be signed in to change notification settings
Fork
0
Star
3
Code
Issues
0
Pull requests
1
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
formatphp
/
tests
/
Intl
/
MessageFormatTest.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
447 lines (371 loc) · 15 KB
Breadcrumbs
formatphp
/
tests
/
Intl
/
MessageFormatTest.php
Copy path
File metadata and controls
447 lines (371 loc) · 15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
declare
(strict_types=
1
);
namespace
FormatPHP
\
Test
\
Intl
;
use
FormatPHP
\
Config
;
use
FormatPHP
\
ConfigInterface
;
use
FormatPHP
\
Descriptor
;
use
FormatPHP
\
DescriptorInterface
;
use
FormatPHP
\
Exception
\
UnableToFormatMessageException
;
use
FormatPHP
\
FormatterInterface
;
use
FormatPHP
\
Icu
\
MessageFormat
\
Parser
\
Exception
\
UnableToParseMessageException
;
use
FormatPHP
\
Intl
\
Locale
;
use
FormatPHP
\
Intl
\
MessageFormat
;
use
FormatPHP
\
Message
;
use
FormatPHP
\
MessageCollection
;
use
FormatPHP
\
MessageInterface
;
use
FormatPHP
\
Test
\
TestCase
;
use
FormatPHP
\
Util
\
DescriptorIdBuilder
;
use
PHPUnit
\
Framework
\
Attributes
\
DataProvider
;
/**
* @phpstan-import-type MessageValuesType from FormatterInterface
*/
class
MessageFormatTest
extends
TestCase
{
use
DescriptorIdBuilder;
private
const
TRANSLATION_MESSAGES_EN
= [
'
myMessage
'
=>
'
Today is {ts, date, ::yyyyMMdd}
'
,
'
foo
'
=>
'
A translation string with no default message
'
,
'
bar
'
=>
'
Howdy!
'
,
'
baz
'
=>
'
I don
\'
t know what to write here.
'
,
'
z2BIsL
'
=>
'
What are you doing this weekend?
'
,
'
KBErIh
'
=>
'
Last time I checked, {gender, select, male {he had} female {she had} other {they had} }
'
.
'
{petCount, plural, =0 {no pets} =1 {a pet} other {# pets} }.
'
,
];
/**
* @var MessageInterface[]
*/
private
array
$
messagesEn
= [];
private
ConfigInterface
$
config
;
protected
function
setUp
():
void
{
parent
::
setUp
();
foreach
(
self
::
TRANSLATION_MESSAGES_EN
as
$
id
=>
$
value
) {
$
this
->
messagesEn
[] =
new
Message
(
$
id
,
$
value
);
}
}
protected
function
getConfig
():
ConfigInterface
{
return
$
this
->
config
;
}
public
function
testFormatThrowsExceptionWhenUnableToFormatMessage
():
void
{
$
formatter
=
new
MessageFormat
(
new
Locale
(
'
en
'
));
$
this
->
expectException
(UnableToFormatMessageException::class);
$
this
->
expectExceptionMessage
(
'
Unable to format message with pattern "" for locale "en"
'
);
$
formatter
->
format
(
''
);
}
/**
* @param array<array-key, int | float | string> $replacements
*/
#[DataProvider(
'
formatProvider
'
)]
public
function
testFormat
(
Locale
$
locale
,
DescriptorInterface
$
descriptor
,
string
$
expected
,
array
$
replacements
= []
):
void
{
$
this
->
config
=
new
Config
(
$
locale
);
$
formatter
=
new
MessageFormat
(
$
locale
);
$
messages
=
new
MessageCollection
(
$
this
->
messagesEn
);
$
messageId
=
$
this
->
buildMessageId
(
$
descriptor
);
/** @var MessageInterface $message */
$
message
=
$
messages
[
$
messageId
];
$
this
->
assertSame
(
$
expected
,
$
formatter
->
format
(
$
message
->
getMessage
(),
$
replacements
));
}
/**
* @return mixed[]
*/
public
static
function
formatProvider
():
array
{
$
localeEn
=
new
Locale
(
'
en
'
);
$
localeFoo
=
new
Locale
(
'
foo
'
);
$
descriptors
= [
'
full
'
=>
new
Descriptor
(
'
myMessage
'
,
'
Today is {ts, date, ::yyyyMMdd}
'
,
'
This tells the user what day it is today
'
,
),
'
id only
'
=>
new
Descriptor
(
'
foo
'
),
'
id with defaultMessage
'
=>
new
Descriptor
(
'
bar
'
,
'
Howdy!
'
),
'
id with description
'
=>
new
Descriptor
(
'
baz
'
,
null
,
'
There is not default message for this one
'
),
'
defaultMessage only
'
=>
new
Descriptor
(
null
,
'
What are you doing this weekend?
'
),
'
complicated pattern
'
=>
new
Descriptor
(
null
,
// There are extra newlines in here to test proper trimming.
<<<'EOM'
Last time I checked, {gender, select,
male {he had}
female {she had}
other {they had}
} {petCount, plural,
=0 {no pets}
=1 {a pet}
other {# pets}
}.
EOM,
'
This is a more complicated message pattern.
'
,
),
];
return
[
[
'
locale
'
=>
$
localeEn
,
'
descriptor
'
=>
$
descriptors
[
'
full
'
],
'
expected
'
=>
'
Today is 10/25/2021
'
,
'
replacements
'
=> [
'
ts
'
=>
1635204852
],
// Mon, 25 Oct 2021 23:34:12 +0000
],
[
'
locale
'
=>
$
localeEn
,
'
descriptor
'
=>
$
descriptors
[
'
id only
'
],
'
expected
'
=>
'
A translation string with no default message
'
,
],
[
'
locale
'
=>
$
localeFoo
,
'
descriptor
'
=>
$
descriptors
[
'
id with defaultMessage
'
],
'
expected
'
=>
'
Howdy!
'
,
],
[
'
locale
'
=>
$
localeEn
,
'
descriptor
'
=>
$
descriptors
[
'
id with description
'
],
'
expected
'
=>
'
I don
\'
t know what to write here.
'
,
],
[
'
locale
'
=>
$
localeEn
,
'
descriptor
'
=>
$
descriptors
[
'
defaultMessage only
'
],
'
expected
'
=>
'
What are you doing this weekend?
'
,
],
[
'
locale
'
=>
$
localeEn
,
'
descriptor
'
=>
$
descriptors
[
'
complicated pattern
'
],
'
expected
'
=>
'
Last time I checked, he had no pets.
'
,
'
replacements
'
=> [
'
gender
'
=>
'
male
'
,
'
petCount
'
=>
0
],
],
[
'
locale
'
=>
$
localeFoo
,
'
descriptor
'
=>
$
descriptors
[
'
complicated pattern
'
],
'
expected
'
=>
'
Last time I checked, they had a pet.
'
,
'
replacements
'
=> [
'
gender
'
=>
'
non-binary
'
,
'
petCount
'
=>
1
],
],
[
'
locale
'
=>
$
localeFoo
,
'
descriptor
'
=>
$
descriptors
[
'
complicated pattern
'
],
'
expected
'
=>
'
Last time I checked, he had no pets.
'
,
'
replacements
'
=> [
'
gender
'
=>
'
male
'
,
'
petCount
'
=>
0
],
],
[
'
locale
'
=>
$
localeEn
,
'
descriptor
'
=>
$
descriptors
[
'
complicated pattern
'
],
'
expected
'
=>
'
Last time I checked, he had a pet.
'
,
'
replacements
'
=> [
'
gender
'
=>
'
male
'
,
'
petCount
'
=>
1
],
],
];
}
public
function
testTags
():
void
{
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
/** @var MessageValuesType $values */
$
values
= [
'
name
'
=>
'
Samwise
'
,
'
profileLink
'
=>
fn
(
string
$
text
):
string
=>
'
<a href="https://example.com">
'
.
$
text
.
'
</a>
'
,
'
boldface
'
=>
fn
(
string
$
text
):
string
=>
"
<strong>
$
text
</strong>
"
,
'
italicized
'
=>
fn
(
string
$
text
):
string
=>
"
<em>
$
text
</em>
"
,
];
$
formatted
=
$
formatter
->
format
(
'
Hi, <profileLink><boldface>{name}</boldface>, our <italicized>great friend</italicized></profileLink>!
'
,
$
values
,
);
$
this
->
assertSame
(
'
Hi, <a href="https://example.com"><strong>Samwise</strong>, our <em>great friend</em></a>!
'
,
$
formatted
,
);
}
public
function
testMixedTags
():
void
{
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
/** @var MessageValuesType $values */
$
values
= [
'
name
'
=>
'
Pippin
'
,
'
profileLink
'
=>
fn
(
string
$
text
):
string
=>
'
<a href="https://example.com">
'
.
$
text
.
'
</a>
'
,
];
$
formatted
=
$
formatter
->
format
(
'
Hi, <profileLink><boldface>{name}</boldface>, <foo /> our
'
.
'
<italicized>great friend</italicized></profileLink>!
'
,
$
values
,
);
$
this
->
assertSame
(
'
Hi, <a href="https://example.com"><boldface>Pippin</boldface>,
'
.
'
<foo/> our <italicized>great friend</italicized></a>!
'
,
$
formatted
,
);
}
public
function
testSelectAndPluralOptionsWithTags
():
void
{
$
message
= <<<'EOD'
Last time I checked, {gender, select,
male {<italicized>he</italicized> had}
female {<italicized>she</italicized> had}
other {<italicized>they</italicized> had}
} {petCount, plural,
=0 {<bold>no</bold> pets}
=1 {<bold>a</bold> pet}
other {<bold>#</bold> pets}
}.
EOD;
$
expected
=
'
Last time I checked, <em>she</em> had <strong>4</strong> pets.
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
/** @var MessageValuesType $values */
$
values
= [
'
gender
'
=>
'
female
'
,
'
petCount
'
=>
4
,
'
italicized
'
=>
fn
(
string
$
text
):
string
=>
"
<em>
$
text
</em>
"
,
'
bold
'
=>
fn
(
string
$
text
):
string
=>
"
<strong>
$
text
</strong>
"
,
];
$
formatted
=
$
formatter
->
format
(
$
message
,
$
values
);
$
this
->
assertSame
(
$
expected
,
$
formatted
);
}
public
function
testThrowsExceptionWhenUnableToParseMessage
():
void
{
$
message
=
'
Hello, <link>{name}
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
/** @var MessageValuesType $values */
$
values
= [
'
name
'
=>
'
Bilbo
'
,
'
link
'
=>
fn
(
string
$
text
):
string
=>
"
<a>
$
text
</a>
"
,
];
// We're not using expectException() because we want to actually
// inspect the exception object as part of this test.
try
{
$
formatter
->
format
(
$
message
,
$
values
);
}
catch
(
UnableToFormatMessageException
$
exception
) {
$
this
->
assertSame
(
'
Unable to format message with pattern "Hello, <link>{name}" for locale "en-US"
'
,
$
exception
->
getMessage
(),
);
$
this
->
assertInstanceOf
(UnableToParseMessageException::class,
$
exception
->
getPrevious
());
$
this
->
assertSame
(
'
Syntax error UNCLOSED_TAG found while parsing message "Hello, <link>{name}"
'
,
$
exception
->
getPrevious
()->
getMessage
(),
);
}
}
public
function
testReplacesEmptyTagWithCallbackResultValue
():
void
{
$
message
=
'
Sometimes a <foobar></foobar> might not have a body
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
$
formatted
=
$
formatter
->
format
(
$
message
, [
'
foobar
'
=>
fn
() =>
'
<em>tag</em>
'
,
]);
$
this
->
assertSame
(
'
Sometimes a <em>tag</em> might not have a body
'
,
$
formatted
);
}
public
function
testReplacesSelfClosingTagWithCallbackResultValue
():
void
{
$
message
=
'
Sometimes a <foobar /> might be self-closing
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
$
formatted
=
$
formatter
->
format
(
$
message
, [
'
foobar
'
=>
fn
() =>
'
<strong>tag</strong>
'
,
]);
$
this
->
assertSame
(
'
Sometimes a <strong>tag</strong> might be self-closing
'
,
$
formatted
);
}
public
function
testThrowsExceptionForIllegalArgumentError
():
void
{
$
message
=
'
Today is {value, date}.
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
// We're not using expectException() because we want to actually
// inspect the exception object as part of this test.
try
{
$
formatter
->
format
(
$
message
, [
'
value
'
=>
'
not a valid date/time
'
]);
}
catch
(
UnableToFormatMessageException
$
exception
) {
$
this
->
assertSame
(
'
Unable to format message with pattern "Today is {value, date}." for locale "en-US"
'
,
$
exception
->
getMessage
(),
);
$
this
->
assertInstanceOf
(UnableToFormatMessageException::class,
$
exception
->
getPrevious
());
$
this
->
assertSame
(
"
The argument for key 'value' cannot be used as a date or time: U_ILLEGAL_ARGUMENT_ERROR
"
,
$
exception
->
getPrevious
()->
getMessage
(),
);
}
}
public
function
testProcessesPercentagesAccordingToEcma402
():
void
{
$
message
=
'
Your discount is {discount, number, ::percent} off the retail value.
'
;
$
expected
=
'
Your discount is 25% off the retail value.
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
$
result
=
$
formatter
->
format
(
$
message
, [
'
discount
'
=>
0.25
]);
$
this
->
assertSame
(
$
expected
,
$
result
);
}
public
function
testProcessesPercentagesAccordingToEcma402WithScaleAt100
():
void
{
$
message
=
'
Your discount is {discount, number, ::percent scale/100} off the retail value.
'
;
$
expected
=
'
Your discount is 2,500% off the retail value.
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
$
result
=
$
formatter
->
format
(
$
message
, [
'
discount
'
=>
0.25
]);
$
this
->
assertSame
(
$
expected
,
$
result
);
}
public
function
testProcessesPercentagesAccordingToEcma402WithScaleAt1
():
void
{
$
message
=
'
Your discount is {discount, number, ::percent scale/1} off the retail value.
'
;
$
expected
=
'
Your discount is 25% off the retail value.
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
$
result
=
$
formatter
->
format
(
$
message
, [
'
discount
'
=>
0.25
]);
$
this
->
assertSame
(
$
expected
,
$
result
);
}
public
function
testProcessesNumberWithoutStyle
():
void
{
$
message
=
'
Your discount is {discount, number} off the retail value.
'
;
$
expected
=
'
Your discount is 25 off the retail value.
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
$
result
=
$
formatter
->
format
(
$
message
, [
'
discount
'
=>
25
]);
$
this
->
assertSame
(
$
expected
,
$
result
);
}
public
function
testArrayCallablesAndClosures
():
void
{
$
message
=
'
Hello, <firstName></firstName> <lastName></lastName>!
'
;
$
expected
=
'
Hello, Jane Doe!
'
;
$
user
=
new
class
{
public
function
getFirstName
():
string
{
return
'
Jane
'
;
}
public
function
getLastName
():
string
{
return
'
Doe
'
;
}
};
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
$
result
=
$
formatter
->
format
(
$
message
,
[
'
firstName
'
=> [
$
user
,
'
getFirstName
'
],
'
lastName
'
=>
fn
():
string
=>
$
user
->
getLastName
(),
],
);
$
this
->
assertSame
(
$
expected
,
$
result
);
}
public
function
testStringsMustNotEvaluateAsCallables
():
void
{
$
message
=
'
Hello, {firstName} {lastName}!
'
;
$
expected
=
'
Hello, Ceil Floor!
'
;
$
locale
=
new
Locale
(
'
en-US
'
);
$
formatter
=
new
MessageFormat
(
$
locale
);
$
result
=
$
formatter
->
format
(
$
message
,
[
'
firstName
'
=>
'
Ceil
'
,
'
lastName
'
=>
'
Floor
'
,
],
);
$
this
->
assertSame
(
$
expected
,
$
result
);
}
}
Back
|
FazBrowse Home
|
New Git URL