StubIntlDateFormatterTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\StubIntlDateFormatter;
  14. use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
  15. class StubIntlDateFormatterTest extends LocaleTestCase
  16. {
  17. /**
  18. * @expectedException Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedException
  19. */
  20. public function testConstructorWithUnsupportedLocale()
  21. {
  22. $formatter = new StubIntlDateFormatter('pt_BR', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  23. }
  24. public function testConstructor()
  25. {
  26. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, 'Y-M-d');
  27. $this->assertEquals('Y-M-d', $formatter->getPattern());
  28. }
  29. /**
  30. * @dataProvider formatProvider
  31. */
  32. public function testFormatStub($pattern, $timestamp, $expected)
  33. {
  34. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  35. $this->assertSame($expected, $formatter->format($timestamp));
  36. }
  37. /**
  38. * @dataProvider formatProvider
  39. */
  40. public function testFormatIntl($pattern, $timestamp, $expected)
  41. {
  42. $this->skipIfIntlExtensionIsNotLoaded();
  43. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  44. $this->assertSame($expected, $formatter->format($timestamp));
  45. }
  46. public function formatProvider()
  47. {
  48. return array(
  49. /* general */
  50. array('y-M-d', 0, '1970-1-1'),
  51. array("yyyy.MM.dd G 'at' HH:mm:ss zzz", 0, '1970.01.01 AD at 00:00:00 GMT+00:00'),
  52. array("EEE, MMM d, ''yy", 0, "Thu, Jan 1, '70"),
  53. array('h:mm a', 0, '12:00 AM'),
  54. array('K:mm a, z', 0, '0:00 AM, GMT+00:00'),
  55. array('yyyyy.MMMM.dd GGG hh:mm aaa', 0, '01970.January.01 AD 12:00 AM'),
  56. /* escaping */
  57. array("'M'", 0, 'M'),
  58. array("'yy'", 0, 'yy'),
  59. array("'''yy'", 0, "'yy"),
  60. array("''y", 0, "'1970"),
  61. array("''yy", 0, "'70"),
  62. array("H 'o'' clock'", 0, "0 o' clock"),
  63. /* month */
  64. array('M', 0, '1'),
  65. array('MM', 0, '01'),
  66. array('MMM', 0, 'Jan'),
  67. array('MMMM', 0, 'January'),
  68. array('MMMMM', 0, 'J'),
  69. array('MMMMMM', 0, '000001'),
  70. array('L', 0, '1'),
  71. array('LL', 0, '01'),
  72. array('LLL', 0, 'Jan'),
  73. array('LLLL', 0, 'January'),
  74. array('LLLLL', 0, 'J'),
  75. array('LLLLLL', 0, '000001'),
  76. /* year */
  77. array('y', 0, '1970'),
  78. array('yy', 0, '70'),
  79. array('yyy', 0, '1970'),
  80. array('yyyy', 0, '1970'),
  81. array('yyyyy', 0, '01970'),
  82. array('yyyyyy', 0, '001970'),
  83. /* day */
  84. array('d', 0, '1'),
  85. array('dd', 0, '01'),
  86. array('ddd', 0, '001'),
  87. /* era */
  88. array('G', 0, 'AD'),
  89. array('G', -62167222800, 'BC'),
  90. /* quarter */
  91. array('Q', 0, '1'),
  92. array('QQ', 0, '01'),
  93. array('QQQ', 0, 'Q1'),
  94. array('QQQQ', 0, '1st quarter'),
  95. array('QQQQQ', 0, '1st quarter'),
  96. array('q', 0, '1'),
  97. array('qq', 0, '01'),
  98. array('qqq', 0, 'Q1'),
  99. array('qqqq', 0, '1st quarter'),
  100. array('qqqqq', 0, '1st quarter'),
  101. // 4 months
  102. array('Q', 7776000, '2'),
  103. array('QQ', 7776000, '02'),
  104. array('QQQ', 7776000, 'Q2'),
  105. array('QQQQ', 7776000, '2nd quarter'),
  106. // 7 months
  107. array('QQQQ', 15638400, '3rd quarter'),
  108. // 10 months
  109. array('QQQQ', 23587200, '4th quarter'),
  110. /* 12-hour (1-12) */
  111. array('h', 0, '12'),
  112. array('hh', 0, '12'),
  113. array('hhh', 0, '012'),
  114. array('h', 1, '12'),
  115. array('h', 3600, '1'),
  116. array('h', 43200, '12'), // 12 hours
  117. /* day of year */
  118. array('D', 0, '1'),
  119. array('D', 86400, '2'), // 1 day
  120. array('D', 31536000, '1'), // 1 year
  121. array('D', 31622400, '2'), // 1 year + 1 day
  122. /* day of week */
  123. array('E', 0, 'Thu'),
  124. array('EE', 0, 'Thu'),
  125. array('EEE', 0, 'Thu'),
  126. array('EEEE', 0, 'Thursday'),
  127. array('EEEEE', 0, 'T'),
  128. array('EEEEEE', 0, 'Thu'),
  129. array('E', 1296540000, 'Tue'), // 2011-02-01
  130. array('E', 1296950400, 'Sun'), // 2011-02-06
  131. /* am/pm marker */
  132. array('a', 0, 'AM'),
  133. array('aa', 0, 'AM'),
  134. array('aaa', 0, 'AM'),
  135. array('aaaa', 0, 'AM'),
  136. // 12 hours
  137. array('a', 43200, 'PM'),
  138. array('aa', 43200, 'PM'),
  139. array('aaa', 43200, 'PM'),
  140. array('aaaa', 43200, 'PM'),
  141. /* 24-hour (0-23) */
  142. array('H', 0, '0'),
  143. array('HH', 0, '00'),
  144. array('HHH', 0, '000'),
  145. array('H', 1, '0'),
  146. array('H', 3600, '1'),
  147. array('H', 43200, '12'),
  148. array('H', 46800, '13'),
  149. /* 24-hour (1-24) */
  150. array('k', 0, '24'),
  151. array('kk', 0, '24'),
  152. array('kkk', 0, '024'),
  153. array('k', 1, '24'),
  154. array('k', 3600, '1'),
  155. array('k', 43200, '12'),
  156. array('k', 46800, '13'),
  157. /* 12-hour (0-11) */
  158. array('K', 0, '0'),
  159. array('KK', 0, '00'),
  160. array('KKK', 0, '000'),
  161. array('K', 1, '0'),
  162. array('K', 3600, '1'),
  163. array('K', 43200, '0'), // 12 hours
  164. /* minute */
  165. array('m', 0, '0'),
  166. array('mm', 0, '00'),
  167. array('mmm', 0, '000'),
  168. array('m', 1, '0'),
  169. array('m', 60, '1'),
  170. array('m', 120, '2'),
  171. array('m', 180, '3'),
  172. array('m', 3600, '0'),
  173. array('m', 3660, '1'),
  174. array('m', 43200, '0'), // 12 hours
  175. /* second */
  176. array('s', 0, '0'),
  177. array('ss', 0, '00'),
  178. array('sss', 0, '000'),
  179. array('s', 1, '1'),
  180. array('s', 2, '2'),
  181. array('s', 5, '5'),
  182. array('s', 30, '30'),
  183. array('s', 59, '59'),
  184. array('s', 60, '0'),
  185. array('s', 120, '0'),
  186. array('s', 180, '0'),
  187. array('s', 3600, '0'),
  188. array('s', 3601, '1'),
  189. array('s', 3630, '30'),
  190. array('s', 43200, '0'), // 12 hours
  191. /* timezone */
  192. array('z', 0, 'GMT+00:00'),
  193. array('zz', 0, 'GMT+00:00'),
  194. array('zzz', 0, 'GMT+00:00'),
  195. array('zzzz', 0, 'GMT+00:00'),
  196. array('zzzzz', 0, 'GMT+00:00'),
  197. );
  198. }
  199. /**
  200. * @dataProvider formatWithTimezoneProvider
  201. */
  202. public function testFormatWithTimezoneStub($timestamp, $timezone, $expected)
  203. {
  204. $pattern = 'yyyy-MM-dd HH:mm:ss';
  205. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, $timezone, StubIntlDateFormatter::GREGORIAN, $pattern);
  206. $this->assertSame($expected, $formatter->format($timestamp));
  207. }
  208. /**
  209. * @dataProvider formatWithTimezoneProvider
  210. */
  211. public function testFormatWithTimezoneIntl($timestamp, $timezone, $expected)
  212. {
  213. $this->skipIfIntlExtensionIsNotLoaded();
  214. $pattern = 'yyyy-MM-dd HH:mm:ss';
  215. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $timezone, \IntlDateFormatter::GREGORIAN, $pattern);
  216. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  217. }
  218. public function formatWithTimezoneProvider()
  219. {
  220. return array(
  221. array(0, 'UTC', '1970-01-01 00:00:00'),
  222. array(0, 'Europe/Zurich', '1970-01-01 01:00:00'),
  223. array(0, 'Europe/Paris', '1970-01-01 01:00:00'),
  224. array(0, 'Africa/Cairo', '1970-01-01 02:00:00'),
  225. array(0, 'Africa/Casablanca', '1970-01-01 00:00:00'),
  226. array(0, 'Africa/Djibouti', '1970-01-01 03:00:00'),
  227. array(0, 'Africa/Johannesburg', '1970-01-01 02:00:00'),
  228. array(0, 'America/Antigua', '1969-12-31 20:00:00'),
  229. array(0, 'America/Toronto', '1969-12-31 19:00:00'),
  230. array(0, 'America/Vancouver', '1969-12-31 16:00:00'),
  231. array(0, 'Asia/Aqtau', '1970-01-01 05:00:00'),
  232. array(0, 'Asia/Bangkok', '1970-01-01 07:00:00'),
  233. array(0, 'Asia/Dubai', '1970-01-01 04:00:00'),
  234. array(0, 'Australia/Brisbane', '1970-01-01 10:00:00'),
  235. array(0, 'Australia/Melbourne', '1970-01-01 10:00:00'),
  236. array(0, 'Europe/Berlin', '1970-01-01 01:00:00'),
  237. array(0, 'Europe/Dublin', '1970-01-01 01:00:00'),
  238. array(0, 'Europe/Warsaw', '1970-01-01 01:00:00'),
  239. array(0, 'Pacific/Fiji', '1970-01-01 12:00:00'),
  240. array(0, 'Foo/Bar', '1970-01-01 00:00:00'),
  241. );
  242. }
  243. /**
  244. * @dataProvider dateAndTimeTypeProvider
  245. */
  246. public function testDateAndTimeTypeStub($timestamp, $datetype, $timetype, $expected)
  247. {
  248. $formatter = new StubIntlDateFormatter('en', $datetype, $timetype, 'UTC');
  249. $this->assertSame($expected, $formatter->format($timestamp));
  250. }
  251. /**
  252. * @dataProvider dateAndTimeTypeProvider
  253. */
  254. public function testDateAndTimeTypeIntl($timestamp, $datetype, $timetype, $expected)
  255. {
  256. $this->skipIfIntlExtensionIsNotLoaded();
  257. $formatter = new \IntlDateFormatter('en', $datetype, $timetype, 'UTC');
  258. $this->assertSame($expected, $formatter->format($timestamp));
  259. }
  260. public function dateAndTimeTypeProvider()
  261. {
  262. return array(
  263. array(0, StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'Thursday, January 1, 1970'),
  264. array(0, StubIntlDateFormatter::LONG, StubIntlDateFormatter::NONE, 'January 1, 1970'),
  265. array(0, StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::NONE, 'Jan 1, 1970'),
  266. array(0, StubIntlDateFormatter::SHORT, StubIntlDateFormatter::NONE, '1/1/70'),
  267. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL, '12:00:00 AM GMT+00:00'),
  268. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::LONG, '12:00:00 AM GMT+00:00'),
  269. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::MEDIUM, '12:00:00 AM'),
  270. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::SHORT, '12:00 AM'),
  271. );
  272. }
  273. public function testGetCalendar()
  274. {
  275. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  276. $this->assertEquals(StubIntlDateFormatter::GREGORIAN, $formatter->getCalendar());
  277. }
  278. public function testGetDateType()
  279. {
  280. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  281. $this->assertEquals(StubIntlDateFormatter::FULL, $formatter->getDateType());
  282. }
  283. /**
  284. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  285. */
  286. public function testGetErrorCode()
  287. {
  288. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  289. $formatter->getErrorCode();
  290. }
  291. /**
  292. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  293. */
  294. public function testGetErrorMessage()
  295. {
  296. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  297. $formatter->getErrorMessage();
  298. }
  299. public function testGetLocale()
  300. {
  301. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  302. $this->assertEquals('en', $formatter->getLocale());
  303. }
  304. public function testGetPattern()
  305. {
  306. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, StubIntlDateFormatter::GREGORIAN, 'UTC', 'yyyy-MM-dd');
  307. $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
  308. }
  309. public function testGetTimeType()
  310. {
  311. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL);
  312. $this->assertEquals(StubIntlDateFormatter::FULL, $formatter->getTimeType());
  313. }
  314. /**
  315. * @dataProvider timeZoneIdProvider
  316. */
  317. public function testGetTimeZoneIdStub($timeZoneId)
  318. {
  319. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, $timeZoneId);
  320. $this->assertEquals($timeZoneId, $formatter->getTimeZoneId());
  321. }
  322. /**
  323. * @dataProvider timeZoneIdProvider
  324. */
  325. public function testGetTimeZoneIdIntl($timeZoneId)
  326. {
  327. $this->skipIfIntlExtensionIsNotLoaded();
  328. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, $timeZoneId);
  329. $this->assertEquals($timeZoneId, $formatter->getTimeZoneId());
  330. }
  331. public function timeZoneIdProvider()
  332. {
  333. return array(
  334. array('Europe/Zurich'),
  335. array('Asia/Dubai'),
  336. );
  337. }
  338. /**
  339. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  340. */
  341. public function testIsLenient()
  342. {
  343. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  344. $formatter->isLenient();
  345. }
  346. /**
  347. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  348. */
  349. public function testLocaltime()
  350. {
  351. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  352. $formatter->localtime('Wednesday, December 31, 1969 4:00:00 PM PT');
  353. }
  354. /**
  355. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  356. */
  357. public function testParse()
  358. {
  359. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  360. $formatter->parse('Wednesday, December 31, 1969 4:00:00 PM PT');
  361. }
  362. /**
  363. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  364. */
  365. public function testSetCalendar()
  366. {
  367. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  368. $formatter->setCalendar(StubIntlDateFormatter::GREGORIAN);
  369. }
  370. /**
  371. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  372. */
  373. public function testSetLenient()
  374. {
  375. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  376. $formatter->setLenient(true);
  377. }
  378. public function testSetPattern()
  379. {
  380. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  381. $formatter->setPattern('yyyy-MM-dd');
  382. $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
  383. }
  384. public function testSetTimeZoneIdStub()
  385. {
  386. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'UTC');
  387. $this->assertEquals('UTC', $formatter->getTimeZoneId());
  388. $formatter->setTimeZoneId('Europe/Zurich');
  389. $this->assertEquals('Europe/Zurich', $formatter->getTimeZoneId());
  390. }
  391. public function testSetTimeZoneIdIntl()
  392. {
  393. $this->skipIfIntlExtensionIsNotLoaded();
  394. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'UTC');
  395. $this->assertEquals('UTC', $formatter->getTimeZoneId());
  396. $formatter->setTimeZoneId('Europe/Zurich');
  397. $this->assertEquals('Europe/Zurich', $formatter->getTimeZoneId());
  398. }
  399. public function testStaticCreate()
  400. {
  401. $formatter = StubIntlDateFormatter::create('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  402. $this->assertInstanceOf('Symfony\Component\Locale\Stub\StubIntlDateFormatter', $formatter);
  403. }
  404. }