提交代码
This commit is contained in:
101
vendor/symfony/translation/Tests/Formatter/IntlFormatterTest.php
vendored
Normal file
101
vendor/symfony/translation/Tests/Formatter/IntlFormatterTest.php
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Translation\Tests\Formatter;
|
||||
|
||||
use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Translation\Formatter\IntlFormatter;
|
||||
use Symfony\Component\Translation\Formatter\IntlFormatterInterface;
|
||||
|
||||
/**
|
||||
* @requires extension intl
|
||||
*/
|
||||
class IntlFormatterTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideDataForFormat
|
||||
*/
|
||||
public function testFormat($expected, $message, $arguments)
|
||||
{
|
||||
$this->assertEquals($expected, trim((new IntlFormatter())->formatIntl($message, 'en', $arguments)));
|
||||
}
|
||||
|
||||
public function testInvalidFormat()
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
(new IntlFormatter())->formatIntl('{foo', 'en', [2]);
|
||||
}
|
||||
|
||||
public function testFormatWithNamedArguments()
|
||||
{
|
||||
if (version_compare(INTL_ICU_VERSION, '4.8', '<')) {
|
||||
$this->markTestSkipped('Format with named arguments can only be run with ICU 4.8 or higher and PHP >= 5.5');
|
||||
}
|
||||
|
||||
$chooseMessage = <<<'_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} as one of the # people invited 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} as one of the # people invited 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} as one of the # people invited to their party.}}}}
|
||||
_MSG_;
|
||||
|
||||
$message = (new IntlFormatter())->formatIntl($chooseMessage, 'en', [
|
||||
'gender_of_host' => 'male',
|
||||
'num_guests' => 10,
|
||||
'host' => 'Fabien',
|
||||
'guest' => 'Guilherme',
|
||||
]);
|
||||
|
||||
$this->assertEquals('Fabien invites Guilherme as one of the 9 people invited to his party.', $message);
|
||||
}
|
||||
|
||||
public function provideDataForFormat()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'There is one apple',
|
||||
'There is one apple',
|
||||
[],
|
||||
],
|
||||
[
|
||||
'4,560 monkeys on 123 trees make 37.073 monkeys per tree',
|
||||
'{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree',
|
||||
[4560, 123, 4560 / 123],
|
||||
],
|
||||
[
|
||||
'',
|
||||
'',
|
||||
[],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function testPercentsAndBracketsAreTrimmed()
|
||||
{
|
||||
$formatter = new IntlFormatter();
|
||||
$this->assertInstanceof(IntlFormatterInterface::class, $formatter);
|
||||
$this->assertSame('Hello Fab', $formatter->formatIntl('Hello {name}', 'en', ['name' => 'Fab']));
|
||||
$this->assertSame('Hello Fab', $formatter->formatIntl('Hello {name}', 'en', ['%name%' => 'Fab']));
|
||||
$this->assertSame('Hello Fab', $formatter->formatIntl('Hello {name}', 'en', ['{{ name }}' => 'Fab']));
|
||||
}
|
||||
}
|
||||
83
vendor/symfony/translation/Tests/Formatter/MessageFormatterTest.php
vendored
Normal file
83
vendor/symfony/translation/Tests/Formatter/MessageFormatterTest.php
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\Translation\Tests\Formatter;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Translation\Formatter\MessageFormatter;
|
||||
|
||||
class MessageFormatterTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getTransMessages
|
||||
*/
|
||||
public function testFormat($expected, $message, $parameters = [])
|
||||
{
|
||||
$this->assertEquals($expected, $this->getMessageFormatter()->format($message, 'en', $parameters));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getTransChoiceMessages
|
||||
* @group legacy
|
||||
*/
|
||||
public function testFormatPlural($expected, $message, $number, $parameters)
|
||||
{
|
||||
$this->assertEquals($expected, $this->getMessageFormatter()->choiceFormat($message, $number, 'fr', $parameters));
|
||||
}
|
||||
|
||||
public function getTransMessages()
|
||||
{
|
||||
return [
|
||||
[
|
||||
'There is one apple',
|
||||
'There is one apple',
|
||||
],
|
||||
[
|
||||
'There are 5 apples',
|
||||
'There are %count% apples',
|
||||
['%count%' => 5],
|
||||
],
|
||||
[
|
||||
'There are 5 apples',
|
||||
'There are {{count}} apples',
|
||||
['{{count}}' => 5],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function getTransChoiceMessages()
|
||||
{
|
||||
return [
|
||||
['Il y a 0 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, ['%count%' => 0]],
|
||||
['Il y a 1 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 1, ['%count%' => 1]],
|
||||
['Il y a 10 pommes', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 10, ['%count%' => 10]],
|
||||
|
||||
['Il y a 0 pomme', 'Il y a %count% pomme|Il y a %count% pommes', 0, ['%count%' => 0]],
|
||||
['Il y a 1 pomme', 'Il y a %count% pomme|Il y a %count% pommes', 1, ['%count%' => 1]],
|
||||
['Il y a 10 pommes', 'Il y a %count% pomme|Il y a %count% pommes', 10, ['%count%' => 10]],
|
||||
|
||||
['Il y a 0 pomme', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 0, ['%count%' => 0]],
|
||||
['Il y a 1 pomme', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 1, ['%count%' => 1]],
|
||||
['Il y a 10 pommes', 'one: Il y a %count% pomme|more: Il y a %count% pommes', 10, ['%count%' => 10]],
|
||||
|
||||
['Il n\'y a aucune pomme', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 0, ['%count%' => 0]],
|
||||
['Il y a 1 pomme', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 1, ['%count%' => 1]],
|
||||
['Il y a 10 pommes', '{0} Il n\'y a aucune pomme|one: Il y a %count% pomme|more: Il y a %count% pommes', 10, ['%count%' => 10]],
|
||||
|
||||
['Il y a 0 pomme', '[0,1] Il y a %count% pomme|]1,Inf] Il y a %count% pommes', 0, ['%count%' => 0]],
|
||||
];
|
||||
}
|
||||
|
||||
private function getMessageFormatter()
|
||||
{
|
||||
return new MessageFormatter();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user