StubIntlDateFormatterTest.php 10.0 KB

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