StubIntlDateFormatterTest.php 11 KB

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