StubIntlDateFormatterTest.php 35 KB

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