IntlDateFormatterTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\Intl\Tests\DateFormatter;
  11. use Symfony\Component\Intl\DateFormatter\IntlDateFormatter;
  12. use Symfony\Component\Intl\Globals\IntlGlobals;
  13. class IntlDateFormatterTest extends AbstractIntlDateFormatterTest
  14. {
  15. public function testConstructor()
  16. {
  17. $formatter = new IntlDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, 'y-M-d');
  18. $this->assertEquals('y-M-d', $formatter->getPattern());
  19. }
  20. public function testConstructorWithoutLocale()
  21. {
  22. $formatter = new IntlDateFormatter(null, IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, 'y-M-d');
  23. $this->assertEquals('y-M-d', $formatter->getPattern());
  24. }
  25. public function testConstructorWithoutCalendar()
  26. {
  27. $formatter = new IntlDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', null, 'y-M-d');
  28. $this->assertEquals('y-M-d', $formatter->getPattern());
  29. }
  30. /**
  31. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
  32. */
  33. public function testConstructorWithUnsupportedLocale()
  34. {
  35. new IntlDateFormatter('pt_BR', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT);
  36. }
  37. public function testStaticCreate()
  38. {
  39. $formatter = IntlDateFormatter::create('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT);
  40. $this->assertInstanceOf('\Symfony\Component\Intl\DateFormatter\IntlDateFormatter', $formatter);
  41. }
  42. public function testFormatWithUnsupportedTimestampArgument()
  43. {
  44. $formatter = $this->getDefaultDateFormatter();
  45. $localtime = array(
  46. 'tm_sec' => 59,
  47. 'tm_min' => 3,
  48. 'tm_hour' => 15,
  49. 'tm_mday' => 15,
  50. 'tm_mon' => 3,
  51. 'tm_year' => 112,
  52. 'tm_wday' => 0,
  53. 'tm_yday' => 105,
  54. 'tm_isdst' => 0,
  55. );
  56. try {
  57. $formatter->format($localtime);
  58. } catch (\Exception $e) {
  59. $this->assertInstanceOf('Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException', $e);
  60. $this->assertStringEndsWith('Only integer Unix timestamps and DateTime objects are supported. Please install the "intl" extension for full localization capabilities.', $e->getMessage());
  61. }
  62. }
  63. /**
  64. * @expectedException \Symfony\Component\Intl\Exception\NotImplementedException
  65. */
  66. public function testFormatWithUnimplementedChars()
  67. {
  68. $pattern = 'Y';
  69. $formatter = new IntlDateFormatter('en', IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, $pattern);
  70. $formatter->format(0);
  71. }
  72. /**
  73. * @expectedException \Symfony\Component\Intl\Exception\NotImplementedException
  74. */
  75. public function testFormatWithNonIntegerTimestamp()
  76. {
  77. $formatter = $this->getDefaultDateFormatter();
  78. $formatter->format(array());
  79. }
  80. public function testGetErrorCode()
  81. {
  82. $formatter = $this->getDefaultDateFormatter();
  83. $this->assertEquals(IntlGlobals::getErrorCode(), $formatter->getErrorCode());
  84. }
  85. public function testGetErrorMessage()
  86. {
  87. $formatter = $this->getDefaultDateFormatter();
  88. $this->assertEquals(IntlGlobals::getErrorMessage(), $formatter->getErrorMessage());
  89. }
  90. public function testIsLenient()
  91. {
  92. $formatter = $this->getDefaultDateFormatter();
  93. $this->assertFalse($formatter->isLenient());
  94. }
  95. /**
  96. * @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
  97. */
  98. public function testLocaltime()
  99. {
  100. $formatter = $this->getDefaultDateFormatter();
  101. $formatter->localtime('Wednesday, December 31, 1969 4:00:00 PM PT');
  102. }
  103. /**
  104. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentNotImplementedException
  105. */
  106. public function testParseWithNotNullPositionValue()
  107. {
  108. $position = 0;
  109. $formatter = $this->getDefaultDateFormatter('y');
  110. $this->assertSame(0, $formatter->parse('1970', $position));
  111. }
  112. /**
  113. * @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
  114. */
  115. public function testSetCalendar()
  116. {
  117. $formatter = $this->getDefaultDateFormatter();
  118. $formatter->setCalendar(IntlDateFormatter::GREGORIAN);
  119. }
  120. /**
  121. * @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
  122. */
  123. public function testSetLenient()
  124. {
  125. $formatter = $this->getDefaultDateFormatter();
  126. $formatter->setLenient(true);
  127. }
  128. /**
  129. * @expectedException \Symfony\Component\Intl\Exception\NotImplementedException
  130. */
  131. public function testFormatWithGmtTimeZoneAndMinutesOffset()
  132. {
  133. parent::testFormatWithGmtTimeZoneAndMinutesOffset();
  134. }
  135. /**
  136. * @expectedException \Symfony\Component\Intl\Exception\NotImplementedException
  137. */
  138. public function testFormatWithNonStandardTimezone()
  139. {
  140. parent::testFormatWithNonStandardTimezone();
  141. }
  142. public function parseStandaloneAmPmProvider()
  143. {
  144. return $this->notImplemented(parent::parseStandaloneAmPmProvider());
  145. }
  146. public function parseDayOfWeekProvider()
  147. {
  148. return $this->notImplemented(parent::parseDayOfWeekProvider());
  149. }
  150. public function parseDayOfYearProvider()
  151. {
  152. return $this->notImplemented(parent::parseDayOfYearProvider());
  153. }
  154. public function parseQuarterProvider()
  155. {
  156. return $this->notImplemented(parent::parseQuarterProvider());
  157. }
  158. protected function getDateFormatter($locale, $datetype, $timetype, $timezone = null, $calendar = IntlDateFormatter::GREGORIAN, $pattern = null)
  159. {
  160. return new IntlDateFormatter($locale, $datetype, $timetype, $timezone, $calendar, $pattern);
  161. }
  162. protected function getIntlErrorMessage()
  163. {
  164. return IntlGlobals::getErrorMessage();
  165. }
  166. protected function getIntlErrorCode()
  167. {
  168. return IntlGlobals::getErrorCode();
  169. }
  170. protected function isIntlFailure($errorCode)
  171. {
  172. return IntlGlobals::isFailure($errorCode);
  173. }
  174. /**
  175. * Just to document the differences between the stub and the intl
  176. * implementations. The intl can parse any of the tested formats alone. The
  177. * stub does not implement them as it would be needed to add more
  178. * abstraction, passing more context to the transformers objects. Any of the
  179. * formats are ignored alone or with date/time data (years, months, days,
  180. * hours, minutes and seconds).
  181. *
  182. * Also in intl, format like 'ss E' for '10 2' (2nd day of year
  183. * + 10 seconds) are added, then we have 86,400 seconds (24h * 60min * 60s)
  184. * + 10 seconds
  185. *
  186. * @param array $dataSets
  187. *
  188. * @return array
  189. */
  190. private function notImplemented(array $dataSets)
  191. {
  192. return array_map(function ($row) {
  193. return array($row[0], $row[1], 0);
  194. }, $dataSets);
  195. }
  196. }