StubIntlDateFormatterTest.php 32 KB

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