StubIntlDateFormatterTest.php 36 KB

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