StubIntlDateFormatterTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /* this is stupid */
  32. array('MMMMMM', 0, '00001'),
  33. /* years */
  34. array('y', 0, '1970'),
  35. array('yy', 0, '70'),
  36. array('yyy', 0, '1970'),
  37. array('yyyy', 0, '1970'),
  38. array('yyyyy', 0, '01970'),
  39. array('yyyyyy', 0, '001970'),
  40. /* day */
  41. array('d', 0, '1'),
  42. array('dd', 0, '01'),
  43. array('ddd', 0, '001'),
  44. );
  45. }
  46. /**
  47. * provides data for cases that are broken in icu/intl
  48. */
  49. public function brokenFormatProvider()
  50. {
  51. return array(
  52. /* escaping */
  53. array("'y-'M-'d", 0, 'y-M-d'),
  54. array("WTF 'y-'M", 0, '0T1 y-M'),
  55. array("n-'M", 0, 'n-M'),
  56. );
  57. }
  58. /**
  59. * @expectedException InvalidArgumentException
  60. */
  61. public function testConstructorWithUnsupportedLocale()
  62. {
  63. $formatter = new StubIntlDateFormatter('pt_BR', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  64. }
  65. public function testConstructor()
  66. {
  67. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, 'Y-M-d');
  68. $this->assertEquals('Y-M-d', $formatter->getPattern());
  69. }
  70. /**
  71. * @dataProvider formatProvider
  72. */
  73. public function testFormat($pattern, $timestamp, $expected)
  74. {
  75. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  76. $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
  77. if (extension_loaded('intl')) {
  78. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  79. $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  80. }
  81. }
  82. /**
  83. * @dataProvider brokenFormatProvider
  84. */
  85. public function testBrokenFormat($pattern, $timestamp, $expected)
  86. {
  87. $this->markTestSkipped('icu/intl has some bugs, thus skipping.');
  88. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT, 'UTC', StubIntlDateFormatter::GREGORIAN, $pattern);
  89. $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with stub implementation.');
  90. if (extension_loaded('intl')) {
  91. $formatter = new \IntlDateFormatter('en', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', \IntlDateFormatter::GREGORIAN, $pattern);
  92. $this->assertEquals($expected, $formatter->format($timestamp), 'Check date format with intl extension.');
  93. }
  94. }
  95. /**
  96. * @expectedException RuntimeException
  97. */
  98. public function testGetCalendar()
  99. {
  100. $formatter = new StubIntlDateFormatter('en', StubIntlDateFormatter::MEDIUM, StubIntlDateFormatter::SHORT);
  101. $formatter->getCalendar();
  102. }
  103. }