StubIntlDateFormatterTest.php 9.3 KB

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