StubIntlDateFormatterTest.php 11 KB

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