123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * 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;
- require_once __DIR__.'/../TestCase.php';
- use Symfony\Component\Locale\Locale;
- use Symfony\Component\Locale\Stub\StubIntlDateFormatter;
- use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
- class StubIntlDateFormatterTest extends LocaleTestCase
- {
- /**
- * @expectedException InvalidArgumentException
- */
- public function testConstructorWithUnsupportedLocale()
- {
- $formatter = new StubIntlDateFormatter('pt_BR', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
- }
- public function testConstructor()
- {
- $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, 'Y-M-d');
- $this->assertEquals('Y-M-d', $formatter->getPattern());
- }
- /**
- * @dataProvider formatProvider
- */
- public function testFormat($pattern, $timestamp, $expected)
- {
- $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
- $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
- if ($this->isIntlExtensionLoaded()) {
- $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
- $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
- }
- }
- public function formatProvider()
- {
- return array(
- /* general */
- array('y-M-d', 0, '1970-1-1'),
- array("yyyy.MM.dd G 'at' HH:mm:ss zzz", 0, '1970.01.01 AD at 00:00:00 GMT+00:00'),
- array("EEE, MMM d, ''yy", 0, "Thu, Jan 1, '70"),
- array('h:mm a', 0, '12:00 AM'),
- array('K:mm a, z', 0, '0:00 AM, GMT+00:00'),
- array('yyyyy.MMMM.dd GGG hh:mm aaa', 0, '01970.January.01 AD 12:00 AM'),
- /* escaping */
- array("'M'", 0, 'M'),
- array("'yy'", 0, 'yy'),
- array("'''yy'", 0, "'yy"),
- array("''y", 0, "'1970"),
- array("''yy", 0, "'70"),
- array("H 'o'' clock'", 0, "0 o' clock"),
- /* month */
- array('M', 0, '1'),
- array('MM', 0, '01'),
- array('MMM', 0, 'Jan'),
- array('MMMM', 0, 'January'),
- array('MMMMM', 0, 'J'),
- array('MMMMMM', 0, '000001'),
- array('L', 0, '1'),
- array('LL', 0, '01'),
- array('LLL', 0, 'Jan'),
- array('LLLL', 0, 'January'),
- array('LLLLL', 0, 'J'),
- array('LLLLLL', 0, '000001'),
- /* year */
- 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'),
- /* era */
- array('G', 0, 'AD'),
- array('G', -62167222800, 'BC'),
- /* 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'),
- // 4 months
- array('Q', 7776000, '2'),
- array('QQ', 7776000, '02'),
- array('QQQ', 7776000, 'Q2'),
- array('QQQQ', 7776000, '2nd quarter'),
- // 7 months
- array('QQQQ', 15638400, '3rd quarter'),
- // 10 months
- array('QQQQ', 23587200, '4th quarter'),
- /* 12-hour (1-12) */
- array('h', 0, '12'),
- array('hh', 0, '12'),
- array('hhh', 0, '012'),
- array('h', 1, '12'),
- array('h', 3600, '1'),
- array('h', 43200, '12'), // 12 hours
- /* 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
- /* 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'),
- 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'),
- /* 24-hour (0-23) */
- 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'),
- /* 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'),
- /* 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
- /* 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
- /* 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
- /* 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'),
- );
- }
- /**
- * @dataProvider formatWithTimezoneProvider
- */
- public function testFormatWithTimezone($timestamp, $timezone, $expected)
- {
- $pattern = 'yyyy-MM-dd HH:mm:ss';
- $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, $timezone, StubIntlDateFormatter::GREGORIAN, $pattern);
- $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
- if ($this->isIntlExtensionLoaded()) {
- $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $timezone, \IntlDateFormatter::GREGORIAN, $pattern);
- $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
- }
- }
- public function formatWithTimezoneProvider()
- {
- return array(
- array(0, 'UTC', '1970-01-01 00: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'),
- 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'),
- array(0, 'Foo/Bar', '1970-01-01 00:00:00'),
- );
- }
- /**
- * @dataProvider defaultDateFormatsProvider
- */
- public function testDefaultDateFormats($timestamp, $datetype, $timetype, $expected)
- {
- $formatter = new StubIntlDateFormatter('en', $datetype, $timetype, 'UTC');
- $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
- if ($this->isIntlExtensionLoaded()) {
- $formatter = new \IntlDateFormatter('en', $datetype, $timetype, 'UTC');
- $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
- }
- }
- public function defaultDateFormatsProvider()
- {
- 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'),
- );
- }
- public function testGetPattern()
- {
- $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, StubIntlDateFormatter::GREGORIAN, 'UTC', 'yyyy-MM-dd');
- $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
- }
- public function testSetPattern()
- {
- $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
- $formatter->setPattern('yyyy-MM-dd');
- $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
- }
- public function testGetLocale()
- {
- $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
- $this->assertEquals('en', $formatter->getLocale());
- }
- /**
- * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
- */
- public function testSetLocale()
- {
- $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
- $formatter->setLocale('pt_BR');
- }
- public function testGetCalendar()
- {
- $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
- $this->assertEquals(StubIntlDateFormatter::GREGORIAN, $formatter->getCalendar());
- }
- /**
- * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
- */
- public function testSetCalendar()
- {
- $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
- $formatter->setCalendar(StubIntlDateFormatter::GREGORIAN);
- }
- }
|