StubIntlDateFormatterTest.php 35 KB

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