2010-10-31 21:33:08 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-10-31 21:33:08 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-28 14:43:52 +01:00
namespace Symfony\Component\Translation\Tests ;
2010-10-31 21:33:08 +00:00
use Symfony\Component\Translation\MessageSelector ;
class MessageSelectorTest extends \PHPUnit_Framework_TestCase
{
/**
* @ dataProvider getChooseTests
*/
public function testChoose ( $expected , $id , $number )
{
$selector = new MessageSelector ();
$this -> assertEquals ( $expected , $selector -> choose ( $id , $number , 'en' ));
}
2013-08-23 16:24:14 +01:00
public function testReturnMessageIfExactlyOneStandardRuleIsGiven ()
{
$selector = new MessageSelector ();
$this -> assertEquals ( 'There are two apples' , $selector -> choose ( 'There are two apples' , 2 , 'en' ));
}
2010-10-31 21:33:08 +00:00
/**
2013-08-23 16:24:14 +01:00
* @ dataProvider getNonMatchingMessages
* @ expectedException \InvalidArgumentException
2010-10-31 21:33:08 +00:00
*/
2013-08-23 16:24:14 +01:00
public function testThrowExceptionIfMatchingMessageCannotBeFound ( $id , $number )
2010-10-31 21:33:08 +00:00
{
$selector = new MessageSelector ();
2013-08-23 16:24:14 +01:00
$selector -> choose ( $id , $number , 'en' );
}
public function getNonMatchingMessages ()
{
return array (
2013-08-24 13:29:44 +01:00
array ( '{0} There are no apples|{1} There is one apple' , 2 ),
2013-08-23 16:24:14 +01:00
array ( '{1} There is one apple|]1,Inf] There are %count% apples' , 0 ),
array ( '{1} There is one apple|]2,Inf] There are %count% apples' , 2 ),
2013-08-24 13:29:44 +01:00
array ( '{0} There are no apples|There is one apple' , 2 ),
2013-08-23 16:24:14 +01:00
);
2010-10-31 21:33:08 +00:00
}
public function getChooseTests ()
{
return array (
2013-08-24 13:29:44 +01:00
array ( 'There are no apples' , '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples' , 0 ),
array ( 'There are no apples' , '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples' , 0 ),
array ( 'There are no apples' , '{0}There are no apples|{1} There is one apple|]1,Inf] There are %count% apples' , 0 ),
2011-09-14 21:04:39 +01:00
2013-08-24 13:29:44 +01:00
array ( 'There is one apple' , '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples' , 1 ),
2011-09-14 21:04:39 +01:00
2013-08-24 13:29:44 +01:00
array ( 'There are %count% apples' , '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples' , 10 ),
array ( 'There are %count% apples' , '{0} There are no apples|{1} There is one apple|]1,Inf]There are %count% apples' , 10 ),
array ( 'There are %count% apples' , '{0} There are no apples|{1} There is one apple|]1,Inf] There are %count% apples' , 10 ),
2010-10-31 21:33:08 +00:00
2013-08-23 16:24:14 +01:00
array ( 'There are %count% apples' , 'There is one apple|There are %count% apples' , 0 ),
array ( 'There is one apple' , 'There is one apple|There are %count% apples' , 1 ),
array ( 'There are %count% apples' , 'There is one apple|There are %count% apples' , 10 ),
2010-10-31 21:33:08 +00:00
2013-08-23 16:24:14 +01:00
array ( 'There are %count% apples' , 'one: There is one apple|more: There are %count% apples' , 0 ),
array ( 'There is one apple' , 'one: There is one apple|more: There are %count% apples' , 1 ),
array ( 'There are %count% apples' , 'one: There is one apple|more: There are %count% apples' , 10 ),
2010-10-31 21:33:08 +00:00
2013-08-24 13:29:44 +01:00
array ( 'There are no apples' , '{0} There are no apples|one: There is one apple|more: There are %count% apples' , 0 ),
array ( 'There is one apple' , '{0} There are no apples|one: There is one apple|more: There are %count% apples' , 1 ),
array ( 'There are %count% apples' , '{0} There are no apples|one: There is one apple|more: There are %count% apples' , 10 ),
2011-09-14 21:04:39 +01:00
2013-08-23 16:24:14 +01:00
array ( '' , '{0}|{1} There is one apple|]1,Inf] There are %count% apples' , 0 ),
2013-08-24 13:29:44 +01:00
array ( '' , '{0} There are no apples|{1}|]1,Inf] There are %count% apples' , 1 ),
2012-05-19 10:54:03 +01:00
// Indexed only tests which are Gettext PoFile* compatible strings.
array ( 'There are %count% apples' , 'There is one apple|There are %count% apples' , 0 ),
array ( 'There is one apple' , 'There is one apple|There are %count% apples' , 1 ),
array ( 'There are %count% apples' , 'There is one apple|There are %count% apples' , 2 ),
2012-11-01 22:19:52 +00:00
// Tests for float numbers
2013-08-24 13:29:44 +01:00
array ( 'There is almost one apple' , '{0} There are no apples|]0,1[ There is almost one apple|{1} There is one apple|[1,Inf] There is more than one apple' , 0.7 ),
array ( 'There is one apple' , '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple' , 1 ),
array ( 'There is more than one apple' , '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple' , 1.7 ),
array ( 'There are no apples' , '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple' , 0 ),
array ( 'There are no apples' , '{0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple' , 0.0 ),
array ( 'There are no apples' , '{0.0} There are no apples|]0,1[There are %count% apples|{1} There is one apple|[1,Inf] There is more than one apple' , 0 ),
2010-10-31 21:33:08 +00:00
);
}
}