StubIntlDateFormatterTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 InvalidArgumentException
  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 testFormat($pattern, $timestamp, $expected)
  33. {
  34. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  35. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
  36. if ($this->isIntlExtensionLoaded()) {
  37. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  38. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  39. }
  40. }
  41. public function formatProvider()
  42. {
  43. return array(
  44. /* general */
  45. array('y-M-d', 0, '1970-1-1'),
  46. array("yyyy.MM.dd G 'at' HH:mm:ss zzz", 0, '1970.01.01 AD at 00:00:00 GMT+00:00'),
  47. array("EEE, MMM d, ''yy", 0, "Thu, Jan 1, '70"),
  48. array('h:mm a', 0, '12:00 AM'),
  49. array('K:mm a, z', 0, '0:00 AM, GMT+00:00'),
  50. array('yyyyy.MMMM.dd GGG hh:mm aaa', 0, '01970.January.01 AD 12:00 AM'),
  51. /* escaping */
  52. array("'M'", 0, 'M'),
  53. array("'yy'", 0, 'yy'),
  54. array("'''yy'", 0, "'yy"),
  55. array("''y", 0, "'1970"),
  56. array("''yy", 0, "'70"),
  57. array("H 'o'' clock'", 0, "0 o' clock"),
  58. /* month */
  59. array('M', 0, '1'),
  60. array('MM', 0, '01'),
  61. array('MMM', 0, 'Jan'),
  62. array('MMMM', 0, 'January'),
  63. array('MMMMM', 0, 'J'),
  64. array('MMMMMM', 0, '000001'),
  65. array('L', 0, '1'),
  66. array('LL', 0, '01'),
  67. array('LLL', 0, 'Jan'),
  68. array('LLLL', 0, 'January'),
  69. array('LLLLL', 0, 'J'),
  70. array('LLLLLL', 0, '000001'),
  71. /* year */
  72. array('y', 0, '1970'),
  73. array('yy', 0, '70'),
  74. array('yyy', 0, '1970'),
  75. array('yyyy', 0, '1970'),
  76. array('yyyyy', 0, '01970'),
  77. array('yyyyyy', 0, '001970'),
  78. /* day */
  79. array('d', 0, '1'),
  80. array('dd', 0, '01'),
  81. array('ddd', 0, '001'),
  82. /* era */
  83. array('G', 0, 'AD'),
  84. array('G', -62167222800, 'BC'),
  85. /* quarter */
  86. array('Q', 0, '1'),
  87. array('QQ', 0, '01'),
  88. array('QQQ', 0, 'Q1'),
  89. array('QQQQ', 0, '1st quarter'),
  90. array('QQQQQ', 0, '1st 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. // 4 months
  97. array('Q', 7776000, '2'),
  98. array('QQ', 7776000, '02'),
  99. array('QQQ', 7776000, 'Q2'),
  100. array('QQQQ', 7776000, '2nd quarter'),
  101. // 7 months
  102. array('QQQQ', 15638400, '3rd quarter'),
  103. // 10 months
  104. array('QQQQ', 23587200, '4th quarter'),
  105. /* 12-hour (1-12) */
  106. array('h', 0, '12'),
  107. array('hh', 0, '12'),
  108. array('hhh', 0, '012'),
  109. array('h', 1, '12'),
  110. array('h', 3600, '1'),
  111. array('h', 43200, '12'), // 12 hours
  112. /* day of year */
  113. array('D', 0, '1'),
  114. array('D', 86400, '2'), // 1 day
  115. array('D', 31536000, '1'), // 1 year
  116. array('D', 31622400, '2'), // 1 year + 1 day
  117. /* day of week */
  118. array('E', 0, 'Thu'),
  119. array('EE', 0, 'Thu'),
  120. array('EEE', 0, 'Thu'),
  121. array('EEEE', 0, 'Thursday'),
  122. array('EEEEE', 0, 'T'),
  123. array('EEEEEE', 0, 'Thu'),
  124. array('E', 1296540000, 'Tue'), // 2011-02-01
  125. array('E', 1296950400, 'Sun'), // 2011-02-06
  126. /* am/pm marker */
  127. array('a', 0, 'AM'),
  128. array('aa', 0, 'AM'),
  129. array('aaa', 0, 'AM'),
  130. array('aaaa', 0, 'AM'),
  131. // 12 hours
  132. array('a', 43200, 'PM'),
  133. array('aa', 43200, 'PM'),
  134. array('aaa', 43200, 'PM'),
  135. array('aaaa', 43200, 'PM'),
  136. /* 24-hour (0-23) */
  137. array('H', 0, '0'),
  138. array('HH', 0, '00'),
  139. array('HHH', 0, '000'),
  140. array('H', 1, '0'),
  141. array('H', 3600, '1'),
  142. array('H', 43200, '12'),
  143. array('H', 46800, '13'),
  144. /* 24-hour (1-24) */
  145. array('k', 0, '24'),
  146. array('kk', 0, '24'),
  147. array('kkk', 0, '024'),
  148. array('k', 1, '24'),
  149. array('k', 3600, '1'),
  150. array('k', 43200, '12'),
  151. array('k', 46800, '13'),
  152. /* 12-hour (0-11) */
  153. array('K', 0, '0'),
  154. array('KK', 0, '00'),
  155. array('KKK', 0, '000'),
  156. array('K', 1, '0'),
  157. array('K', 3600, '1'),
  158. array('K', 43200, '0'), // 12 hours
  159. /* minute */
  160. array('m', 0, '0'),
  161. array('mm', 0, '00'),
  162. array('mmm', 0, '000'),
  163. array('m', 1, '0'),
  164. array('m', 60, '1'),
  165. array('m', 120, '2'),
  166. array('m', 180, '3'),
  167. array('m', 3600, '0'),
  168. array('m', 3660, '1'),
  169. array('m', 43200, '0'), // 12 hours
  170. /* second */
  171. array('s', 0, '0'),
  172. array('ss', 0, '00'),
  173. array('sss', 0, '000'),
  174. array('s', 1, '1'),
  175. array('s', 2, '2'),
  176. array('s', 5, '5'),
  177. array('s', 30, '30'),
  178. array('s', 59, '59'),
  179. array('s', 60, '0'),
  180. array('s', 120, '0'),
  181. array('s', 180, '0'),
  182. array('s', 3600, '0'),
  183. array('s', 3601, '1'),
  184. array('s', 3630, '30'),
  185. array('s', 43200, '0'), // 12 hours
  186. /* timezone */
  187. array('z', 0, 'GMT+00:00'),
  188. array('zz', 0, 'GMT+00:00'),
  189. array('zzz', 0, 'GMT+00:00'),
  190. array('zzzz', 0, 'GMT+00:00'),
  191. array('zzzzz', 0, 'GMT+00:00'),
  192. );
  193. }
  194. /**
  195. * @dataProvider formatWithTimezoneProvider
  196. */
  197. public function testFormatWithTimezone($timestamp, $timezone, $expected)
  198. {
  199. $pattern = 'yyyy-MM-dd HH:mm:ss';
  200. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, $timezone, StubIntlDateFormatter::GREGORIAN, $pattern);
  201. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
  202. if ($this->isIntlExtensionLoaded()) {
  203. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, $timezone, \IntlDateFormatter::GREGORIAN, $pattern);
  204. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  205. }
  206. }
  207. public function formatWithTimezoneProvider()
  208. {
  209. return array(
  210. array(0, 'UTC', '1970-01-01 00:00:00'),
  211. array(0, 'Europe/Zurich', '1970-01-01 01:00:00'),
  212. array(0, 'Europe/Paris', '1970-01-01 01:00:00'),
  213. array(0, 'Africa/Cairo', '1970-01-01 02:00:00'),
  214. array(0, 'Africa/Casablanca', '1970-01-01 00:00:00'),
  215. array(0, 'Africa/Djibouti', '1970-01-01 03:00:00'),
  216. array(0, 'Africa/Johannesburg', '1970-01-01 02:00:00'),
  217. array(0, 'America/Antigua', '1969-12-31 20:00:00'),
  218. array(0, 'America/Toronto', '1969-12-31 19:00:00'),
  219. array(0, 'America/Vancouver', '1969-12-31 16:00:00'),
  220. array(0, 'Asia/Aqtau', '1970-01-01 05:00:00'),
  221. array(0, 'Asia/Bangkok', '1970-01-01 07:00:00'),
  222. array(0, 'Asia/Dubai', '1970-01-01 04:00:00'),
  223. array(0, 'Australia/Brisbane', '1970-01-01 10:00:00'),
  224. array(0, 'Australia/Melbourne', '1970-01-01 10:00:00'),
  225. array(0, 'Europe/Berlin', '1970-01-01 01:00:00'),
  226. array(0, 'Europe/Dublin', '1970-01-01 01:00:00'),
  227. array(0, 'Europe/Warsaw', '1970-01-01 01:00:00'),
  228. array(0, 'Pacific/Fiji', '1970-01-01 12:00:00'),
  229. array(0, 'Foo/Bar', '1970-01-01 00:00:00'),
  230. );
  231. }
  232. /**
  233. * @dataProvider defaultDateFormatsProvider
  234. */
  235. public function testDefaultDateFormats($timestamp, $datetype, $timetype, $expected)
  236. {
  237. $formatter = new StubIntlDateFormatter('en', $datetype, $timetype, 'UTC');
  238. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
  239. if ($this->isIntlExtensionLoaded()) {
  240. $formatter = new \IntlDateFormatter('en', $datetype, $timetype, 'UTC');
  241. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  242. }
  243. }
  244. public function defaultDateFormatsProvider()
  245. {
  246. return array(
  247. array(0, StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, 'Thursday, January 1, 1970'),
  248. array(0, StubIntlDateFormatter::LONG, StubIntlDateFormatter::NONE, 'January 1, 1970'),
  249. array(0, StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::NONE, 'Jan 1, 1970'),
  250. array(0, StubIntlDateFormatter::SHORT, StubIntlDateFormatter::NONE, '1/1/70'),
  251. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::FULL, '12:00:00 AM GMT+00:00'),
  252. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::LONG, '12:00:00 AM GMT+00:00'),
  253. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::MEDIUM, '12:00:00 AM'),
  254. array(0, StubIntlDateFormatter::NONE, StubIntlDateFormatter::SHORT, '12:00 AM'),
  255. );
  256. }
  257. public function testGetPattern()
  258. {
  259. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE, StubIntlDateFormatter::GREGORIAN, 'UTC', 'yyyy-MM-dd');
  260. $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
  261. }
  262. public function testSetPattern()
  263. {
  264. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  265. $formatter->setPattern('yyyy-MM-dd');
  266. $this->assertEquals('yyyy-MM-dd', $formatter->getPattern());
  267. }
  268. public function testGetLocale()
  269. {
  270. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  271. $this->assertEquals('en', $formatter->getLocale());
  272. }
  273. /**
  274. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  275. */
  276. public function testSetLocale()
  277. {
  278. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  279. $formatter->setLocale('pt_BR');
  280. }
  281. public function testGetCalendar()
  282. {
  283. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  284. $this->assertEquals(StubIntlDateFormatter::GREGORIAN, $formatter->getCalendar());
  285. }
  286. /**
  287. * @expectedException Symfony\Component\Locale\Exception\MethodNotImplementedException
  288. */
  289. public function testSetCalendar()
  290. {
  291. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::FULL, StubIntlDateFormatter::NONE);
  292. $formatter->setCalendar(StubIntlDateFormatter::GREGORIAN);
  293. }
  294. }