|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Translation\Tests\Formatter; |
| 13 | + |
| 14 | +use Symfony\Component\Translation\Formatter\IntlMessageFormatter; |
| 15 | + |
| 16 | +class IntlMessageFormatterTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + public function setUp() |
| 19 | + { |
| 20 | + if (!extension_loaded('intl')) { |
| 21 | + $this->markTestSkipped( |
| 22 | + 'The Intl extension is not available.' |
| 23 | + ); |
| 24 | +
A3D4
} |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @dataProvider provideDataForFormat |
| 29 | + */ |
| 30 | + public function testFormat($expected, $message, $arguments) |
| 31 | + { |
| 32 | + $this->assertEquals($expected, trim($this->getMessageFormatter()->format($message, 'en', $arguments))); |
| 33 | + } |
| 34 | + |
| 35 | + public function testFormatWithNamedArguments() |
| 36 | + { |
| 37 | + if (PHP_VERSION_ID < 50500 || version_compare(INTL_ICU_VERSION, '4.8', '<')) { |
| 38 | + $this->markTestSkipped('Format with named arguments can only be run with ICU 4.8 or higher and PHP >= 5.5'); |
| 39 | + } |
| 40 | + |
| 41 | + $chooseMessage = <<<'_MSG_' |
| 42 | +{gender_of_host, select, |
| 43 | + female {{num_guests, plural, offset:1 |
| 44 | + =0 {{host} does not give a party.} |
| 45 | + =1 {{host} invites {guest} to her party.} |
| 46 | + =2 {{host} invites {guest} and one other person to her party.} |
| 47 | + other {{host} invites {guest} as one of the # people invited to her party.}}} |
| 48 | + male {{num_guests, plural, offset:1 |
| 49 | + =0 {{host} does not give a party.} |
| 50 | + =1 {{host} invites {guest} to his party.} |
| 51 | + =2 {{host} invites {guest} and one other person to his party.} |
| 52 | + other {{host} invites {guest} as one of the # people invited to his party.}}} |
| 53 | + other {{num_guests, plural, offset:1 |
| 54 | + =0 {{host} does not give a party.} |
| 55 | + =1 {{host} invites {guest} to their party.} |
| 56 | + =2 {{host} invites {guest} and one other person to their party.} |
| 57 | + other {{host} invites {guest} as one of the # people invited to their party.}}}} |
| 58 | +_MSG_; |
| 59 | + |
| 60 | + $formatter = $this->getMessageFormatter(); |
| 61 | + $message = $formatter->format($chooseMessage, 'en', array( |
| 62 | + 'gender_of_host' => 'male', |
| 63 | + 'num_guests' => 10, |
| 64 | + 'host' => 'Fabien', |
| 65 | + 'guest' => 'Guilherme', |
| 66 | + )); |
| 67 | + |
| 68 | + $this->assertEquals('Fabien invites Guilherme as one of the 9 people invited to his party.', $message); |
| 69 | + } |
| 70 | + |
| 71 | + public function provideDataForFormat() |
| 72 | + { |
| 73 | + return array( |
| 74 | + array( |
| 75 | + 'There is one apple', |
| 76 | + 'There is one apple', |
| 77 | + array(), |
| 78 | + ), |
| 79 | + array( |
| 80 | + '4,560 monkeys on 123 trees make 37.073 monkeys per tree', |
| 81 | + '{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree', |
| 82 | + array(4560, 123, 4560 / 123), |
| 83 | + ), |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + private function getMessageFormatter() |
| 88 | + { |
| 89 | + return new IntlMessageFormatter(); |
| 90 | + } |
| 91 | +} |
0 commit comments