|
|
|
@ -22,18 +22,19 @@ class MessageFormatterTest extends TestCase
|
|
|
|
|
const SUBJECT = 'сабж'; |
|
|
|
|
const SUBJECT_VALUE = 'Answer to the Ultimate Question of Life, the Universe, and Everything'; |
|
|
|
|
|
|
|
|
|
public function testNamedArguments() |
|
|
|
|
public function patterns() |
|
|
|
|
{ |
|
|
|
|
$expected = self::SUBJECT_VALUE.' is '.self::N_VALUE; |
|
|
|
|
|
|
|
|
|
$result = MessageFormatter::formatMessage('en_US', '{'.self::SUBJECT.'} is {'.self::N.', number}', array( |
|
|
|
|
return array( |
|
|
|
|
array( |
|
|
|
|
'{'.self::SUBJECT.'} is {'.self::N.', number}', // pattern |
|
|
|
|
self::SUBJECT_VALUE.' is '.self::N_VALUE, // expected |
|
|
|
|
array( // params |
|
|
|
|
self::N => self::N_VALUE, |
|
|
|
|
self::SUBJECT => self::SUBJECT_VALUE, |
|
|
|
|
)); |
|
|
|
|
) |
|
|
|
|
), |
|
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result); |
|
|
|
|
|
|
|
|
|
$pattern = <<<_MSG_ |
|
|
|
|
array(<<<_MSG_ |
|
|
|
|
{gender_of_host, select, |
|
|
|
|
female {{num_guests, plural, offset:1 |
|
|
|
|
=0 {{host} does not give a party.} |
|
|
|
@ -50,53 +51,83 @@ class MessageFormatterTest extends TestCase
|
|
|
|
|
=1 {{host} invites {guest} to their party.} |
|
|
|
|
=2 {{host} invites {guest} and one other person to their party.} |
|
|
|
|
other {{host} invites {guest} and # other people to their party.}}}} |
|
|
|
|
_MSG_; |
|
|
|
|
$result = MessageFormatter::formatMessage('en_US', $pattern, array( |
|
|
|
|
_MSG_ |
|
|
|
|
, |
|
|
|
|
'ralph invites beep and 3 other people to his party.', |
|
|
|
|
array( |
|
|
|
|
'gender_of_host' => 'male', |
|
|
|
|
'num_guests' => 4, |
|
|
|
|
'host' => 'ralph', |
|
|
|
|
'guest' => 'beep' |
|
|
|
|
)); |
|
|
|
|
$this->assertEquals('ralph invites beep and 3 other people to his party.', $result); |
|
|
|
|
) |
|
|
|
|
), |
|
|
|
|
|
|
|
|
|
$pattern = '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'; |
|
|
|
|
$result = MessageFormatter::formatMessage('en_US', $pattern, array( |
|
|
|
|
array( |
|
|
|
|
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!', |
|
|
|
|
'Alexander is male and he loves Yii!', |
|
|
|
|
array( |
|
|
|
|
'name' => 'Alexander', |
|
|
|
|
'gender' => 'male', |
|
|
|
|
)); |
|
|
|
|
$this->assertEquals('Alexander is male and he loves Yii!', $result); |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
|
|
|
|
|
// verify pattern in select does not get replaced |
|
|
|
|
$pattern = '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'; |
|
|
|
|
$result = MessageFormatter::formatMessage('en_US', $pattern, array( |
|
|
|
|
array( |
|
|
|
|
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!', |
|
|
|
|
'Alexander is male and he loves Yii!', |
|
|
|
|
array( |
|
|
|
|
'name' => 'Alexander', |
|
|
|
|
'gender' => 'male', |
|
|
|
|
// following should not be replaced |
|
|
|
|
'he' => 'wtf', |
|
|
|
|
'she' => 'wtf', |
|
|
|
|
'it' => 'wtf', |
|
|
|
|
)); |
|
|
|
|
$this->assertEquals('Alexander is male and he loves Yii!', $result); |
|
|
|
|
) |
|
|
|
|
), |
|
|
|
|
|
|
|
|
|
// verify pattern in select message gets replaced |
|
|
|
|
$pattern = '{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!'; |
|
|
|
|
$result = MessageFormatter::formatMessage('en_US', $pattern, array( |
|
|
|
|
array( |
|
|
|
|
'{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!', |
|
|
|
|
'Alexander is male and wtf loves Yii!', |
|
|
|
|
array( |
|
|
|
|
'name' => 'Alexander', |
|
|
|
|
'gender' => 'male', |
|
|
|
|
'he' => 'wtf', |
|
|
|
|
'she' => 'wtf', |
|
|
|
|
)); |
|
|
|
|
$this->assertEquals('Alexander is male and wtf loves Yii!', $result); |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
|
|
|
|
|
// some parser specific verifications |
|
|
|
|
$pattern = '{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr, number} is {gender}!'; |
|
|
|
|
$result = MessageFormatter::formatMessage('en_US', $pattern, array( |
|
|
|
|
array( |
|
|
|
|
'{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr, number} is {gender}!', |
|
|
|
|
'male and wtf loves 42 is male!', |
|
|
|
|
array( |
|
|
|
|
'nr' => 42, |
|
|
|
|
'gender' => 'male', |
|
|
|
|
'he' => 'wtf', |
|
|
|
|
'she' => 'wtf', |
|
|
|
|
)); |
|
|
|
|
$this->assertEquals('male and wtf loves 42 is male!', $result); |
|
|
|
|
), |
|
|
|
|
), |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dataProvider patterns |
|
|
|
|
*/ |
|
|
|
|
public function testNamedArgumentsStatic($pattern, $expected, $args) |
|
|
|
|
{ |
|
|
|
|
$result = MessageFormatter::formatMessage('en_US', $pattern, $args); |
|
|
|
|
$this->assertEquals($expected, $result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @dataProvider patterns |
|
|
|
|
*/ |
|
|
|
|
public function testNamedArgumentsObject($pattern, $expected, $args) |
|
|
|
|
{ |
|
|
|
|
$formatter = new MessageFormatter('en_US', $pattern); |
|
|
|
|
$result = $formatter->format($args); |
|
|
|
|
$this->assertEquals($expected, $result); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testInsufficientArguments() |
|
|
|
|