mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-07 15:57:35 +01:00
* php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
242 lines
8.5 KiB
PHP
242 lines
8.5 KiB
PHP
<?php
|
|
/**
|
|
* @link http://www.yiiframework.com/
|
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
|
* @license http://www.yiiframework.com/license/
|
|
*/
|
|
|
|
namespace yiiunit\framework\i18n;
|
|
|
|
use yii\i18n\MessageFormatter;
|
|
use yiiunit\TestCase;
|
|
|
|
/**
|
|
* @author Carsten Brandt <mail@cebe.cc>
|
|
* @since 2.0
|
|
* @group i18n
|
|
*/
|
|
class FallbackMessageFormatterTest extends TestCase
|
|
{
|
|
const N = 'n';
|
|
const N_VALUE = 42;
|
|
const F = 'f';
|
|
const F_VALUE = 2e+8;
|
|
const F_VALUE_FORMATTED = '200,000,000';
|
|
const D = 'd';
|
|
const D_VALUE = 200000000.101;
|
|
const D_VALUE_FORMATTED = '200,000,000.101';
|
|
const D_VALUE_FORMATTED_INTEGER = '200,000,000';
|
|
const SUBJECT = 'сабж';
|
|
const SUBJECT_VALUE = 'Answer to the Ultimate Question of Life, the Universe, and Everything';
|
|
|
|
public function patterns()
|
|
{
|
|
return [
|
|
[
|
|
'{' . self::SUBJECT . '} is {' . self::N . '}', // pattern
|
|
self::SUBJECT_VALUE . ' is ' . self::N_VALUE, // expected
|
|
[ // params
|
|
self::N => self::N_VALUE,
|
|
self::SUBJECT => self::SUBJECT_VALUE,
|
|
],
|
|
],
|
|
|
|
[
|
|
'{' . self::SUBJECT . '} is {' . self::N . ', number}', // pattern
|
|
self::SUBJECT_VALUE . ' is ' . self::N_VALUE, // expected
|
|
[ // params
|
|
self::N => self::N_VALUE,
|
|
self::SUBJECT => self::SUBJECT_VALUE,
|
|
],
|
|
],
|
|
|
|
[
|
|
'{' . self::SUBJECT . '} is {' . self::N . ', number, integer}', // pattern
|
|
self::SUBJECT_VALUE . ' is ' . self::N_VALUE, // expected
|
|
[ // params
|
|
self::N => self::N_VALUE,
|
|
self::SUBJECT => self::SUBJECT_VALUE,
|
|
],
|
|
],
|
|
|
|
[
|
|
'Here is a big number: {' . self::F . ', number}', // pattern
|
|
'Here is a big number: ' . self::F_VALUE_FORMATTED, // expected
|
|
[ // params
|
|
self::F => self::F_VALUE,
|
|
],
|
|
],
|
|
|
|
[
|
|
'Here is a big number: {' . self::F . ', number, integer}', // pattern
|
|
'Here is a big number: ' . self::F_VALUE_FORMATTED, // expected
|
|
[ // params
|
|
self::F => self::F_VALUE,
|
|
],
|
|
],
|
|
|
|
[
|
|
'Here is a big number: {' . self::D . ', number}', // pattern
|
|
'Here is a big number: ' . self::D_VALUE_FORMATTED, // expected
|
|
[ // params
|
|
self::D => self::D_VALUE,
|
|
],
|
|
],
|
|
|
|
[
|
|
'Here is a big number: {' . self::D . ', number, integer}', // pattern
|
|
'Here is a big number: ' . self::D_VALUE_FORMATTED_INTEGER, // expected
|
|
[ // params
|
|
self::D => self::D_VALUE,
|
|
],
|
|
],
|
|
|
|
// This one was provided by Aura.Intl. Thanks!
|
|
[<<<_MSG_
|
|
{gender_of_host, select,
|
|
female {{num_guests, plural, offset:1
|
|
=0 {{host} does not give a party.}
|
|
=1 {{host} invites {guest} to her party.}
|
|
=2 {{host} invites {guest} and one other person to her party.}
|
|
other {{host} invites {guest} and # other people to her party.}}}
|
|
male {{num_guests, plural, offset:1
|
|
=0 {{host} does not give a party.}
|
|
=1 {{host} invites {guest} to his party.}
|
|
=2 {{host} invites {guest} and one other person to his party.}
|
|
other {{host} invites {guest} and # other people to his party.}}}
|
|
other {{num_guests, plural, offset:1
|
|
=0 {{host} does not give a party.}
|
|
=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_
|
|
,
|
|
'ralph invites beep and 3 other people to his party.',
|
|
[
|
|
'gender_of_host' => 'male',
|
|
'num_guests' => 4,
|
|
'host' => 'ralph',
|
|
'guest' => 'beep',
|
|
],
|
|
],
|
|
|
|
[
|
|
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!',
|
|
'Alexander is male and he loves Yii!',
|
|
[
|
|
'name' => 'Alexander',
|
|
'gender' => 'male',
|
|
],
|
|
],
|
|
|
|
// verify pattern in select does not get replaced
|
|
[
|
|
'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!',
|
|
'Alexander is male and he loves Yii!',
|
|
[
|
|
'name' => 'Alexander',
|
|
'gender' => 'male',
|
|
// following should not be replaced
|
|
'he' => 'wtf',
|
|
'she' => 'wtf',
|
|
'it' => 'wtf',
|
|
],
|
|
],
|
|
|
|
// verify pattern in select message gets replaced
|
|
[
|
|
'{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!',
|
|
'Alexander is male and wtf loves Yii!',
|
|
[
|
|
'name' => 'Alexander',
|
|
'gender' => 'male',
|
|
'he' => 'wtf',
|
|
'she' => 'wtf',
|
|
],
|
|
],
|
|
|
|
// formatting a message that contains params but they are not provided.
|
|
[
|
|
'Incorrect password (length must be from {min, number} to {max, number} symbols).',
|
|
'Incorrect password (length must be from {min, number} to {max, number} symbols).',
|
|
['attribute' => 'password'],
|
|
],
|
|
|
|
// some parser specific verifications
|
|
[
|
|
'{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr} is {gender}!',
|
|
'male and wtf loves 42 is male!',
|
|
[
|
|
'nr' => 42,
|
|
'gender' => 'male',
|
|
'he' => 'wtf',
|
|
'she' => 'wtf',
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider patterns
|
|
*/
|
|
public function testNamedArguments($pattern, $expected, $args)
|
|
{
|
|
$formatter = new FallbackMessageFormatter();
|
|
$result = $formatter->fallbackFormat($pattern, $args, 'en-US');
|
|
$this->assertEquals($expected, $result, $formatter->getErrorMessage());
|
|
}
|
|
|
|
public function testInsufficientArguments()
|
|
{
|
|
$expected = '{' . self::SUBJECT . '} is ' . self::N_VALUE;
|
|
|
|
$formatter = new FallbackMessageFormatter();
|
|
$result = $formatter->fallbackFormat('{' . self::SUBJECT . '} is {' . self::N . '}', [
|
|
self::N => self::N_VALUE,
|
|
], 'en-US');
|
|
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testNoParams()
|
|
{
|
|
$pattern = '{' . self::SUBJECT . '} is ' . self::N;
|
|
|
|
$formatter = new FallbackMessageFormatter();
|
|
$result = $formatter->fallbackFormat($pattern, [], 'en-US');
|
|
$this->assertEquals($pattern, $result, $formatter->getErrorMessage());
|
|
}
|
|
|
|
public function testGridViewMessage()
|
|
{
|
|
$pattern = 'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.';
|
|
$formatter = new FallbackMessageFormatter();
|
|
$result = $formatter->fallbackFormat($pattern, ['begin' => 1, 'end' => 5, 'totalCount' => 10], 'en-US');
|
|
$this->assertEquals('Showing <b>1-5</b> of <b>10</b> items.', $result);
|
|
}
|
|
|
|
public function testUnsupportedPercentException()
|
|
{
|
|
$pattern = 'Number {' . self::N . ', number, percent}';
|
|
$formatter = new FallbackMessageFormatter();
|
|
$this->expectException('yii\base\NotSupportedException');
|
|
$formatter->fallbackFormat($pattern, [self::N => self::N_VALUE], 'en-US');
|
|
}
|
|
|
|
public function testUnsupportedCurrencyException()
|
|
{
|
|
$pattern = 'Number {' . self::N . ', number, currency}';
|
|
$formatter = new FallbackMessageFormatter();
|
|
$this->expectException('yii\base\NotSupportedException');
|
|
$formatter->fallbackFormat($pattern, [self::N => self::N_VALUE], 'en-US');
|
|
}
|
|
}
|
|
|
|
class FallbackMessageFormatter extends MessageFormatter
|
|
{
|
|
public function fallbackFormat($pattern, $args, $locale)
|
|
{
|
|
return parent::fallbackFormat($pattern, $args, $locale);
|
|
}
|
|
}
|