StubIntlDateFormatterTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Locale\Stub;
  11. require_once __DIR__.'/../TestCase.php';
  12. use Symfony\Component\Locale\Locale;
  13. use Symfony\Component\Locale\Stub\StubIntl;
  14. use Symfony\Component\Locale\Stub\StubIntlDateFormatter;
  15. use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
  16. class StubIntlDateFormatterTest extends LocaleTestCase
  17. {
  18. /**
  19. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  20. */
  21. public function testConstructorWithUnsupportedLocale()
  22. {
  23. $formatter = new StubIntlDateFormatter('pt_BR', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  24. }
  25. public function testConstructor()
  26. {
  27. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, 'y-M-d');
  28. $this->assertEquals('y-M-d', $formatter->getPattern());
  29. }
  30. /**
  31. * When a time zone is not specified, it uses the system default however it returns null in the getter method
  32. * @covers Symfony\Component\Locale\Stub\StubIntlDateFormatter::getTimeZoneId
  33. * @covers Symfony\Component\Locale\Stub\StubIntlDateFormatter::setTimeZoneId
  34. * @see StubIntlDateFormatterTest::testDefaultTimeZoneIntl()
  35. */
  36. public function testConstructorDefaultTimeZoneStub()
  37. {
  38. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  39. $this->assertNull($formatter->getTimeZoneId());
  40. }
  41. public function testConstructorDefaultTimeZoneIntl()
  42. {
  43. $this->skipIfIntlExtensionIsNotLoaded();
  44. $formatter = new \IntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  45. $this->assertNull($formatter->getTimeZoneId());
  46. }
  47. public function testFormatWithUnsupportedTimestampArgument()
  48. {
  49. $formatter = $this->createStubFormatter();
  50. $localtime = array(
  51. 'tm_sec' => 59,
  52. 'tm_min' => 3,
  53. 'tm_hour' => 15,
  54. 'tm_mday' => 15,
  55. 'tm_mon' => 3,
  56. 'tm_year' => 112,
  57. 'tm_wday' => 0,
  58. 'tm_yday' => 105,
  59. 'tm_isdst' => 0
  60. );
  61. try {
  62. $formatter->format($localtime);
  63. } catch (\Exception $e) {
  64. $this->assertInstanceOf('Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException', $e);
  65. if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) {
  66. $this->assertStringEndsWith('Only integer unix timestamps and DateTime objects are supported. Please install the \'intl\' extension for full localization capabilities.', $e->getMessage());
  67. } else {
  68. $this->assertStringEndsWith('Only integer unix timestamps are supported. Please install the \'intl\' extension for full localization capabilities.', $e->getMessage());
  69. }
  70. }
  71. }
  72. /**
  73. * @dataProvider formatProvider
  74. */
  75. public function testFormatStub($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
  76. {
  77. $formatter = $this->createStubFormatter($pattern);
  78. $this->assertSame($expected, $formatter->format($timestamp));
  79. $this->assertSame($errorMessage, StubIntl::getErrorMessage());
  80. $this->assertSame($errorCode, StubIntl::getErrorCode());
  81. $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
  82. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  83. $this->assertSame($errorCode, $formatter->getErrorCode());
  84. $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
  85. }
  86. /**
  87. * @dataProvider formatProvider
  88. */
  89. public function testFormatIntl($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
  90. {
  91. $this->skipIfIntlExtensionIsNotLoaded();
  92. $this->skipIfICUVersionIsTooOld();
  93. $formatter = $this->createIntlFormatter($pattern);
  94. $this->assertSame($expected, $formatter->format($timestamp));
  95. $this->assertSame($errorMessage, intl_get_error_message());
  96. $this->assertSame($errorCode, intl_get_error_code());
  97. $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
  98. }
  99. public function formatProvider()
  100. {
  101. $formatData = array(
  102. /* general */
  103. array('y-M-d', 0, '1970-1-1'),
  104. array("yyyy.MM.dd 'at' HH:mm:ss zzz", 0, '1970.01.01 at 00:00:00 GMT+00:00'),
  105. array("EEE, MMM d, ''yy", 0, "Thu, Jan 1, '70"),
  106. array('h:mm a', 0, '12:00 AM'),
  107. array('K:mm a, z', 0, '0:00 AM, GMT+00:00'),
  108. array('yyyyy.MMMM.dd hh:mm aaa', 0, '01970.January.01 12:00 AM'),
  109. /* escaping */
  110. array("'M'", 0, 'M'),
  111. array("'yy'", 0, 'yy'),
  112. array("'''yy'", 0, "'yy"),
  113. array("''y", 0, "'1970"),
  114. array("''yy", 0, "'70"),
  115. array("H 'o'' clock'", 0, "0 o' clock"),
  116. /* month */
  117. array('M', 0, '1'),
  118. array('MM', 0, '01'),
  119. array('MMM', 0, 'Jan'),
  120. array('MMMM', 0, 'January'),
  121. array('MMMMM', 0, 'J'),
  122. array('MMMMMM', 0, '000001'),
  123. array('L', 0, '1'),
  124. array('LL', 0, '01'),
  125. array('LLL', 0, 'Jan'),
  126. array('LLLL', 0, 'January'),
  127. array('LLLLL', 0, 'J'),
  128. array('LLLLLL', 0, '000001'),
  129. /* year */
  130. array('y', 0, '1970'),
  131. array('yy', 0, '70'),
  132. array('yyy', 0, '1970'),
  133. array('yyyy', 0, '1970'),
  134. array('yyyyy', 0, '01970'),
  135. array('yyyyyy', 0, '001970'),
  136. /* day */
  137. array('d', 0, '1'),
  138. array('dd', 0, '01'),
  139. array('ddd', 0, '001'),
  140. /* quarter */
  141. array('Q', 0, '1'),
  142. array('QQ', 0, '01'),
  143. array('QQQ', 0, 'Q1'),
  144. array('QQQQ', 0, '1st quarter'),
  145. array('QQQQQ', 0, '1st quarter'),
  146. array('q', 0, '1'),
  147. array('qq', 0, '01'),
  148. array('qqq', 0, 'Q1'),
  149. array('qqqq', 0, '1st quarter'),
  150. array('qqqqq', 0, '1st quarter'),
  151. // 4 months
  152. array('Q', 7776000, '2'),
  153. array('QQ', 7776000, '02'),
  154. array('QQQ', 7776000, 'Q2'),
  155. array('QQQQ', 7776000, '2nd quarter'),
  156. // 7 months
  157. array('QQQQ', 15638400, '3rd quarter'),
  158. // 10 months
  159. array('QQQQ', 23587200, '4th quarter'),
  160. /* 12-hour (1-12) */
  161. array('h', 0, '12'),
  162. array('hh', 0, '12'),
  163. array('hhh', 0, '012'),
  164. array('h', 1, '12'),
  165. array('h', 3600, '1'),
  166. array('h', 43200, '12'), // 12 hours
  167. /* day of year */
  168. array('D', 0, '1'),
  169. array('D', 86400, '2'), // 1 day
  170. array('D', 31536000, '1'), // 1 year
  171. array('D', 31622400, '2'), // 1 year + 1 day
  172. /* day of week */
  173. array('E', 0, 'Thu'),
  174. array('EE', 0, 'Thu'),
  175. array('EEE', 0, 'Thu'),
  176. array('EEEE', 0, 'Thursday'),
  177. array('EEEEE', 0, 'T'),
  178. array('EEEEEE', 0, 'Thu'),
  179. array('E', 1296540000, 'Tue'), // 2011-02-01
  180. array('E', 1296950400, 'Sun'), // 2011-02-06
  181. /* am/pm marker */
  182. array('a', 0, 'AM'),
  183. array('aa', 0, 'AM'),
  184. array('aaa', 0, 'AM'),
  185. array('aaaa', 0, 'AM'),
  186. // 12 hours
  187. array('a', 43200, 'PM'),
  188. array('aa', 43200, 'PM'),
  189. array('aaa', 43200, 'PM'),
  190. array('aaaa', 43200, 'PM'),
  191. /* 24-hour (0-23) */
  192. array('H', 0, '0'),
  193. array('HH', 0, '00'),
  194. array('HHH', 0, '000'),
  195. array('H', 1, '0'),
  196. array('H', 3600, '1'),
  197. array('H', 43200, '12'),
  198. array('H', 46800, '13'),
  199. /* 24-hour (1-24) */
  200. array('k', 0, '24'),
  201. array('kk', 0, '24'),
  202. array('kkk', 0, '024'),
  203. array('k', 1, '24'),
  204. array('k', 3600, '1'),
  205. array('k', 43200, '12'),
  206. array('k', 46800, '13'),
  207. /* 12-hour (0-11) */
  208. array('K', 0, '0'),
  209. array('KK', 0, '00'),
  210. array('KKK', 0, '000'),
  211. array('K', 1, '0'),
  212. array('K', 3600, '1'),
  213. array('K', 43200, '0'), // 12 hours
  214. /* minute */
  215. array('m', 0, '0'),
  216. array('mm', 0, '00'),
  217. array('mmm', 0, '000'),
  218. array('m', 1, '0'),
  219. array('m', 60, '1'),
  220. array('m', 120, '2'),
  221. array('m', 180, '3'),
  222. array('m', 3600, '0'),
  223. array('m', 3660, '1'),
  224. array('m', 43200, '0'), // 12 hours
  225. /* second */
  226. array('s', 0, '0'),
  227. array('ss', 0, '00'),
  228. array('sss', 0, '000'),
  229. array('s', 1, '1'),
  230. array('s', 2, '2'),
  231. array('s', 5, '5'),
  232. array('s', 30, '30'),
  233. array('s', 59, '59'),
  234. array('s', 60, '0'),
  235. array('s', 120, '0'),
  236. array('s', 180, '0'),
  237. array('s', 3600, '0'),
  238. array('s', 3601, '1'),
  239. array('s', 3630, '30'),
  240. array('s', 43200, '0'), // 12 hours
  241. /* timezone */
  242. array('z', 0, 'GMT+00:00'),
  243. array('zz', 0, 'GMT+00:00'),
  244. array('zzz', 0, 'GMT+00:00'),
  245. array('zzzz', 0, 'GMT+00:00'),
  246. array('zzzzz', 0, 'GMT+00:00'),
  247. );
  248. // As of PHP 5.3.4, IntlDateFormatter::format() accepts DateTime instances
  249. if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) {
  250. $dateTime = new \DateTime('@0');
  251. /* general, DateTime */
  252. $formatData[] = array('y-M-d', $dateTime, '1970-1-1');
  253. $formatData[] = array("yyyy.MM.dd 'at' HH:mm:ss zzz", $dateTime, '1970.01.01 at 00:00:00 GMT+00:00');
  254. $formatData[] = array("EEE, MMM d, ''yy", $dateTime, "Thu, Jan 1, '70");
  255. $formatData[] = array('h:mm a', $dateTime, '12:00 AM');
  256. $formatData[] = array('K:mm a, z', $dateTime, '0:00 AM, GMT+00:00');
  257. $formatData[] = array('yyyyy.MMMM.dd hh:mm aaa', $dateTime, '01970.January.01 12:00 AM');
  258. }
  259. return $formatData;
  260. }
  261. /**
  262. * @dataProvider formatErrorProvider
  263. */
  264. public function testFormatErrorStub($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
  265. {
  266. $formatter = $this->createStubFormatter($pattern);
  267. $this->assertSame($expected, $formatter->format($timestamp));
  268. $this->assertSame($errorMessage, StubIntl::getErrorMessage());
  269. $this->assertSame($errorCode, StubIntl::getErrorCode());
  270. $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
  271. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  272. $this->assertSame($errorCode, $formatter->getErrorCode());
  273. $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
  274. }
  275. /**
  276. * @dataProvider formatErrorProvider
  277. */
  278. public function testFormatErrorIntl($pattern, $timestamp, $expected, $errorCode = 0, $errorMessage = 'U_ZERO_ERROR')
  279. {
  280. $this->skipIfIntlExtensionIsNotLoaded();
  281. $this->skipIfICUVersionIsTooOld();
  282. $formatter = $this->createIntlFormatter($pattern);
  283. $this->assertSame($expected, $formatter->format($timestamp));
  284. $this->assertSame($errorMessage, intl_get_error_message());
  285. $this->assertSame($errorCode, intl_get_error_code());
  286. $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
  287. }
  288. public function formatErrorProvider()
  289. {
  290. $message = 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR';
  291. if ($this->isGreaterOrEqualThanPhpVersion('5.3.4')) {
  292. $message = 'datefmt_format: takes either an array or an integer timestamp value or a DateTime object: U_ILLEGAL_ARGUMENT_ERROR';
  293. }
  294. return array(
  295. array('y-M-d', '0', false, 1, $message),
  296. array('y-M-d', 'foobar', false, 1, $message),
  297. );
  298. }
  299. /**
  300. * @dataProvider formatWithTimezoneProvider
  301. */
  302. public function testFormatWithTimezoneStub($timestamp, $timezone, $expected)
  303. {
  304. $pattern = 'yyyy-MM-dd HH:mm:ss';
  305. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, $timezone, StubIntlDateFormatter::GREGORIAN, $pattern);
  306. $this->assertSame($expected, $formatter->format($timestamp));
  307. }
  308. /**
  309. * @dataProvider formatWithTimezoneProvider
  310. */
  311. public function testFormatWithTimezoneIntl($timestamp, $timezone, $expected)
  312. {
  313. $this->skipIfIntlExtensionIsNotLoaded();
  314. $pattern = 'yyyy-MM-dd HH:mm:ss';
  315. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $timezone, \IntlDateFormatter::GREGORIAN, $pattern);
  316. $this->assertSame($expected, $formatter->format($timestamp));
  317. }
  318. public function formatWithTimezoneProvider()
  319. {
  320. return array(
  321. array(0, 'UTC', '1970-01-01 00:00:00'),
  322. array(0, 'GMT', '1970-01-01 00:00:00'),
  323. array(0, 'GMT-03:00', '1969-12-31 21:00:00'),
  324. array(0, 'GMT+03:00', '1970-01-01 03:00:00'),
  325. array(0, 'Europe/Zurich', '1970-01-01 01:00:00'),
  326. array(0, 'Europe/Paris', '1970-01-01 01:00:00'),
  327. array(0, 'Africa/Cairo', '1970-01-01 02:00:00'),
  328. array(0, 'Africa/Casablanca', '1970-01-01 00:00:00'),
  329. array(0, 'Africa/Djibouti', '1970-01-01 03:00:00'),
  330. array(0, 'Africa/Johannesburg', '1970-01-01 02:00:00'),
  331. array(0, 'America/Antigua', '1969-12-31 20:00:00'),
  332. array(0, 'America/Toronto', '1969-12-31 19:00:00'),
  333. array(0, 'America/Vancouver', '1969-12-31 16:00:00'),
  334. array(0, 'Asia/Aqtau', '1970-01-01 05:00:00'),
  335. array(0, 'Asia/Bangkok', '1970-01-01 07:00:00'),
  336. array(0, 'Asia/Dubai', '1970-01-01 04:00:00'),
  337. array(0, 'Australia/Brisbane', '1970-01-01 10:00:00'),
  338. array(0, 'Australia/Eucla', '1970-01-01 08:45:00'),
  339. array(0, 'Australia/Melbourne', '1970-01-01 10:00:00'),
  340. array(0, 'Europe/Berlin', '1970-01-01 01:00:00'),
  341. array(0, 'Europe/Dublin', '1970-01-01 01:00:00'),
  342. array(0, 'Europe/Warsaw', '1970-01-01 01:00:00'),
  343. array(0, 'Pacific/Fiji', '1970-01-01 12:00:00'),
  344. // When time zone not exists, uses UTC by default
  345. array(0, 'Foo/Bar', '1970-01-01 00:00:00'),
  346. array(0, 'UTC+04:30', '1970-01-01 00:00:00'),
  347. array(0, 'UTC+04:AA', '1970-01-01 00:00:00'),
  348. );
  349. }
  350. /**
  351. * @expectedException Symfony\Component\Locale\Exception\NotImplementedException
  352. */
  353. public function testFormatWithTimezoneFormatOptionAndDifferentThanUtcStub()
  354. {
  355. $formatter = $this->createStubFormatter('zzzz');
  356. $formatter->setTimeZoneId('Pacific/Fiji');
  357. $formatter->format(0);
  358. }
  359. public function testFormatWithTimezoneFormatOptionAndDifferentThanUtcIntl()
  360. {
  361. $this->skipIfIntlExtensionIsNotLoaded();
  362. $formatter = $this->createIntlFormatter('zzzz');
  363. $formatter->setTimeZoneId('Pacific/Fiji');
  364. $this->assertEquals('Fiji Time', $formatter->format(0));
  365. }
  366. public function testFormatWithGmtTimezoneStub()
  367. {
  368. $formatter = $this->createStubFormatter('zzzz');
  369. $formatter->setTimeZoneId('GMT+03:00');
  370. $this->assertEquals('GMT+03:00', $formatter->format(0));
  371. }
  372. public function testFormatWithGmtTimezoneIntl()
  373. {
  374. $this->skipIfIntlExtensionIsNotLoaded();
  375. $formatter = $this->createIntlFormatter('zzzz');
  376. $formatter->setTimeZoneId('GMT+03:00');
  377. $this->assertEquals('GMT+03:00', $formatter->format(0));
  378. }
  379. public function testFormatWithDefaultTimezoneStub()
  380. {
  381. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  382. $formatter->setPattern('yyyy-MM-dd HH:mm:ss');
  383. $this->assertEquals(
  384. $this->createDateTime(0)->format('Y-m-d H:i:s'),
  385. $formatter->format(0)
  386. );
  387. }
  388. public function testFormatWithDefaultTimezoneIntl()
  389. {
  390. $this->skipIfIntlExtensionIsNotLoaded();
  391. $this->skipIfICUVersionIsTooOld();
  392. $formatter = new \IntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  393. $formatter->setPattern('yyyy-MM-dd HH:mm:ss');
  394. $this->assertEquals(
  395. $this->createDateTime(0)->format('Y-m-d H:i:s'),
  396. $formatter->format(0)
  397. );
  398. }
  399. /**
  400. * @expectedException Symfony\Component\Locale\Exception\NotImplementedException
  401. */
  402. public function testFormatWithUnimplementedCharsStub()
  403. {
  404. $pattern = 'Y';
  405. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  406. $formatter->format(0);
  407. }
  408. /**
  409. * @expectedException Symfony\Component\Locale\Exception\NotImplementedException
  410. */
  411. public function testFormatWithNonIntegerTimestamp()
  412. {
  413. $formatter = $this->createStubFormatter();
  414. $formatter->format(array());
  415. }
  416. /**
  417. * @dataProvider dateAndTimeTypeProvider
  418. */
  419. public function testDateAndTimeTypeStub($timestamp, $datetype, $timetype, $expected)
  420. {
  421. $formatter = new StubIntlDateFormatter('en', $datetype, $timetype, 'UTC');
  422. $this->assertSame($expected, $formatter->format($timestamp));
  423. }
  424. /**
  425. * @dataProvider dateAndTimeTypeProvider
  426. */
  427. public function testDateAndTimeTypeIntl($timestamp, $datetype, $timetype, $expected)
  428. {
  429. $this->skipIfIntlExtensionIsNotLoaded();
  430. $formatter = new \IntlDateFormatter('en', $datetype, $timetype, 'UTC');
  431. $this->assertSame($expected, $formatter->format($timestamp));
  432. }
  433. public function dateAndTimeTypeProvider()
  434. {
  435. return array(
  436. array(0, StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'Thursday, January 1, 1970'),
  437. array(0, StubIntlDateFormatter::LONG, StubIntlDateFormatter::NONE, 'January 1, 1970'),
  438. array(0, StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::NONE, 'Jan 1, 1970'),
  439. array(0, StubIntlDateFormatter::SHORT, StubIntlDateFormatter::NONE, '1/1/70'),
  440. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL, '12:00:00 AM GMT+00:00'),
  441. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::LONG, '12:00:00 AM GMT+00:00'),
  442. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::MEDIUM, '12:00:00 AM'),
  443. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::SHORT, '12:00 AM'),
  444. );
  445. }
  446. public function testGetCalendar()
  447. {
  448. $formatter = $this->createStubFormatter();
  449. $this->assertEquals(StubIntlDateFormatter::GREGORIAN, $formatter->getCalendar());
  450. }
  451. public function testGetDateType()
  452. {
  453. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  454. $this->assertEquals(StubIntlDateFormatter::FULL, $formatter->getDateType());
  455. }
  456. public function testGetErrorCode()
  457. {
  458. $formatter = $this->createStubFormatter();
  459. $this->assertEquals(StubIntl::getErrorCode(), $formatter->getErrorCode());
  460. }
  461. public function testGetErrorMessage()
  462. {
  463. $formatter = $this->createStubFormatter();
  464. $this->assertEquals(StubIntl::getErrorMessage(), $formatter->getErrorMessage());
  465. }
  466. public function testGetLocale()
  467. {
  468. $formatter = $this->createStubFormatter();
  469. $this->assertEquals('en', $formatter->getLocale());
  470. }
  471. public function testGetPattern()
  472. {
  473. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'UTC', StubIntlDateFormatter::GREGORIAN, 'yyyy-MM-dd');
  474. $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
  475. }
  476. public function testGetTimeType()
  477. {
  478. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL);
  479. $this->assertEquals(StubIntlDateFormatter::FULL, $formatter->getTimeType());
  480. }
  481. /**
  482. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  483. */
  484. public function testIsLenient()
  485. {
  486. $formatter = $this->createStubFormatter();
  487. $formatter->isLenient();
  488. }
  489. /**
  490. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  491. */
  492. public function testLocaltime()
  493. {
  494. $formatter = $this->createStubFormatter();
  495. $formatter->localtime('Wednesday, December 31, 1969 4:00:00 PM PT');
  496. }
  497. /**
  498. * @dataProvider parseProvider
  499. */
  500. public function testParseIntl($pattern, $value, $expected)
  501. {
  502. $errorCode = StubIntl::U_ZERO_ERROR;
  503. $errorMessage = 'U_ZERO_ERROR';
  504. $this->skipIfIntlExtensionIsNotLoaded();
  505. $formatter = $this->createIntlFormatter($pattern);
  506. $this->assertSame($expected, $formatter->parse($value));
  507. $this->assertSame($errorMessage, intl_get_error_message());
  508. $this->assertSame($errorCode, intl_get_error_code());
  509. $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
  510. }
  511. /**
  512. * @dataProvider parseProvider
  513. */
  514. public function testParseStub($pattern, $value, $expected)
  515. {
  516. $errorCode = StubIntl::U_ZERO_ERROR;
  517. $errorMessage = 'U_ZERO_ERROR';
  518. $formatter = $this->createStubFormatter($pattern);
  519. $this->assertSame($expected, $formatter->parse($value));
  520. $this->assertSame($errorMessage, StubIntl::getErrorMessage());
  521. $this->assertSame($errorCode, StubIntl::getErrorCode());
  522. $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
  523. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  524. $this->assertSame($errorCode, $formatter->getErrorCode());
  525. $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
  526. }
  527. public function parseProvider()
  528. {
  529. return array(
  530. // years
  531. array('y-M-d', '1970-1-1', 0),
  532. array('yy-M-d', '70-1-1', 0),
  533. // months
  534. array('y-M-d', '1970-1-1', 0),
  535. array('y-MMM-d', '1970-Jan-1', 0),
  536. array('y-MMMM-d', '1970-January-1', 0),
  537. // standalone months
  538. array('y-L-d', '1970-1-1', 0),
  539. array('y-LLL-d', '1970-Jan-1', 0),
  540. array('y-LLLL-d', '1970-January-1', 0),
  541. // days
  542. array('y-M-d', '1970-1-1', 0),
  543. array('y-M-dd', '1970-1-01', 0),
  544. array('y-M-ddd', '1970-1-001', 0),
  545. // 12 hours (1-12)
  546. array('y-M-d h', '1970-1-1 1', 3600),
  547. array('y-M-d h', '1970-1-1 10', 36000),
  548. array('y-M-d hh', '1970-1-1 11', 39600),
  549. array('y-M-d hh', '1970-1-1 12', 0),
  550. array('y-M-d hh a', '1970-1-1 0 AM', 0),
  551. array('y-M-d hh a', '1970-1-1 1 AM', 3600),
  552. array('y-M-d hh a', '1970-1-1 10 AM', 36000),
  553. array('y-M-d hh a', '1970-1-1 11 AM', 39600),
  554. array('y-M-d hh a', '1970-1-1 12 AM', 0),
  555. array('y-M-d hh a', '1970-1-1 23 AM', 82800),
  556. array('y-M-d hh a', '1970-1-1 24 AM', 86400),
  557. array('y-M-d hh a', '1970-1-1 0 PM', 43200),
  558. array('y-M-d hh a', '1970-1-1 1 PM', 46800),
  559. array('y-M-d hh a', '1970-1-1 10 PM', 79200),
  560. array('y-M-d hh a', '1970-1-1 11 PM', 82800),
  561. array('y-M-d hh a', '1970-1-1 12 PM', 43200),
  562. array('y-M-d hh a', '1970-1-1 23 PM', 126000),
  563. array('y-M-d hh a', '1970-1-1 24 PM', 129600),
  564. // 12 hours (0-11)
  565. array('y-M-d K', '1970-1-1 1', 3600),
  566. array('y-M-d K', '1970-1-1 10', 36000),
  567. array('y-M-d KK', '1970-1-1 11', 39600),
  568. array('y-M-d KK', '1970-1-1 12', 43200),
  569. array('y-M-d KK a', '1970-1-1 0 AM', 0),
  570. array('y-M-d KK a', '1970-1-1 1 AM', 3600),
  571. array('y-M-d KK a', '1970-1-1 10 AM', 36000),
  572. array('y-M-d KK a', '1970-1-1 11 AM', 39600),
  573. array('y-M-d KK a', '1970-1-1 12 AM', 43200),
  574. array('y-M-d KK a', '1970-1-1 23 AM', 82800),
  575. array('y-M-d KK a', '1970-1-1 24 AM', 86400),
  576. array('y-M-d KK a', '1970-1-1 0 PM', 43200),
  577. array('y-M-d KK a', '1970-1-1 1 PM', 46800),
  578. array('y-M-d KK a', '1970-1-1 10 PM', 79200),
  579. array('y-M-d KK a', '1970-1-1 11 PM', 82800),
  580. array('y-M-d KK a', '1970-1-1 12 PM', 86400),
  581. array('y-M-d KK a', '1970-1-1 23 PM', 126000),
  582. array('y-M-d KK a', '1970-1-1 24 PM', 129600),
  583. // 24 hours (0-23)
  584. array('y-M-d H', '1970-1-1 0', 0),
  585. array('y-M-d H', '1970-1-1 1', 3600),
  586. array('y-M-d H', '1970-1-1 10', 36000),
  587. array('y-M-d HH', '1970-1-1 11', 39600),
  588. array('y-M-d HH', '1970-1-1 12', 43200),
  589. array('y-M-d HH', '1970-1-1 23', 82800),
  590. array('y-M-d HH a', '1970-1-1 0 AM', 0),
  591. array('y-M-d HH a', '1970-1-1 1 AM', 0),
  592. array('y-M-d HH a', '1970-1-1 10 AM', 0),
  593. array('y-M-d HH a', '1970-1-1 11 AM', 0),
  594. array('y-M-d HH a', '1970-1-1 12 AM', 0),
  595. array('y-M-d HH a', '1970-1-1 23 AM', 0),
  596. array('y-M-d HH a', '1970-1-1 24 AM', 0),
  597. array('y-M-d HH a', '1970-1-1 0 PM', 43200),
  598. array('y-M-d HH a', '1970-1-1 1 PM', 43200),
  599. array('y-M-d HH a', '1970-1-1 10 PM', 43200),
  600. array('y-M-d HH a', '1970-1-1 11 PM', 43200),
  601. array('y-M-d HH a', '1970-1-1 12 PM', 43200),
  602. array('y-M-d HH a', '1970-1-1 23 PM', 43200),
  603. array('y-M-d HH a', '1970-1-1 24 PM', 43200),
  604. // 24 hours (1-24)
  605. array('y-M-d k', '1970-1-1 1', 3600),
  606. array('y-M-d k', '1970-1-1 10', 36000),
  607. array('y-M-d kk', '1970-1-1 11', 39600),
  608. array('y-M-d kk', '1970-1-1 12', 43200),
  609. array('y-M-d kk', '1970-1-1 23', 82800),
  610. array('y-M-d kk', '1970-1-1 24', 0),
  611. array('y-M-d kk a', '1970-1-1 0 AM', 0),
  612. array('y-M-d kk a', '1970-1-1 1 AM', 0),
  613. array('y-M-d kk a', '1970-1-1 10 AM', 0),
  614. array('y-M-d kk a', '1970-1-1 11 AM', 0),
  615. array('y-M-d kk a', '1970-1-1 12 AM', 0),
  616. array('y-M-d kk a', '1970-1-1 23 AM', 0),
  617. array('y-M-d kk a', '1970-1-1 24 AM', 0),
  618. array('y-M-d kk a', '1970-1-1 0 PM', 43200),
  619. array('y-M-d kk a', '1970-1-1 1 PM', 43200),
  620. array('y-M-d kk a', '1970-1-1 10 PM', 43200),
  621. array('y-M-d kk a', '1970-1-1 11 PM', 43200),
  622. array('y-M-d kk a', '1970-1-1 12 PM', 43200),
  623. array('y-M-d kk a', '1970-1-1 23 PM', 43200),
  624. array('y-M-d kk a', '1970-1-1 24 PM', 43200),
  625. // minutes
  626. array('y-M-d HH:m', '1970-1-1 0:1', 60),
  627. array('y-M-d HH:mm', '1970-1-1 0:10', 600),
  628. // seconds
  629. array('y-M-d HH:mm:s', '1970-1-1 00:01:1', 61),
  630. array('y-M-d HH:mm:ss', '1970-1-1 00:01:10', 70),
  631. // timezone
  632. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT-03:00', 10800),
  633. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT-04:00', 14400),
  634. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT-00:00', 0),
  635. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT+03:00', -10800),
  636. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT+04:00', -14400),
  637. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT-0300', 10800),
  638. array('y-M-d HH:mm:ss zzzz', '1970-1-1 00:00:00 GMT+0300', -10800),
  639. // a previous timezoned parsing should not change the timezone for the next parsing
  640. array('y-M-d HH:mm:ss', '1970-1-1 00:00:00', 0),
  641. // AM/PM (already covered by hours tests)
  642. array('y-M-d HH:mm:ss a', '1970-1-1 00:00:00 AM', 0),
  643. array('y-M-d HH:mm:ss a', '1970-1-1 00:00:00 PM', 43200),
  644. // regExp metachars in the pattern string
  645. array('y[M-d', '1970[1-1', 0),
  646. array('y[M/d', '1970[1/1', 0),
  647. // quote characters
  648. array("'M'", 'M', 0),
  649. array("'yy'", 'yy', 0),
  650. array("'''yy'", "'yy", 0),
  651. array("''y", "'1970", 0),
  652. array("H 'o'' clock'", "0 o' clock", 0),
  653. );
  654. }
  655. /**
  656. * @dataProvider parseErrorProvider
  657. */
  658. public function testParseErrorIntl($pattern, $value)
  659. {
  660. $errorCode = StubIntl::U_PARSE_ERROR;
  661. $errorMessage = 'Date parsing failed: U_PARSE_ERROR';
  662. $this->skipIfIntlExtensionIsNotLoaded();
  663. $formatter = $this->createIntlFormatter($pattern);
  664. $this->assertFalse($formatter->parse($value));
  665. $this->assertSame($errorMessage, intl_get_error_message());
  666. $this->assertSame($errorCode, intl_get_error_code());
  667. $this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
  668. }
  669. /**
  670. * @dataProvider parseErrorProvider
  671. */
  672. public function testParseErrorStub($pattern, $value)
  673. {
  674. $errorCode = StubIntl::U_PARSE_ERROR;
  675. $errorMessage = 'Date parsing failed: U_PARSE_ERROR';
  676. $formatter = $this->createStubFormatter($pattern);
  677. $this->assertFalse($formatter->parse($value));
  678. $this->assertSame($errorMessage, StubIntl::getErrorMessage());
  679. $this->assertSame($errorCode, StubIntl::getErrorCode());
  680. $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
  681. $this->assertSame($errorMessage, $formatter->getErrorMessage());
  682. $this->assertSame($errorCode, $formatter->getErrorCode());
  683. $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
  684. }
  685. public function parseErrorProvider()
  686. {
  687. return array(
  688. array('y-M-d', '1970/1/1'),
  689. array('yy-M-d', '70/1/1'),
  690. // 1 char month
  691. array('y-MMMMM-d', '1970-J-1'),
  692. array('y-MMMMM-d', '1970-S-1'),
  693. // standalone 1 char month
  694. array('y-LLLLL-d', '1970-J-1'),
  695. array('y-LLLLL-d', '1970-S-1'),
  696. );
  697. }
  698. /**
  699. * Just to document the differences between the stub and the intl implementations. The intl can parse
  700. * any of the tested formats alone. The stub does not implement them as it would be needed to add more
  701. * abstraction, passing more context to the transformers objects. Any of the formats are ignored alone
  702. * or with date/time data (years, months, days, hours, minutes and seconds).
  703. *
  704. * Also in intl, format like 'ss E' for '10 2' (2nd day of year + 10 seconds) are added, then we have
  705. * 86,400 seconds (24h * 60min * 60s) + 10 seconds
  706. *
  707. * @dataProvider parseDifferences()
  708. */
  709. public function testParseDifferencesStub($pattern, $value, $stubExpected, $intlExpected)
  710. {
  711. $formatter = $this->createStubFormatter($pattern);
  712. $this->assertSame($stubExpected, $formatter->parse($value));
  713. }
  714. /**
  715. * @dataProvider parseDifferences()
  716. */
  717. public function testParseDifferencesIntl($pattern, $value, $stubExpected, $intlExpected)
  718. {
  719. $this->skipIfIntlExtensionIsNotLoaded();
  720. $this->skipIfICUVersionIsTooOld();
  721. $formatter = $this->createIntlFormatter($pattern);
  722. $this->assertSame($intlExpected, $formatter->parse($value));
  723. }
  724. public function parseDifferences()
  725. {
  726. return array(
  727. // AM/PM, ignored if alone
  728. array('a', 'AM', 0, 0),
  729. array('a', 'PM', 0, 43200),
  730. // day of week
  731. array('E', 'Thu', 0, 0),
  732. array('EE', 'Thu', 0, 0),
  733. array('EEE', 'Thu', 0, 0),
  734. array('EEEE', 'Thursday', 0, 0),
  735. array('EEEEE', 'T', 0, 432000),
  736. array('EEEEEE', 'Thu', 0, 0),
  737. // day of year
  738. array('D', '1', 0, 0),
  739. array('D', '2', 0, 86400),
  740. // quarter
  741. array('Q', '1', 0, 0),
  742. array('QQ', '01', 0, 0),
  743. array('QQQ', 'Q1', 0, 0),
  744. array('QQQQ', '1st quarter', 0, 0),
  745. array('QQQQQ', '1st quarter', 0, 0),
  746. array('Q', '2', 0, 7776000),
  747. array('QQ', '02', 0, 7776000),
  748. array('QQQ', 'Q2', 0, 7776000),
  749. array('QQQQ', '2nd quarter', 0, 7776000),
  750. array('QQQQQ', '2nd quarter', 0, 7776000),
  751. array('q', '1', 0, 0),
  752. array('qq', '01', 0, 0),
  753. array('qqq', 'Q1', 0, 0),
  754. array('qqqq', '1st quarter', 0, 0),
  755. array('qqqqq', '1st quarter', 0, 0),
  756. );
  757. }
  758. public function testParseWithNullPositionValueStub()
  759. {
  760. $position = null;
  761. $formatter = $this->createStubFormatter('y');
  762. $this->assertSame(0, $formatter->parse('1970', $position));
  763. $this->assertNull($position);
  764. }
  765. /**
  766. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentNotImplementedException
  767. */
  768. public function testParseWithNotNullPositionValueStub()
  769. {
  770. $position = 0;
  771. $formatter = $this->createStubFormatter('y');
  772. $this->assertSame(0, $formatter->parse('1970', $position));
  773. }
  774. /**
  775. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  776. */
  777. public function testSetCalendar()
  778. {
  779. $formatter = $this->createStubFormatter();
  780. $formatter->setCalendar(StubIntlDateFormatter::GREGORIAN);
  781. }
  782. /**
  783. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  784. */
  785. public function testSetLenient()
  786. {
  787. $formatter = $this->createStubFormatter();
  788. $formatter->setLenient(true);
  789. }
  790. public function testSetPattern()
  791. {
  792. $formatter = $this->createStubFormatter();
  793. $formatter->setPattern('yyyy-MM-dd');
  794. $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
  795. }
  796. /**
  797. * @covers Symfony\Component\Locale\Stub\StubIntlDateFormatter::getTimeZoneId
  798. * @dataProvider setTimeZoneIdProvider()
  799. */
  800. public function testSetTimeZoneIdStub($timeZoneId)
  801. {
  802. $formatter = $this->createStubFormatter();
  803. $formatter->setTimeZoneId($timeZoneId);
  804. $this->assertEquals($timeZoneId, $formatter->getTimeZoneId());
  805. }
  806. /**
  807. * @dataProvider setTimeZoneIdProvider()
  808. */
  809. public function testSetTimeZoneIdIntl($timeZoneId)
  810. {
  811. $this->skipIfIntlExtensionIsNotLoaded();
  812. $formatter = $this->createIntlFormatter();
  813. $formatter->setTimeZoneId($timeZoneId);
  814. $this->assertEquals($timeZoneId, $formatter->getTimeZoneId());
  815. }
  816. public function setTimeZoneIdProvider()
  817. {
  818. return array(
  819. array('UTC'),
  820. array('GMT'),
  821. array('GMT-03:00'),
  822. array('GMT-0300'),
  823. array('Europe/Zurich'),
  824. // When time zone not exists, uses UTC by default
  825. array('Foo/Bar'),
  826. array('GMT+00:AA'),
  827. array('GMT+00AA'),
  828. );
  829. }
  830. /**
  831. * @expectedException Symfony\Component\Locale\Exception\NotImplementedException
  832. */
  833. public function testSetTimeZoneIdWithGmtTimeZoneWithMinutesOffsetStub()
  834. {
  835. $formatter = $this->createStubFormatter();
  836. $formatter->setTimeZoneId('GMT+00:30');
  837. }
  838. public function testSetTimeZoneIdWithGmtTimeZoneWithMinutesOffsetIntl()
  839. {
  840. $this->skipIfIntlExtensionIsNotLoaded();
  841. $formatter = $this->createIntlFormatter();
  842. $formatter->setTimeZoneId('GMT+00:30');
  843. $this->assertEquals('GMT+00:30', $formatter->getTimeZoneId());
  844. }
  845. public function testStaticCreate()
  846. {
  847. $formatter = StubIntlDateFormatter::create('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  848. $this->assertInstanceOf('Symfony\Component\Locale\Stub\StubIntlDateFormatter', $formatter);
  849. }
  850. protected function createStubFormatter($pattern = null)
  851. {
  852. return new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  853. }
  854. protected function createIntlFormatter($pattern = null)
  855. {
  856. return new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  857. }
  858. protected function createDateTime($timestamp = null, $timeZone = null)
  859. {
  860. $dateTime = new \DateTime();
  861. $dateTime->setTimestamp(null === $timestamp ? time() : $timestamp);
  862. $dateTime->setTimeZone(new \DateTimeZone(null === $timeZone ? date_default_timezone_get() : $timeZone));
  863. return $dateTime;
  864. }
  865. }