StubIntlDateFormatterTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /* months */
  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, '00001'),
  32. /* years */
  33. array('y', 0, '1970'),
  34. array('yy', 0, '70'),
  35. array('yyy', 0, '1970'),
  36. array('yyyy', 0, '1970'),
  37. array('yyyyy', 0, '01970'),
  38. array('yyyyyy', 0, '001970'),
  39. /* day */
  40. array('d', 0, '1'),
  41. array('dd', 0, '01'),
  42. array('ddd', 0, '001'),
  43. /* era */
  44. array('G', 0, 'AD'),
  45. array('G', -62167222800, 'BC'),
  46. /* quarter */
  47. array('Q', 0, '1'),
  48. array('QQ', 0, '01'),
  49. array('QQQ', 0, 'Q1'),
  50. array('QQQQ', 0, '1st quarter'),
  51. array('QQQQQ', 0, '1st quarter'),
  52. array('q', 0, '1'),
  53. array('qq', 0, '01'),
  54. array('qqq', 0, 'Q1'),
  55. array('qqqq', 0, '1st quarter'),
  56. array('qqqqq', 0, '1st quarter'),
  57. array('Q', 7776000, '2'),
  58. array('QQ', 7776000, '02'),
  59. array('QQQ', 7776000, 'Q2'),
  60. array('QQQQ', 7776000, '2nd quarter'),
  61. array('QQQQ', 15638400, '3rd quarter'),
  62. array('QQQQ', 23587200, '4th quarter'),
  63. );
  64. }
  65. /**
  66. * provides data for cases that are broken in icu/intl
  67. */
  68. public function brokenFormatProvider()
  69. {
  70. return array(
  71. /* escaping */
  72. array("'y-'M-'d", 0, 'y-M-d'),
  73. array("WTF 'y-'M", 0, '0T1 y-M'),
  74. array("n-'M", 0, 'n-M'),
  75. );
  76. }
  77. /**
  78. * @expectedException InvalidArgumentException
  79. */
  80. public function testConstructorWithUnsupportedLocale()
  81. {
  82. $formatter = new StubIntlDateFormatter('pt_BR', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  83. }
  84. public function testConstructor()
  85. {
  86. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, 'Y-M-d');
  87. $this->assertEquals('Y-M-d', $formatter->getPattern());
  88. }
  89. /**
  90. * @dataProvider formatProvider
  91. */
  92. public function testFormat($pattern, $timestamp, $expected)
  93. {
  94. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  95. $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
  96. if (extension_loaded('intl')) {
  97. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  98. $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  99. }
  100. }
  101. /**
  102. * @dataProvider brokenFormatProvider
  103. */
  104. public function testBrokenFormat($pattern, $timestamp, $expected)
  105. {
  106. $this->markTestSkipped('icu/intl has some bugs, thus skipping.');
  107. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  108. $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
  109. if (extension_loaded('intl')) {
  110. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  111. $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  112. }
  113. }
  114. /**
  115. * @expectedException RuntimeException
  116. */
  117. public function testGetCalendar()
  118. {
  119. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  120. $formatter->getCalendar();
  121. }
  122. }