StubIntlDateFormatterTest.php 9.5 KB

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