StubIntlDateFormatterTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /* hour */
  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. /* day of year */
  79. array('D', 0, '1'),
  80. array('D', 86400, '2'), // 1 day
  81. array('D', 31536000, '1'), // 1 year
  82. array('D', 31622400, '2'), // 1 year + 1 day
  83. );
  84. }
  85. /**
  86. * provides data for cases that are broken in icu/intl
  87. */
  88. public function brokenFormatProvider()
  89. {
  90. return array(
  91. /* escaping */
  92. array("'y-'M-'d", 0, 'y-M-d'),
  93. /* weird bugs */
  94. array("WTF 'y-'M", 0, '0T1 y-M'),
  95. array("n-'M", 0, 'n-M'),
  96. );
  97. }
  98. /**
  99. * @expectedException InvalidArgumentException
  100. */
  101. public function testConstructorWithUnsupportedLocale()
  102. {
  103. $formatter = new StubIntlDateFormatter('pt_BR', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  104. }
  105. public function testConstructor()
  106. {
  107. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, 'Y-M-d');
  108. $this->assertEquals('Y-M-d', $formatter->getPattern());
  109. }
  110. /**
  111. * @dataProvider formatProvider
  112. */
  113. public function testFormat($pattern, $timestamp, $expected)
  114. {
  115. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  116. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
  117. if (extension_loaded('intl')) {
  118. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  119. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  120. }
  121. }
  122. /**
  123. * @dataProvider brokenFormatProvider
  124. */
  125. public function testBrokenFormat($pattern, $timestamp, $expected)
  126. {
  127. $this->markTestSkipped('icu/intl has some bugs, thus skipping.');
  128. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  129. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
  130. if (extension_loaded('intl')) {
  131. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  132. $this->assertSame($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  133. }
  134. }
  135. /**
  136. * @expectedException RuntimeException
  137. */
  138. public function testGetCalendar()
  139. {
  140. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  141. $formatter->getCalendar();
  142. }
  143. }