2011-02-05 20:29:40 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-14 00:22:11 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-02-05 20:29:40 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Tests\Component\Locale\Stub ;
2011-02-17 08:42:30 +00:00
require_once __DIR__ . '/../TestCase.php' ;
2011-02-05 20:29:40 +00:00
use Symfony\Component\Locale\Locale ;
2011-05-19 00:15:37 +01:00
use Symfony\Component\Locale\Stub\StubIntl ;
2011-02-05 20:29:40 +00:00
use Symfony\Component\Locale\Stub\StubIntlDateFormatter ;
2011-02-17 08:42:30 +00:00
use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase ;
2011-02-05 20:29:40 +00:00
2011-02-17 08:42:30 +00:00
class StubIntlDateFormatterTest extends LocaleTestCase
2011-02-05 20:29:40 +00:00
{
2011-02-10 22:22:08 +00:00
/**
2011-02-18 09:37:37 +00:00
* @ expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
2011-02-10 22:22:08 +00:00
*/
public function testConstructorWithUnsupportedLocale ()
{
$formatter = new StubIntlDateFormatter ( 'pt_BR' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT );
}
public function testConstructor ()
{
2011-02-28 12:31:06 +00:00
$formatter = new StubIntlDateFormatter ( 'en' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT , 'UTC' , StubIntlDateFormatter :: GREGORIAN , 'y-M-d' );
$this -> assertEquals ( 'y-M-d' , $formatter -> getPattern ());
2011-02-10 22:22:08 +00:00
}
/**
2011-03-07 02:46:23 +00:00
* When a time zone is not specified , it uses the system default however it returns null in the getter method
* @ covers Symfony\Component\Locale\Stub\StubIntlDateFormatter :: getTimeZoneId
* @ covers Symfony\Component\Locale\Stub\StubIntlDateFormatter :: setTimeZoneId
* @ see StubIntlDateFormatterTest :: testDefaultTimeZoneIntl ()
*/
public function testConstructorDefaultTimeZoneStub ()
{
$formatter = new StubIntlDateFormatter ( 'en' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT );
$this -> assertNull ( $formatter -> getTimeZoneId ());
}
public function testConstructorDefaultTimeZoneIntl ()
{
$this -> skipIfIntlExtensionIsNotLoaded ();
$formatter = new \IntlDateFormatter ( 'en' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT );
$this -> assertNull ( $formatter -> getTimeZoneId ());
}
2012-04-15 19:32:40 +01:00
public function testFormatWithUnsupportedTimestampArgument ()
{
$formatter = $this -> createStubFormatter ();
$localtime = array (
'tm_sec' => 59 ,
'tm_min' => 3 ,
'tm_hour' => 15 ,
'tm_mday' => 15 ,
'tm_mon' => 3 ,
'tm_year' => 112 ,
'tm_wday' => 0 ,
'tm_yday' => 105 ,
'tm_isdst' => 0
);
try {
$formatter -> format ( $localtime );
} catch ( \Exception $e ) {
$this -> assertInstanceOf ( 'Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException' , $e );
if ( $this -> isGreaterOrEqualThanPhpVersion ( '5.3.4' )) {
$this -> assertStringEndsWith ( 'Only integer unix timestamps and DateTime objects are supported. Please install the \'intl\' extension for full localization capabilities.' , $e -> getMessage ());
} else {
$this -> assertStringEndsWith ( 'Only integer unix timestamps are supported. Please install the \'intl\' extension for full localization capabilities.' , $e -> getMessage ());
}
}
}
2011-03-07 02:46:23 +00:00
/**
* @ dataProvider formatProvider
*/
2012-04-16 00:01:42 +01:00
public function testFormatStub ( $pattern , $timestamp , $expected )
2011-02-10 22:22:08 +00:00
{
2012-04-16 00:01:42 +01:00
$errorCode = StubIntl :: U_ZERO_ERROR ;
$errorMessage = 'U_ZERO_ERROR' ;
2011-02-23 18:08:06 +00:00
$formatter = $this -> createStubFormatter ( $pattern );
2011-02-18 09:37:37 +00:00
$this -> assertSame ( $expected , $formatter -> format ( $timestamp ));
2011-05-19 00:15:37 +01:00
$this -> assertSame ( $errorMessage , StubIntl :: getErrorMessage ());
$this -> assertSame ( $errorCode , StubIntl :: getErrorCode ());
2012-04-16 00:23:02 +01:00
$this -> assertFalse ( StubIntl :: isFailure ( StubIntl :: getErrorCode ()));
2012-04-14 21:11:17 +01:00
$this -> assertSame ( $errorMessage , $formatter -> getErrorMessage ());
$this -> assertSame ( $errorCode , $formatter -> getErrorCode ());
2012-04-16 00:23:02 +01:00
$this -> assertFalse ( StubIntl :: isFailure ( $formatter -> getErrorCode ()));
2011-02-18 09:37:37 +00:00
}
2011-02-10 22:22:08 +00:00
2011-02-18 09:37:37 +00:00
/**
2011-05-25 00:08:09 +01:00
* @ dataProvider formatProvider
*/
2012-04-16 00:01:42 +01:00
public function testFormatIntl ( $pattern , $timestamp , $expected )
2011-02-18 09:37:37 +00:00
{
$this -> skipIfIntlExtensionIsNotLoaded ();
2011-04-02 14:17:16 +01:00
$this -> skipIfICUVersionIsTooOld ();
2012-04-16 00:01:42 +01:00
$errorCode = StubIntl :: U_ZERO_ERROR ;
$errorMessage = 'U_ZERO_ERROR' ;
2011-02-23 18:08:06 +00:00
$formatter = $this -> createIntlFormatter ( $pattern );
2011-02-18 09:37:37 +00:00
$this -> assertSame ( $expected , $formatter -> format ( $timestamp ));
2011-05-19 00:15:37 +01:00
$this -> assertSame ( $errorMessage , intl_get_error_message ());
$this -> assertSame ( $errorCode , intl_get_error_code ());
2012-04-16 00:23:02 +01:00
$this -> assertFalse ( intl_is_failure ( intl_get_error_code ()));
2011-02-10 22:22:08 +00:00
}
2011-02-05 20:29:40 +00:00
public function formatProvider ()
{
2011-02-18 21:44:27 +00:00
$formatData = array (
2011-02-08 17:56:43 +00:00
/* general */
2011-02-05 20:29:40 +00:00
array ( 'y-M-d' , 0 , '1970-1-1' ),
2011-02-28 12:25:21 +00:00
array ( " yyyy.MM.dd 'at' HH:mm:ss zzz " , 0 , '1970.01.01 at 00:00:00 GMT+00:00' ),
2011-02-17 08:46:11 +00:00
array ( " EEE, MMM d, ''yy " , 0 , " Thu, Jan 1, '70 " ),
2011-02-09 07:56:49 +00:00
array ( 'h:mm a' , 0 , '12:00 AM' ),
array ( 'K:mm a, z' , 0 , '0:00 AM, GMT+00:00' ),
2011-02-28 12:25:21 +00:00
array ( 'yyyyy.MMMM.dd hh:mm aaa' , 0 , '01970.January.01 12:00 AM' ),
2011-02-05 20:29:40 +00:00
2011-02-06 15:57:45 +00:00
/* escaping */
2011-02-08 17:56:43 +00:00
array ( " 'M' " , 0 , 'M' ),
array ( " 'yy' " , 0 , 'yy' ),
array ( " '''yy' " , 0 , " 'yy " ),
2011-02-06 15:57:45 +00:00
array ( " ''y " , 0 , " '1970 " ),
array ( " ''yy " , 0 , " '70 " ),
2011-02-15 14:13:45 +00:00
array ( " H 'o'' clock' " , 0 , " 0 o' clock " ),
2011-02-06 15:57:45 +00:00
2011-02-06 19:21:41 +00:00
/* month */
2011-02-05 20:29:40 +00:00
array ( 'M' , 0 , '1' ),
array ( 'MM' , 0 , '01' ),
array ( 'MMM' , 0 , 'Jan' ),
array ( 'MMMM' , 0 , 'January' ),
array ( 'MMMMM' , 0 , 'J' ),
2011-02-06 19:21:41 +00:00
array ( 'MMMMMM' , 0 , '000001' ),
2011-02-05 20:29:40 +00:00
2011-02-06 18:00:29 +00:00
array ( 'L' , 0 , '1' ),
array ( 'LL' , 0 , '01' ),
array ( 'LLL' , 0 , 'Jan' ),
array ( 'LLLL' , 0 , 'January' ),
array ( 'LLLLL' , 0 , 'J' ),
2011-02-06 19:21:41 +00:00
array ( 'LLLLLL' , 0 , '000001' ),
2011-02-06 18:00:29 +00:00
2011-02-06 19:21:41 +00:00
/* year */
2011-02-05 20:29:40 +00:00
array ( 'y' , 0 , '1970' ),
array ( 'yy' , 0 , '70' ),
array ( 'yyy' , 0 , '1970' ),
array ( 'yyyy' , 0 , '1970' ),
array ( 'yyyyy' , 0 , '01970' ),
array ( 'yyyyyy' , 0 , '001970' ),
/* day */
array ( 'd' , 0 , '1' ),
array ( 'dd' , 0 , '01' ),
array ( 'ddd' , 0 , '001' ),
2011-02-06 17:53:20 +00:00
/* quarter */
array ( 'Q' , 0 , '1' ),
array ( 'QQ' , 0 , '01' ),
array ( 'QQQ' , 0 , 'Q1' ),
array ( 'QQQQ' , 0 , '1st quarter' ),
array ( 'QQQQQ' , 0 , '1st quarter' ),
array ( 'q' , 0 , '1' ),
array ( 'qq' , 0 , '01' ),
array ( 'qqq' , 0 , 'Q1' ),
array ( 'qqqq' , 0 , '1st quarter' ),
array ( 'qqqqq' , 0 , '1st quarter' ),
2011-02-06 19:30:29 +00:00
// 4 months
2011-02-06 17:53:20 +00:00
array ( 'Q' , 7776000 , '2' ),
array ( 'QQ' , 7776000 , '02' ),
array ( 'QQQ' , 7776000 , 'Q2' ),
array ( 'QQQQ' , 7776000 , '2nd quarter' ),
2011-02-06 19:30:29 +00:00
// 7 months
2011-02-06 17:53:20 +00:00
array ( 'QQQQ' , 15638400 , '3rd quarter' ),
2011-02-06 19:30:29 +00:00
// 10 months
2011-02-06 17:53:20 +00:00
array ( 'QQQQ' , 23587200 , '4th quarter' ),
2011-02-06 19:21:41 +00:00
2011-02-07 11:13:36 +00:00
/* 12-hour (1-12) */
2011-02-06 19:21:41 +00:00
array ( 'h' , 0 , '12' ),
array ( 'hh' , 0 , '12' ),
array ( 'hhh' , 0 , '012' ),
array ( 'h' , 1 , '12' ),
array ( 'h' , 3600 , '1' ),
2011-02-06 20:06:48 +00:00
array ( 'h' , 43200 , '12' ), // 12 hours
2011-02-06 19:30:29 +00:00
/* day of year */
array ( 'D' , 0 , '1' ),
array ( 'D' , 86400 , '2' ), // 1 day
array ( 'D' , 31536000 , '1' ), // 1 year
array ( 'D' , 31622400 , '2' ), // 1 year + 1 day
2011-02-06 19:42:30 +00:00
/* day of week */
array ( 'E' , 0 , 'Thu' ),
array ( 'EE' , 0 , 'Thu' ),
array ( 'EEE' , 0 , 'Thu' ),
array ( 'EEEE' , 0 , 'Thursday' ),
array ( 'EEEEE' , 0 , 'T' ),
array ( 'EEEEEE' , 0 , 'Thu' ),
2011-02-06 20:00:22 +00:00
array ( 'E' , 1296540000 , 'Tue' ), // 2011-02-01
array ( 'E' , 1296950400 , 'Sun' ), // 2011-02-06
/* am/pm marker */
array ( 'a' , 0 , 'AM' ),
array ( 'aa' , 0 , 'AM' ),
array ( 'aaa' , 0 , 'AM' ),
array ( 'aaaa' , 0 , 'AM' ),
// 12 hours
array ( 'a' , 43200 , 'PM' ),
array ( 'aa' , 43200 , 'PM' ),
array ( 'aaa' , 43200 , 'PM' ),
array ( 'aaaa' , 43200 , 'PM' ),
2011-02-06 20:06:48 +00:00
2011-02-07 11:07:41 +00:00
/* 24-hour (0-23) */
2011-02-06 20:06:48 +00:00
array ( 'H' , 0 , '0' ),
array ( 'HH' , 0 , '00' ),
array ( 'HHH' , 0 , '000' ),
array ( 'H' , 1 , '0' ),
array ( 'H' , 3600 , '1' ),
array ( 'H' , 43200 , '12' ),
array ( 'H' , 46800 , '13' ),
2011-02-07 11:07:41 +00:00
/* 24-hour (1-24) */
array ( 'k' , 0 , '24' ),
array ( 'kk' , 0 , '24' ),
array ( 'kkk' , 0 , '024' ),
array ( 'k' , 1 , '24' ),
array ( 'k' , 3600 , '1' ),
array ( 'k' , 43200 , '12' ),
array ( 'k' , 46800 , '13' ),
2011-02-07 11:13:36 +00:00
/* 12-hour (0-11) */
array ( 'K' , 0 , '0' ),
array ( 'KK' , 0 , '00' ),
array ( 'KKK' , 0 , '000' ),
array ( 'K' , 1 , '0' ),
array ( 'K' , 3600 , '1' ),
array ( 'K' , 43200 , '0' ), // 12 hours
2011-02-07 11:18:41 +00:00
/* minute */
array ( 'm' , 0 , '0' ),
array ( 'mm' , 0 , '00' ),
array ( 'mmm' , 0 , '000' ),
array ( 'm' , 1 , '0' ),
array ( 'm' , 60 , '1' ),
array ( 'm' , 120 , '2' ),
array ( 'm' , 180 , '3' ),
array ( 'm' , 3600 , '0' ),
array ( 'm' , 3660 , '1' ),
array ( 'm' , 43200 , '0' ), // 12 hours
2011-02-07 11:23:47 +00:00
/* second */
array ( 's' , 0 , '0' ),
array ( 'ss' , 0 , '00' ),
array ( 'sss' , 0 , '000' ),
array ( 's' , 1 , '1' ),
array ( 's' , 2 , '2' ),
array ( 's' , 5 , '5' ),
array ( 's' , 30 , '30' ),
array ( 's' , 59 , '59' ),
array ( 's' , 60 , '0' ),
array ( 's' , 120 , '0' ),
array ( 's' , 180 , '0' ),
array ( 's' , 3600 , '0' ),
array ( 's' , 3601 , '1' ),
array ( 's' , 3630 , '30' ),
array ( 's' , 43200 , '0' ), // 12 hours
2011-02-07 18:36:02 +00:00
/* timezone */
array ( 'z' , 0 , 'GMT+00:00' ),
array ( 'zz' , 0 , 'GMT+00:00' ),
array ( 'zzz' , 0 , 'GMT+00:00' ),
array ( 'zzzz' , 0 , 'GMT+00:00' ),
array ( 'zzzzz' , 0 , 'GMT+00:00' ),
2011-05-25 00:08:09 +01:00
);
2012-04-08 23:21:28 +01:00
// As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances
2012-04-15 19:32:40 +01:00
if ( $this -> isGreaterOrEqualThanPhpVersion ( '5.3.4' )) {
2012-04-08 23:21:28 +01:00
$dateTime = new \DateTime ( '@0' );
/* general, DateTime */
$formatData [] = array ( 'y-M-d' , $dateTime , '1970-1-1' );
$formatData [] = array ( " yyyy.MM.dd 'at' HH:mm:ss zzz " , $dateTime , '1970.01.01 at 00:00:00 GMT+00:00' );
$formatData [] = array ( " EEE, MMM d, ''yy " , $dateTime , " Thu, Jan 1, '70 " );
$formatData [] = array ( 'h:mm a' , $dateTime , '12:00 AM' );
$formatData [] = array ( 'K:mm a, z' , $dateTime , '0:00 AM, GMT+00:00' );
$formatData [] = array ( 'yyyyy.MMMM.dd hh:mm aaa' , $dateTime , '01970.January.01 12:00 AM' );
}
2011-05-25 00:08:09 +01:00
return $formatData ;
}
2011-05-19 00:15:37 +01:00
2012-04-15 18:57:23 +01:00
/**
* @ dataProvider formatErrorProvider
*/
2012-04-16 00:01:42 +01:00
public function testFormatIllegalArgumentErrorStub ( $pattern , $timestamp , $errorMessage )
2012-04-15 18:57:23 +01:00
{
2012-04-16 00:01:42 +01:00
$errorCode = StubIntl :: U_ILLEGAL_ARGUMENT_ERROR ;
2012-04-15 18:57:23 +01:00
$formatter = $this -> createStubFormatter ( $pattern );
2012-04-16 00:01:42 +01:00
$this -> assertFalse ( $formatter -> format ( $timestamp ));
2012-04-15 18:57:23 +01:00
$this -> assertSame ( $errorMessage , StubIntl :: getErrorMessage ());
$this -> assertSame ( $errorCode , StubIntl :: getErrorCode ());
2012-04-16 00:23:02 +01:00
$this -> assertTrue ( StubIntl :: isFailure ( StubIntl :: getErrorCode ()));
2012-04-15 18:57:23 +01:00
$this -> assertSame ( $errorMessage , $formatter -> getErrorMessage ());
$this -> assertSame ( $errorCode , $formatter -> getErrorCode ());
2012-04-16 00:23:02 +01:00
$this -> assertTrue ( StubIntl :: isFailure ( $formatter -> getErrorCode ()));
2012-04-15 18:57:23 +01:00
}
/**
* @ dataProvider formatErrorProvider
*/
2012-04-16 00:01:42 +01:00
public function testFormatIllegalArgumentErrorIntl ( $pattern , $timestamp , $errorMessage )
2012-04-15 18:57:23 +01:00
{
$this -> skipIfIntlExtensionIsNotLoaded ();
$this -> skipIfICUVersionIsTooOld ();
2012-04-16 00:01:42 +01:00
$errorCode = StubIntl :: U_ILLEGAL_ARGUMENT_ERROR ;
2012-04-15 18:57:23 +01:00
$formatter = $this -> createIntlFormatter ( $pattern );
2012-04-16 00:01:42 +01:00
$this -> assertFalse ( $formatter -> format ( $timestamp ));
2012-04-15 18:57:23 +01:00
$this -> assertSame ( $errorMessage , intl_get_error_message ());
$this -> assertSame ( $errorCode , intl_get_error_code ());
2012-04-16 00:23:02 +01:00
$this -> assertTrue ( intl_is_failure ( intl_get_error_code ()));
2012-04-15 18:57:23 +01:00
}
2011-05-25 00:08:09 +01:00
public function formatErrorProvider ()
{
2012-04-15 18:56:41 +01:00
$message = 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR' ;
2012-04-15 19:32:40 +01:00
if ( $this -> isGreaterOrEqualThanPhpVersion ( '5.3.4' )) {
2012-04-15 18:56:41 +01:00
$message = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object: U_ILLEGAL_ARGUMENT_ERROR' ;
}
2011-06-08 18:56:59 +01:00
2011-05-25 00:08:09 +01:00
return array (
2012-04-16 00:01:42 +01:00
array ( 'y-M-d' , '0' , $message ),
array ( 'y-M-d' , 'foobar' , $message ),
2011-02-05 20:29:40 +00:00
);
}
2011-02-10 22:22:08 +00:00
/**
2011-03-07 02:46:23 +00:00
* @ dataProvider formatWithTimezoneProvider
*/
2011-02-18 09:37:37 +00:00
public function testFormatWithTimezoneStub ( $timestamp , $timezone , $expected )
2011-02-10 22:22:08 +00:00
{
$pattern = 'yyyy-MM-dd HH:mm:ss' ;
$formatter = new StubIntlDateFormatter ( 'en' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT , $timezone , StubIntlDateFormatter :: GREGORIAN , $pattern );
2011-02-18 09:37:37 +00:00
$this -> assertSame ( $expected , $formatter -> format ( $timestamp ));
}
2011-02-10 22:22:08 +00:00
2011-02-18 09:37:37 +00:00
/**
2011-03-07 02:46:23 +00:00
* @ dataProvider formatWithTimezoneProvider
*/
2011-02-18 09:37:37 +00:00
public function testFormatWithTimezoneIntl ( $timestamp , $timezone , $expected )
{
$this -> skipIfIntlExtensionIsNotLoaded ();
$pattern = 'yyyy-MM-dd HH:mm:ss' ;
$formatter = new \IntlDateFormatter ( 'en' , \IntlDateFormatter :: MEDIUM , \IntlDateFormatter :: SHORT , $timezone , \IntlDateFormatter :: GREGORIAN , $pattern );
2011-02-18 22:52:19 +00:00
$this -> assertSame ( $expected , $formatter -> format ( $timestamp ));
2011-02-10 22:22:08 +00:00
}
2011-02-07 18:22:22 +00:00
public function formatWithTimezoneProvider ()
{
return array (
array ( 0 , 'UTC' , '1970-01-01 00:00:00' ),
2011-03-07 02:46:23 +00:00
array ( 0 , 'GMT' , '1970-01-01 00:00:00' ),
array ( 0 , 'GMT-03:00' , '1969-12-31 21:00:00' ),
array ( 0 , 'GMT+03:00' , '1970-01-01 03:00:00' ),
2011-02-07 18:22:22 +00:00
array ( 0 , 'Europe/Zurich' , '1970-01-01 01:00:00' ),
array ( 0 , 'Europe/Paris' , '1970-01-01 01:00:00' ),
array ( 0 , 'Africa/Cairo' , '1970-01-01 02:00:00' ),
array ( 0 , 'Africa/Casablanca' , '1970-01-01 00:00:00' ),
array ( 0 , 'Africa/Djibouti' , '1970-01-01 03:00:00' ),
array ( 0 , 'Africa/Johannesburg' , '1970-01-01 02:00:00' ),
array ( 0 , 'America/Antigua' , '1969-12-31 20:00:00' ),
array ( 0 , 'America/Toronto' , '1969-12-31 19:00:00' ),
array ( 0 , 'America/Vancouver' , '1969-12-31 16:00:00' ),
array ( 0 , 'Asia/Aqtau' , '1970-01-01 05:00:00' ),
array ( 0 , 'Asia/Bangkok' , '1970-01-01 07:00:00' ),
array ( 0 , 'Asia/Dubai' , '1970-01-01 04:00:00' ),
array ( 0 , 'Australia/Brisbane' , '1970-01-01 10:00:00' ),
2011-03-07 02:46:23 +00:00
array ( 0 , 'Australia/Eucla' , '1970-01-01 08:45:00' ),
2011-02-07 18:22:22 +00:00
array ( 0 , 'Australia/Melbourne' , '1970-01-01 10:00:00' ),
array ( 0 , 'Europe/Berlin' , '1970-01-01 01:00:00' ),
array ( 0 , 'Europe/Dublin' , '1970-01-01 01:00:00' ),
array ( 0 , 'Europe/Warsaw' , '1970-01-01 01:00:00' ),
array ( 0 , 'Pacific/Fiji' , '1970-01-01 12:00:00' ),
2011-02-09 07:56:16 +00:00
2011-03-07 02:46:23 +00:00
// When time zone not exists, uses UTC by default
2011-02-09 07:56:16 +00:00
array ( 0 , 'Foo/Bar' , '1970-01-01 00:00:00' ),
2011-03-07 02:46:23 +00:00
array ( 0 , 'UTC+04:30' , '1970-01-01 00:00:00' ),
array ( 0 , 'UTC+04:AA' , '1970-01-01 00:00:00' ),
);
}
/**
* @ expectedException Symfony\Component\Locale\Exception\NotImplementedException
*/
public function testFormatWithTimezoneFormatOptionAndDifferentThanUtcStub ()
{
$formatter = $this -> createStubFormatter ( 'zzzz' );
$formatter -> setTimeZoneId ( 'Pacific/Fiji' );
$formatter -> format ( 0 );
}
public function testFormatWithTimezoneFormatOptionAndDifferentThanUtcIntl ()
{
$this -> skipIfIntlExtensionIsNotLoaded ();
$formatter = $this -> createIntlFormatter ( 'zzzz' );
$formatter -> setTimeZoneId ( 'Pacific/Fiji' );
$this -> assertEquals ( 'Fiji Time' , $formatter -> format ( 0 ));
}
public function testFormatWithGmtTimezoneStub ()
{
$formatter = $this -> createStubFormatter ( 'zzzz' );
$formatter -> setTimeZoneId ( 'GMT+03:00' );
$this -> assertEquals ( 'GMT+03:00' , $formatter -> format ( 0 ));
}
public function testFormatWithGmtTimezoneIntl ()
{
$this -> skipIfIntlExtensionIsNotLoaded ();
$formatter = $this -> createIntlFormatter ( 'zzzz' );
$formatter -> setTimeZoneId ( 'GMT+03:00' );
$this -> assertEquals ( 'GMT+03:00' , $formatter -> format ( 0 ));
}
public function testFormatWithDefaultTimezoneStub ()
{
$formatter = new StubIntlDateFormatter ( 'en' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT );
$formatter -> setPattern ( 'yyyy-MM-dd HH:mm:ss' );
$this -> assertEquals (
$this -> createDateTime ( 0 ) -> format ( 'Y-m-d H:i:s' ),
$formatter -> format ( 0 )
);
}
public function testFormatWithDefaultTimezoneIntl ()
{
$this -> skipIfIntlExtensionIsNotLoaded ();
2011-04-02 14:17:16 +01:00
$this -> skipIfICUVersionIsTooOld ();
2011-03-07 02:46:23 +00:00
$formatter = new \IntlDateFormatter ( 'en' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT );
$formatter -> setPattern ( 'yyyy-MM-dd HH:mm:ss' );
$this -> assertEquals (
$this -> createDateTime ( 0 ) -> format ( 'Y-m-d H:i:s' ),
$formatter -> format ( 0 )
2011-02-07 18:22:22 +00:00
);
}
2011-02-18 22:52:19 +00:00
/**
* @ expectedException Symfony\Component\Locale\Exception\NotImplementedException
*/
public function testFormatWithUnimplementedCharsStub ()
{
$pattern = 'Y' ;
$formatter = new StubIntlDateFormatter ( 'en' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT , 'UTC' , StubIntlDateFormatter :: GREGORIAN , $pattern );
$formatter -> format ( 0 );
}
2011-02-28 12:18:31 +00:00
/**
* @ expectedException Symfony\Component\Locale\Exception\NotImplementedException
*/
public function testFormatWithNonIntegerTimestamp ()
{
$formatter = $this -> createStubFormatter ();
$formatter -> format ( array ());
}
2011-02-05 20:29:40 +00:00
/**
2011-02-18 09:37:37 +00:00
* @ dataProvider dateAndTimeTypeProvider
2011-02-05 20:29:40 +00:00
*/
2011-02-18 09:37:37 +00:00
public function testDateAndTimeTypeStub ( $timestamp , $datetype , $timetype , $expected )
2011-02-05 20:29:40 +00:00
{
2011-02-10 22:22:08 +00:00
$formatter = new StubIntlDateFormatter ( 'en' , $datetype , $timetype , 'UTC' );
2011-02-18 09:37:37 +00:00
$this -> assertSame ( $expected , $formatter -> format ( $timestamp ));
}
2011-02-05 20:29:40 +00:00
2011-02-18 09:37:37 +00:00
/**
* @ dataProvider dateAndTimeTypeProvider
*/
public function testDateAndTimeTypeIntl ( $timestamp , $datetype , $timetype , $expected )
{
$this -> skipIfIntlExtensionIsNotLoaded ();
$formatter = new \IntlDateFormatter ( 'en' , $datetype , $timetype , 'UTC' );
$this -> assertSame ( $expected , $formatter -> format ( $timestamp ));
2011-02-05 20:29:40 +00:00
}
2011-02-18 09:37:37 +00:00
public function dateAndTimeTypeProvider ()
2011-02-07 18:22:22 +00:00
{
2011-02-10 22:22:08 +00:00
return array (
array ( 0 , StubIntlDateFormatter :: FULL , StubIntlDateFormatter :: NONE , 'Thursday, January 1, 1970' ),
array ( 0 , StubIntlDateFormatter :: LONG , StubIntlDateFormatter :: NONE , 'January 1, 1970' ),
array ( 0 , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: NONE , 'Jan 1, 1970' ),
array ( 0 , StubIntlDateFormatter :: SHORT , StubIntlDateFormatter :: NONE , '1/1/70' ),
array ( 0 , StubIntlDateFormatter :: NONE , StubIntlDateFormatter :: FULL , '12:00:00 AM GMT+00:00' ),
array ( 0 , StubIntlDateFormatter :: NONE , StubIntlDateFormatter :: LONG , '12:00:00 AM GMT+00:00' ),
array ( 0 , StubIntlDateFormatter :: NONE , StubIntlDateFormatter :: MEDIUM , '12:00:00 AM' ),
array ( 0 , StubIntlDateFormatter :: NONE , StubIntlDateFormatter :: SHORT , '12:00 AM' ),
);
2011-02-07 18:22:22 +00:00
}
2011-02-18 09:37:37 +00:00
public function testGetCalendar ()
2011-02-17 21:26:45 +00:00
{
2011-02-20 22:46:09 +00:00
$formatter = $this -> createStubFormatter ();
2011-02-18 09:37:37 +00:00
$this -> assertEquals ( StubIntlDateFormatter :: GREGORIAN , $formatter -> getCalendar ());
2011-02-17 21:26:45 +00:00
}
2011-02-18 09:37:37 +00:00
public function testGetDateType ()
2011-02-17 21:26:45 +00:00
{
$formatter = new StubIntlDateFormatter ( 'en' , StubIntlDateFormatter :: FULL , StubIntlDateFormatter :: NONE );
2011-02-18 09:37:37 +00:00
$this -> assertEquals ( StubIntlDateFormatter :: FULL , $formatter -> getDateType ());
2011-02-17 21:26:45 +00:00
}
2011-02-18 09:37:37 +00:00
public function testGetErrorCode ()
2011-02-05 20:29:40 +00:00
{
2011-02-20 22:46:09 +00:00
$formatter = $this -> createStubFormatter ();
2012-04-14 21:11:17 +01:00
$this -> assertEquals ( StubIntl :: getErrorCode (), $formatter -> getErrorCode ());
2011-02-10 22:22:08 +00:00
}
2011-02-18 09:37:37 +00:00
public function testGetErrorMessage ()
2011-02-10 22:22:08 +00:00
{
2011-02-20 22:46:09 +00:00
$formatter = $this -> createStubFormatter ();
2012-04-14 21:11:17 +01:00
$this -> assertEquals ( StubIntl :: getErrorMessage (), $formatter -> getErrorMessage ());
2011-02-05 20:29:40 +00:00
}
2011-02-17 22:43:34 +00:00
2011-02-18 09:37:37 +00:00
public function testGetLocale ()
2011-02-17 22:43:34 +00:00
{
2011-02-20 22:46:09 +00:00
$formatter = $this -> createStubFormatter ();
2011-02-18 09:37:37 +00:00
$this -> assertEquals ( 'en' , $formatter -> getLocale ());
2011-02-17 22:43:34 +00:00
}
2011-02-18 09:37:37 +00:00
public function testGetPattern ()
2011-02-17 22:43:34 +00:00
{
2011-02-23 18:08:06 +00:00
$formatter = new StubIntlDateFormatter ( 'en' , StubIntlDateFormatter :: FULL , StubIntlDateFormatter :: NONE , 'UTC' , StubIntlDateFormatter :: GREGORIAN , 'yyyy-MM-dd' );
2011-02-18 09:37:37 +00:00
$this -> assertEquals ( 'yyyy-MM-dd' , $formatter -> getPattern ());
2011-02-17 22:43:34 +00:00
}
2011-02-18 09:37:37 +00:00
public function testGetTimeType ()
2011-02-17 22:43:34 +00:00
{
2011-02-18 09:37:37 +00:00
$formatter = new StubIntlDateFormatter ( 'en' , StubIntlDateFormatter :: NONE , StubIntlDateFormatter :: FULL );
$this -> assertEquals ( StubIntlDateFormatter :: FULL , $formatter -> getTimeType ());
2011-02-17 22:43:34 +00:00
}
2011-02-18 09:37:37 +00:00
/**
* @ expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
*/
public function testIsLenient ()
2011-02-17 22:43:34 +00:00
{
2011-02-20 22:46:09 +00:00
$formatter = $this -> createStubFormatter ();
2011-02-18 09:37:37 +00:00
$formatter -> isLenient ();
2011-02-17 22:43:34 +00:00
}
/**
* @ expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
*/
public function testLocaltime ()
{
2011-02-20 22:46:09 +00:00
$formatter = $this -> createStubFormatter ();
2011-02-17 22:43:34 +00:00
$formatter -> localtime ( 'Wednesday, December 31, 1969 4:00:00 PM PT' );
}
/**
2011-02-27 05:22:07 +00:00
* @ dataProvider parseProvider
2011-02-17 22:43:34 +00:00
*/
2012-02-14 03:35:14 +00:00
public function testParseIntl ( $pattern , $value , $expected )
2011-02-17 22:43:34 +00:00
{
2012-02-14 03:35:14 +00:00
$errorCode = StubIntl :: U_ZERO_ERROR ;
$errorMessage = 'U_ZERO_ERROR' ;
2011-03-01 05:08:13 +00:00
$this -> skipIfIntlExtensionIsNotLoaded ();
2011-02-27 05:22:07 +00:00
$formatter = $this -> createIntlFormatter ( $pattern );
2011-02-28 09:16:40 +00:00
$this -> assertSame ( $expected , $formatter -> parse ( $value ));
2011-05-19 00:15:37 +01:00
$this -> assertSame ( $errorMessage , intl_get_error_message ());
$this -> assertSame ( $errorCode , intl_get_error_code ());
2012-04-16 00:23:02 +01:00
$this -> assertFalse ( intl_is_failure ( intl_get_error_code ()));
2011-02-27 05:22:07 +00:00
}
/**
* @ dataProvider parseProvider
*/
2012-02-14 03:35:14 +00:00
public function testParseStub ( $pattern , $value , $expected )
2011-02-27 05:22:07 +00:00
{
2012-02-14 03:35:14 +00:00
$errorCode = StubIntl :: U_ZERO_ERROR ;
$errorMessage = 'U_ZERO_ERROR' ;
2011-02-27 05:22:07 +00:00
$formatter = $this -> createStubFormatter ( $pattern );
2011-02-28 09:16:40 +00:00
$this -> assertSame ( $expected , $formatter -> parse ( $value ));
2011-05-19 00:15:37 +01:00
$this -> assertSame ( $errorMessage , StubIntl :: getErrorMessage ());
$this -> assertSame ( $errorCode , StubIntl :: getErrorCode ());
2012-04-16 00:23:02 +01:00
$this -> assertFalse ( StubIntl :: isFailure ( StubIntl :: getErrorCode ()));
2012-04-14 21:11:17 +01:00
$this -> assertSame ( $errorMessage , $formatter -> getErrorMessage ());
$this -> assertSame ( $errorCode , $formatter -> getErrorCode ());
2012-04-16 00:23:02 +01:00
$this -> assertFalse ( StubIntl :: isFailure ( $formatter -> getErrorCode ()));
2011-02-27 05:22:07 +00:00
}
public function parseProvider ()
{
return array (
// years
array ( 'y-M-d' , '1970-1-1' , 0 ),
2011-09-16 12:12:22 +01:00
array ( 'yy-M-d' , '70-1-1' , 0 ),
2011-02-27 05:22:07 +00:00
// months
array ( 'y-M-d' , '1970-1-1' , 0 ),
array ( 'y-MMM-d' , '1970-Jan-1' , 0 ),
array ( 'y-MMMM-d' , '1970-January-1' , 0 ),
// standalone months
array ( 'y-L-d' , '1970-1-1' , 0 ),
array ( 'y-LLL-d' , '1970-Jan-1' , 0 ),
array ( 'y-LLLL-d' , '1970-January-1' , 0 ),
// days
array ( 'y-M-d' , '1970-1-1' , 0 ),
array ( 'y-M-dd' , '1970-1-01' , 0 ),
array ( 'y-M-ddd' , '1970-1-001' , 0 ),
2011-02-27 18:22:25 +00:00
// 12 hours (1-12)
array ( 'y-M-d h' , '1970-1-1 1' , 3600 ),
array ( 'y-M-d h' , '1970-1-1 10' , 36000 ),
array ( 'y-M-d hh' , '1970-1-1 11' , 39600 ),
array ( 'y-M-d hh' , '1970-1-1 12' , 0 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d hh a' , '1970-1-1 0 AM' , 0 ),
array ( 'y-M-d hh a' , '1970-1-1 1 AM' , 3600 ),
array ( 'y-M-d hh a' , '1970-1-1 10 AM' , 36000 ),
2011-02-27 18:22:25 +00:00
array ( 'y-M-d hh a' , '1970-1-1 11 AM' , 39600 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d hh a' , '1970-1-1 12 AM' , 0 ),
array ( 'y-M-d hh a' , '1970-1-1 23 AM' , 82800 ),
array ( 'y-M-d hh a' , '1970-1-1 24 AM' , 86400 ),
array ( 'y-M-d hh a' , '1970-1-1 0 PM' , 43200 ),
array ( 'y-M-d hh a' , '1970-1-1 1 PM' , 46800 ),
array ( 'y-M-d hh a' , '1970-1-1 10 PM' , 79200 ),
2011-02-27 18:22:25 +00:00
array ( 'y-M-d hh a' , '1970-1-1 11 PM' , 82800 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d hh a' , '1970-1-1 12 PM' , 43200 ),
array ( 'y-M-d hh a' , '1970-1-1 23 PM' , 126000 ),
array ( 'y-M-d hh a' , '1970-1-1 24 PM' , 129600 ),
2011-02-27 18:22:25 +00:00
// 12 hours (0-11)
array ( 'y-M-d K' , '1970-1-1 1' , 3600 ),
array ( 'y-M-d K' , '1970-1-1 10' , 36000 ),
array ( 'y-M-d KK' , '1970-1-1 11' , 39600 ),
array ( 'y-M-d KK' , '1970-1-1 12' , 43200 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d KK a' , '1970-1-1 0 AM' , 0 ),
array ( 'y-M-d KK a' , '1970-1-1 1 AM' , 3600 ),
2011-02-27 18:22:25 +00:00
array ( 'y-M-d KK a' , '1970-1-1 10 AM' , 36000 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d KK a' , '1970-1-1 11 AM' , 39600 ),
array ( 'y-M-d KK a' , '1970-1-1 12 AM' , 43200 ),
array ( 'y-M-d KK a' , '1970-1-1 23 AM' , 82800 ),
array ( 'y-M-d KK a' , '1970-1-1 24 AM' , 86400 ),
array ( 'y-M-d KK a' , '1970-1-1 0 PM' , 43200 ),
array ( 'y-M-d KK a' , '1970-1-1 1 PM' , 46800 ),
2011-02-27 18:22:25 +00:00
array ( 'y-M-d KK a' , '1970-1-1 10 PM' , 79200 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d KK a' , '1970-1-1 11 PM' , 82800 ),
array ( 'y-M-d KK a' , '1970-1-1 12 PM' , 86400 ),
array ( 'y-M-d KK a' , '1970-1-1 23 PM' , 126000 ),
array ( 'y-M-d KK a' , '1970-1-1 24 PM' , 129600 ),
2011-02-27 18:22:25 +00:00
// 24 hours (0-23)
array ( 'y-M-d H' , '1970-1-1 0' , 0 ),
array ( 'y-M-d H' , '1970-1-1 1' , 3600 ),
array ( 'y-M-d H' , '1970-1-1 10' , 36000 ),
array ( 'y-M-d HH' , '1970-1-1 11' , 39600 ),
array ( 'y-M-d HH' , '1970-1-1 12' , 43200 ),
array ( 'y-M-d HH' , '1970-1-1 23' , 82800 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d HH a' , '1970-1-1 0 AM' , 0 ),
array ( 'y-M-d HH a' , '1970-1-1 1 AM' , 0 ),
array ( 'y-M-d HH a' , '1970-1-1 10 AM' , 0 ),
2011-02-27 18:22:25 +00:00
array ( 'y-M-d HH a' , '1970-1-1 11 AM' , 0 ),
array ( 'y-M-d HH a' , '1970-1-1 12 AM' , 0 ),
array ( 'y-M-d HH a' , '1970-1-1 23 AM' , 0 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d HH a' , '1970-1-1 24 AM' , 0 ),
array ( 'y-M-d HH a' , '1970-1-1 0 PM' , 43200 ),
array ( 'y-M-d HH a' , '1970-1-1 1 PM' , 43200 ),
array ( 'y-M-d HH a' , '1970-1-1 10 PM' , 43200 ),
array ( 'y-M-d HH a' , '1970-1-1 11 PM' , 43200 ),
array ( 'y-M-d HH a' , '1970-1-1 12 PM' , 43200 ),
array ( 'y-M-d HH a' , '1970-1-1 23 PM' , 43200 ),
array ( 'y-M-d HH a' , '1970-1-1 24 PM' , 43200 ),
2011-02-27 18:22:25 +00:00
// 24 hours (1-24)
array ( 'y-M-d k' , '1970-1-1 1' , 3600 ),
array ( 'y-M-d k' , '1970-1-1 10' , 36000 ),
array ( 'y-M-d kk' , '1970-1-1 11' , 39600 ),
array ( 'y-M-d kk' , '1970-1-1 12' , 43200 ),
array ( 'y-M-d kk' , '1970-1-1 23' , 82800 ),
array ( 'y-M-d kk' , '1970-1-1 24' , 0 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d kk a' , '1970-1-1 0 AM' , 0 ),
array ( 'y-M-d kk a' , '1970-1-1 1 AM' , 0 ),
array ( 'y-M-d kk a' , '1970-1-1 10 AM' , 0 ),
2011-02-27 18:22:25 +00:00
array ( 'y-M-d kk a' , '1970-1-1 11 AM' , 0 ),
array ( 'y-M-d kk a' , '1970-1-1 12 AM' , 0 ),
array ( 'y-M-d kk a' , '1970-1-1 23 AM' , 0 ),
2011-03-07 04:24:04 +00:00
array ( 'y-M-d kk a' , '1970-1-1 24 AM' , 0 ),
array ( 'y-M-d kk a' , '1970-1-1 0 PM' , 43200 ),
array ( 'y-M-d kk a' , '1970-1-1 1 PM' , 43200 ),
array ( 'y-M-d kk a' , '1970-1-1 10 PM' , 43200 ),
array ( 'y-M-d kk a' , '1970-1-1 11 PM' , 43200 ),
array ( 'y-M-d kk a' , '1970-1-1 12 PM' , 43200 ),
array ( 'y-M-d kk a' , '1970-1-1 23 PM' , 43200 ),
array ( 'y-M-d kk a' , '1970-1-1 24 PM' , 43200 ),
2011-02-27 21:21:35 +00:00
// minutes
array ( 'y-M-d HH:m' , '1970-1-1 0:1' , 60 ),
array ( 'y-M-d HH:mm' , '1970-1-1 0:10' , 600 ),
// seconds
array ( 'y-M-d HH:mm:s' , '1970-1-1 00:01:1' , 61 ),
array ( 'y-M-d HH:mm:ss' , '1970-1-1 00:01:10' , 70 ),
2011-02-28 00:09:36 +00:00
// timezone
array ( 'y-M-d HH:mm:ss zzzz' , '1970-1-1 00:00:00 GMT-03:00' , 10800 ),
array ( 'y-M-d HH:mm:ss zzzz' , '1970-1-1 00:00:00 GMT-04:00' , 14400 ),
array ( 'y-M-d HH:mm:ss zzzz' , '1970-1-1 00:00:00 GMT-00:00' , 0 ),
array ( 'y-M-d HH:mm:ss zzzz' , '1970-1-1 00:00:00 GMT+03:00' , - 10800 ),
array ( 'y-M-d HH:mm:ss zzzz' , '1970-1-1 00:00:00 GMT+04:00' , - 14400 ),
2011-03-07 02:46:23 +00:00
array ( 'y-M-d HH:mm:ss zzzz' , '1970-1-1 00:00:00 GMT-0300' , 10800 ),
array ( 'y-M-d HH:mm:ss zzzz' , '1970-1-1 00:00:00 GMT+0300' , - 10800 ),
2011-02-28 00:09:36 +00:00
// a previous timezoned parsing should not change the timezone for the next parsing
2011-02-28 12:44:14 +00:00
array ( 'y-M-d HH:mm:ss' , '1970-1-1 00:00:00' , 0 ),
2011-03-07 05:39:27 +00:00
// AM/PM (already covered by hours tests)
array ( 'y-M-d HH:mm:ss a' , '1970-1-1 00:00:00 AM' , 0 ),
array ( 'y-M-d HH:mm:ss a' , '1970-1-1 00:00:00 PM' , 43200 ),
2011-02-28 12:44:14 +00:00
// regExp metachars in the pattern string
array ( 'y[M-d' , '1970[1-1' , 0 ),
2011-02-28 12:54:38 +00:00
array ( 'y[M/d' , '1970[1/1' , 0 ),
2011-02-28 17:22:22 +00:00
// quote characters
array ( " 'M' " , 'M' , 0 ),
array ( " 'yy' " , 'yy' , 0 ),
array ( " '''yy' " , " 'yy " , 0 ),
array ( " ''y " , " '1970 " , 0 ),
array ( " H 'o'' clock' " , " 0 o' clock " , 0 ),
2011-02-27 05:22:07 +00:00
);
2011-02-17 22:43:34 +00:00
}
2012-02-14 03:35:14 +00:00
/**
* @ dataProvider parseErrorProvider
*/
public function testParseErrorIntl ( $pattern , $value )
{
$errorCode = StubIntl :: U_PARSE_ERROR ;
$errorMessage = 'Date parsing failed: U_PARSE_ERROR' ;
$this -> skipIfIntlExtensionIsNotLoaded ();
$formatter = $this -> createIntlFormatter ( $pattern );
2012-04-14 21:11:17 +01:00
$this -> assertFalse ( $formatter -> parse ( $value ));
2012-02-14 03:35:14 +00:00
$this -> assertSame ( $errorMessage , intl_get_error_message ());
$this -> assertSame ( $errorCode , intl_get_error_code ());
2012-04-16 00:23:02 +01:00
$this -> assertTrue ( intl_is_failure ( intl_get_error_code ()));
2012-02-14 03:35:14 +00:00
}
/**
* @ dataProvider parseErrorProvider
*/
public function testParseErrorStub ( $pattern , $value )
{
$errorCode = StubIntl :: U_PARSE_ERROR ;
$errorMessage = 'Date parsing failed: U_PARSE_ERROR' ;
$formatter = $this -> createStubFormatter ( $pattern );
2012-04-14 21:11:17 +01:00
$this -> assertFalse ( $formatter -> parse ( $value ));
2012-02-14 03:35:14 +00:00
$this -> assertSame ( $errorMessage , StubIntl :: getErrorMessage ());
$this -> assertSame ( $errorCode , StubIntl :: getErrorCode ());
2012-04-16 00:23:02 +01:00
$this -> assertTrue ( StubIntl :: isFailure ( StubIntl :: getErrorCode ()));
2012-04-14 21:11:17 +01:00
$this -> assertSame ( $errorMessage , $formatter -> getErrorMessage ());
$this -> assertSame ( $errorCode , $formatter -> getErrorCode ());
2012-04-16 00:23:02 +01:00
$this -> assertTrue ( StubIntl :: isFailure ( $formatter -> getErrorCode ()));
2012-02-14 03:35:14 +00:00
}
public function parseErrorProvider ()
{
return array (
array ( 'y-M-d' , '1970/1/1' ),
array ( 'yy-M-d' , '70/1/1' ),
// 1 char month
array ( 'y-MMMMM-d' , '1970-J-1' ),
array ( 'y-MMMMM-d' , '1970-S-1' ),
// standalone 1 char month
array ( 'y-LLLLL-d' , '1970-J-1' ),
array ( 'y-LLLLL-d' , '1970-S-1' ),
);
}
2011-03-07 05:39:27 +00:00
/**
2011-03-08 20:00:32 +00:00
* Just to document the differences between the stub and the intl implementations . The intl can parse
2011-03-07 05:39:27 +00:00
* any of the tested formats alone . The stub does not implement them as it would be needed to add more
* abstraction , passing more context to the transformers objects . Any of the formats are ignored alone
* or with date / time data ( years , months , days , hours , minutes and seconds ) .
*
* Also in intl , format like 'ss E' for '10 2' ( 2 nd day of year + 10 seconds ) are added , then we have
* 86 , 400 seconds ( 24 h * 60 min * 60 s ) + 10 seconds
*
2011-03-08 20:00:32 +00:00
* @ dataProvider parseDifferences ()
2011-03-07 05:39:27 +00:00
*/
2011-03-08 20:00:32 +00:00
public function testParseDifferencesStub ( $pattern , $value , $stubExpected , $intlExpected )
2011-03-07 05:39:27 +00:00
{
$formatter = $this -> createStubFormatter ( $pattern );
$this -> assertSame ( $stubExpected , $formatter -> parse ( $value ));
}
/**
2011-03-08 20:00:32 +00:00
* @ dataProvider parseDifferences ()
2011-03-07 05:39:27 +00:00
*/
2011-03-08 20:00:32 +00:00
public function testParseDifferencesIntl ( $pattern , $value , $stubExpected , $intlExpected )
2011-03-07 05:39:27 +00:00
{
2011-03-07 06:19:47 +00:00
$this -> skipIfIntlExtensionIsNotLoaded ();
2011-04-02 14:17:16 +01:00
$this -> skipIfICUVersionIsTooOld ();
2011-03-07 05:39:27 +00:00
$formatter = $this -> createIntlFormatter ( $pattern );
$this -> assertSame ( $intlExpected , $formatter -> parse ( $value ));
}
2011-03-08 20:00:32 +00:00
public function parseDifferences ()
2011-03-07 05:39:27 +00:00
{
return array (
// AM/PM, ignored if alone
array ( 'a' , 'AM' , 0 , 0 ),
array ( 'a' , 'PM' , 0 , 43200 ),
// day of week
array ( 'E' , 'Thu' , 0 , 0 ),
array ( 'EE' , 'Thu' , 0 , 0 ),
array ( 'EEE' , 'Thu' , 0 , 0 ),
array ( 'EEEE' , 'Thursday' , 0 , 0 ),
array ( 'EEEEE' , 'T' , 0 , 432000 ),
array ( 'EEEEEE' , 'Thu' , 0 , 0 ),
// day of year
array ( 'D' , '1' , 0 , 0 ),
array ( 'D' , '2' , 0 , 86400 ),
// quarter
array ( 'Q' , '1' , 0 , 0 ),
array ( 'QQ' , '01' , 0 , 0 ),
array ( 'QQQ' , 'Q1' , 0 , 0 ),
array ( 'QQQQ' , '1st quarter' , 0 , 0 ),
array ( 'QQQQQ' , '1st quarter' , 0 , 0 ),
array ( 'Q' , '2' , 0 , 7776000 ),
array ( 'QQ' , '02' , 0 , 7776000 ),
array ( 'QQQ' , 'Q2' , 0 , 7776000 ),
array ( 'QQQQ' , '2nd quarter' , 0 , 7776000 ),
array ( 'QQQQQ' , '2nd quarter' , 0 , 7776000 ),
array ( 'q' , '1' , 0 , 0 ),
array ( 'qq' , '01' , 0 , 0 ),
array ( 'qqq' , 'Q1' , 0 , 0 ),
array ( 'qqqq' , '1st quarter' , 0 , 0 ),
array ( 'qqqqq' , '1st quarter' , 0 , 0 ),
);
}
2011-03-01 05:08:13 +00:00
public function testParseWithNullPositionValueStub ()
{
$position = null ;
$formatter = $this -> createStubFormatter ( 'y' );
$this -> assertSame ( 0 , $formatter -> parse ( '1970' , $position ));
$this -> assertNull ( $position );
}
/**
* @ expectedException Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException
*/
public function testParseWithNotNullPositionValueStub ()
{
$position = 0 ;
$formatter = $this -> createStubFormatter ( 'y' );
$this -> assertSame ( 0 , $formatter -> parse ( '1970' , $position ));
}
2011-02-17 22:43:34 +00:00
/**
* @ expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
*/
2011-02-18 09:37:37 +00:00
public function testSetCalendar ()
2011-02-17 22:43:34 +00:00
{
2011-02-20 22:46:09 +00:00
$formatter = $this -> createStubFormatter ();
2011-02-18 09:37:37 +00:00
$formatter -> setCalendar ( StubIntlDateFormatter :: GREGORIAN );
2011-02-17 22:43:34 +00:00
}
2011-02-18 09:09:33 +00:00
/**
* @ expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
*/
public function testSetLenient ()
{
2011-02-20 22:46:09 +00:00
$formatter = $this -> createStubFormatter ();
2011-02-18 09:09:33 +00:00
$formatter -> setLenient ( true );
}
2011-02-18 09:37:37 +00:00
public function testSetPattern ()
{
2011-02-20 22:46:09 +00:00
$formatter = $this -> createStubFormatter ();
2011-02-18 09:37:37 +00:00
$formatter -> setPattern ( 'yyyy-MM-dd' );
$this -> assertEquals ( 'yyyy-MM-dd' , $formatter -> getPattern ());
}
2011-03-07 02:46:23 +00:00
/**
* @ covers Symfony\Component\Locale\Stub\StubIntlDateFormatter :: getTimeZoneId
* @ dataProvider setTimeZoneIdProvider ()
*/
public function testSetTimeZoneIdStub ( $timeZoneId )
2011-02-18 09:37:37 +00:00
{
2011-02-23 18:08:06 +00:00
$formatter = $this -> createStubFormatter ();
2011-03-07 02:46:23 +00:00
$formatter -> setTimeZoneId ( $timeZoneId );
$this -> assertEquals ( $timeZoneId , $formatter -> getTimeZoneId ());
2011-02-18 21:28:32 +00:00
}
2011-02-18 09:37:37 +00:00
2011-03-07 02:46:23 +00:00
/**
* @ dataProvider setTimeZoneIdProvider ()
*/
public function testSetTimeZoneIdIntl ( $timeZoneId )
2011-02-18 21:28:32 +00:00
{
$this -> skipIfIntlExtensionIsNotLoaded ();
2011-02-23 18:08:06 +00:00
$formatter = $this -> createIntlFormatter ();
2011-03-07 02:46:23 +00:00
$formatter -> setTimeZoneId ( $timeZoneId );
$this -> assertEquals ( $timeZoneId , $formatter -> getTimeZoneId ());
}
public function setTimeZoneIdProvider ()
{
return array (
array ( 'UTC' ),
array ( 'GMT' ),
array ( 'GMT-03:00' ),
array ( 'GMT-0300' ),
array ( 'Europe/Zurich' ),
2011-02-18 09:37:37 +00:00
2011-03-07 02:46:23 +00:00
// When time zone not exists, uses UTC by default
array ( 'Foo/Bar' ),
array ( 'GMT+00:AA' ),
array ( 'GMT+00AA' ),
);
}
/**
* @ expectedException Symfony\Component\Locale\Exception\NotImplementedException
*/
public function testSetTimeZoneIdWithGmtTimeZoneWithMinutesOffsetStub ()
{
$formatter = $this -> createStubFormatter ();
$formatter -> setTimeZoneId ( 'GMT+00:30' );
}
public function testSetTimeZoneIdWithGmtTimeZoneWithMinutesOffsetIntl ()
{
$this -> skipIfIntlExtensionIsNotLoaded ();
$formatter = $this -> createIntlFormatter ();
$formatter -> setTimeZoneId ( 'GMT+00:30' );
$this -> assertEquals ( 'GMT+00:30' , $formatter -> getTimeZoneId ());
2011-02-18 09:37:37 +00:00
}
2011-02-17 22:43:34 +00:00
public function testStaticCreate ()
{
2011-02-20 22:46:09 +00:00
$formatter = StubIntlDateFormatter :: create ( 'en' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT );
2011-02-17 22:43:34 +00:00
$this -> assertInstanceOf ( 'Symfony\Component\Locale\Stub\StubIntlDateFormatter' , $formatter );
}
2011-02-20 22:46:09 +00:00
2011-02-23 18:08:06 +00:00
protected function createStubFormatter ( $pattern = null )
{
return new StubIntlDateFormatter ( 'en' , StubIntlDateFormatter :: MEDIUM , StubIntlDateFormatter :: SHORT , 'UTC' , StubIntlDateFormatter :: GREGORIAN , $pattern );
}
protected function createIntlFormatter ( $pattern = null )
2011-02-20 22:46:09 +00:00
{
2011-02-23 18:08:06 +00:00
return new \IntlDateFormatter ( 'en' , \IntlDateFormatter :: MEDIUM , \IntlDateFormatter :: SHORT , 'UTC' , \IntlDateFormatter :: GREGORIAN , $pattern );
2011-02-20 22:46:09 +00:00
}
2011-03-07 02:46:23 +00:00
protected function createDateTime ( $timestamp = null , $timeZone = null )
{
$dateTime = new \DateTime ();
2011-04-07 08:45:39 +01:00
$dateTime -> setTimestamp ( null === $timestamp ? time () : $timestamp );
$dateTime -> setTimeZone ( new \DateTimeZone ( null === $timeZone ? date_default_timezone_get () : $timeZone ));
2011-03-07 02:46:23 +00:00
return $dateTime ;
}
2011-06-08 18:56:59 +01:00
}