MessageFormatterTest.php 9.04 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?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 Alexander Makarov <sam@rmcreative.ru>
 * @since 2.0
 * @group i18n
 */
class MessageFormatterTest extends TestCase
{
	const N = 'n';
	const N_VALUE = 42;
	const SUBJECT = 'сабж';
	const SUBJECT_VALUE = 'Answer to the Ultimate Question of Life, the Universe, and Everything';

25
	public function patterns()
26
	{
Alexander Makarov committed
27 28
		return [
			[
29 30
				'{'.self::SUBJECT.'} is {'.self::N.', number}', // pattern
				self::SUBJECT_VALUE.' is '.self::N_VALUE, // expected
Alexander Makarov committed
31
				[ // params
32 33
					self::N => self::N_VALUE,
					self::SUBJECT => self::SUBJECT_VALUE,
Alexander Makarov committed
34 35
				]
			],
36

37 38 39 40 41 42 43 44 45
			[
				'{'.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,
				]
			],

46
			// This one was provided by Aura.Intl. Thanks!
Alexander Makarov committed
47
			[<<<_MSG_
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
{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.}}}}
64 65 66
_MSG_
				,
				'ralph invites beep and 3 other people to his party.',
Alexander Makarov committed
67
				[
68 69 70 71
					'gender_of_host' => 'male',
					'num_guests' => 4,
					'host' => 'ralph',
					'guest' => 'beep'
72 73 74
				],
				defined('INTL_ICU_VERSION') && version_compare(INTL_ICU_VERSION, '4.8', '<'),
				'select format is available in ICU > 4.4 and plural format with =X selector is avilable since 4.8'
Alexander Makarov committed
75
			],
76

Alexander Makarov committed
77
			[
78 79
				'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!',
				'Alexander is male and he loves Yii!',
Alexander Makarov committed
80
				[
81 82
					'name' => 'Alexander',
					'gender' => 'male',
Alexander Makarov committed
83
				],
84 85
				defined('INTL_ICU_VERSION') && version_compare(INTL_ICU_VERSION, '4.4.2', '<'),
				'select format is available in ICU > 4.4'
Alexander Makarov committed
86
			],
87

88
			// verify pattern in select does not get replaced
Alexander Makarov committed
89
			[
90 91
				'{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!',
				'Alexander is male and he loves Yii!',
Alexander Makarov committed
92
				[
93 94 95 96 97 98
					'name' => 'Alexander',
					'gender' => 'male',
					 // following should not be replaced
					'he' => 'wtf',
					'she' => 'wtf',
					'it' => 'wtf',
99 100 101
				],
				defined('INTL_ICU_VERSION') && version_compare(INTL_ICU_VERSION, '4.4.2', '<'),
				'select format is available in ICU > 4.4'
Alexander Makarov committed
102
			],
103

104
			// verify pattern in select message gets replaced
Alexander Makarov committed
105
			[
106 107
				'{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!',
				'Alexander is male and wtf loves Yii!',
Alexander Makarov committed
108
				[
109 110 111 112
					'name' => 'Alexander',
					'gender' => 'male',
					'he' => 'wtf',
					'she' => 'wtf',
Alexander Makarov committed
113
				],
114 115
				defined('INTL_ICU_VERSION') && version_compare(INTL_ICU_VERSION, '4.8', '<'),
				'parameters in select format do not seem to work in ICU < 4.8'
Alexander Makarov committed
116
			],
117

118
			// some parser specific verifications
Alexander Makarov committed
119
			[
120 121
				'{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr, number} is {gender}!',
				'male and wtf loves 42 is male!',
Alexander Makarov committed
122
				[
123 124 125 126
					'nr' => 42,
					'gender' => 'male',
					'he' => 'wtf',
					'she' => 'wtf',
Alexander Makarov committed
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
				defined('INTL_ICU_VERSION') && version_compare(INTL_ICU_VERSION, '4.4.2', '<'),
				'select format is available in ICU > 4.4'
			],

			// test ICU version compatibility
			[
				'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.',
				'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.',
				[],
			],
			[
				'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.',
				'Showing <b>1-10</b> of <b>12</b> items.',
				[// A
					'begin' => 1,
					'end' => 10,
					'count' => 10,
					'totalCount' => 12,
					'page' => 1,
					'pageCount' => 2,
				]
			],
			[
				'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.',
				'Showing <b>1-1</b> of <b>1</b> item.',
				[// B
					'begin' => 1,
					'end' => 1,
					'count' => 1,
					'totalCount' => 1,
					'page' => 1,
					'pageCount' => 1,
				]
			],
			[
				'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.',
				'Showing <b>0-0</b> of <b>0</b> items.',
				[// C
					'begin' => 0,
					'end' => 0,
					'count' => 0,
					'totalCount' => 0,
					'page' => 1,
					'pageCount' => 1,
				]
			],
			[
				'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.',
				'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.',
				[]
			],
			[
				'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.',
				'Total <b>1</b> item.',
				[
					'count' => 1,
				]
			],
			[
				'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.',
				'Total <b>1</b> item.',
				[
					'begin' => 5,
					'count' => 1,
					'end' => 10,
				]
			],
			[
				'{0, plural, one {offer} other {offers}}',
				'{0, plural, one {offer} other {offers}}',
				[],
			],
			[
				'{0, plural, one {offer} other {offers}}',
				'offers',
				[0],
			],
			[
				'{0, plural, one {offer} other {offers}}',
				'offer',
				[1],
			],
			[
				'{0, plural, one {offer} other {offers}}',
				'offers',
				[13],
Alexander Makarov committed
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
	public function parsePatterns()
	{
		return [
			[
				self::SUBJECT_VALUE.' is {0, number}', // pattern
				self::SUBJECT_VALUE.' is '.self::N_VALUE, // expected
				[ // params
					0 => self::N_VALUE,
				]
			],

			[
				self::SUBJECT_VALUE.' is {'.self::N.', number}', // pattern
				self::SUBJECT_VALUE.' is '.self::N_VALUE, // expected
				[ // params
					self::N => self::N_VALUE,
				]
			],

			[
				self::SUBJECT_VALUE.' is {'.self::N.', number, integer}', // pattern
				self::SUBJECT_VALUE.' is '.self::N_VALUE, // expected
				[ // params
					self::N => self::N_VALUE,
				]
			],

			[
				"{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree",
				"4,560 monkeys on 123 trees make 37.073 monkeys per tree",
				[
					0 => 4560,
					1 => 123,
					2 => 37.073
				],
253
				'en-US'
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
			],

			[
				"{0,number,integer} Affen auf {1,number,integer} Bäumen sind {2,number} Affen pro Baum",
				"4.560 Affen auf 123 Bäumen sind 37,073 Affen pro Baum",
				[
					0 => 4560,
					1 => 123,
					2 => 37.073
				],
				'de',
			],

			[
				"{monkeyCount,number,integer} monkeys on {trees,number,integer} trees make {monkeysPerTree,number} monkeys per tree",
				"4,560 monkeys on 123 trees make 37.073 monkeys per tree",
				[
					'monkeyCount' => 4560,
					'trees' => 123,
					'monkeysPerTree' => 37.073
				],
275
				'en-US'
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
			],

			[
				"{monkeyCount,number,integer} Affen auf {trees,number,integer} Bäumen sind {monkeysPerTree,number} Affen pro Baum",
				"4.560 Affen auf 123 Bäumen sind 37,073 Affen pro Baum",
				[
					'monkeyCount' => 4560,
					'trees' => 123,
					'monkeysPerTree' => 37.073
				],
				'de',
			],
		];
	}

291 292 293
	/**
	 * @dataProvider patterns
	 */
294
	public function testNamedArguments($pattern, $expected, $args, $skip = false, $skipMessage = '')
295
	{
296 297 298
		if ($skip) {
			$this->markTestSkipped($skipMessage);
		}
299
		$formatter = new MessageFormatter();
300
		$result = $formatter->format($pattern, $args, 'en-US');
301
		$this->assertEquals($expected, $result, $formatter->getErrorMessage());
302 303
	}

304 305 306
	/**
	 * @dataProvider parsePatterns
	 */
307
	public function testParseNamedArguments($pattern, $expected, $args, $locale = 'en-US')
308
	{
309 310 311 312
		if (!extension_loaded("intl")) {
			$this->markTestSkipped("intl not installed. Skipping.");
		}

313 314 315 316 317
		$formatter = new MessageFormatter();
		$result = $formatter->parse($pattern, $expected, $locale);
		$this->assertEquals($args, $result, $formatter->getErrorMessage() . ' Pattern: ' . $pattern);
	}

318 319 320 321
	public function testInsufficientArguments()
	{
		$expected = '{'.self::SUBJECT.'} is '.self::N_VALUE;

322
		$formatter = new MessageFormatter();
323
		$result = $formatter->format('{'.self::SUBJECT.'} is {'.self::N.', number}', [
324
			self::N => self::N_VALUE,
325
		], 'en-US');
326

327
		$this->assertEquals($expected, $result, $formatter->getErrorMessage());
328 329 330 331 332
	}

	public function testNoParams()
	{
		$pattern = '{'.self::SUBJECT.'} is '.self::N;
333
		$formatter = new MessageFormatter();
334
		$result = $formatter->format($pattern, [], 'en-US');
335
		$this->assertEquals($pattern, $result, $formatter->getErrorMessage());
336
	}
337
}