IntlCasterTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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\VarDumper\Tests\Caster;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
  13. /**
  14. * @requires extension intl
  15. */
  16. class IntlCasterTest extends TestCase
  17. {
  18. use VarDumperTestTrait;
  19. public function testMessageFormatter()
  20. {
  21. $var = new \MessageFormatter('en', 'Hello {name}');
  22. $expected = <<<EOTXT
  23. MessageFormatter {
  24. locale: "en"
  25. pattern: "Hello {name}"
  26. }
  27. EOTXT;
  28. $this->assertDumpEquals($expected, $var);
  29. }
  30. public function testCastNumberFormatter()
  31. {
  32. $var = new \NumberFormatter('en', \NumberFormatter::DECIMAL);
  33. $expectedLocale = $var->getLocale();
  34. $expectedPattern = $var->getPattern();
  35. $expectedAttribute1 = $var->getAttribute(\NumberFormatter::PARSE_INT_ONLY);
  36. $expectedAttribute2 = $var->getAttribute(\NumberFormatter::GROUPING_USED);
  37. $expectedAttribute3 = $var->getAttribute(\NumberFormatter::DECIMAL_ALWAYS_SHOWN);
  38. $expectedAttribute4 = $var->getAttribute(\NumberFormatter::MAX_INTEGER_DIGITS);
  39. $expectedAttribute5 = $var->getAttribute(\NumberFormatter::MIN_INTEGER_DIGITS);
  40. $expectedAttribute6 = $var->getAttribute(\NumberFormatter::INTEGER_DIGITS);
  41. $expectedAttribute7 = $var->getAttribute(\NumberFormatter::MAX_FRACTION_DIGITS);
  42. $expectedAttribute8 = $var->getAttribute(\NumberFormatter::MIN_FRACTION_DIGITS);
  43. $expectedAttribute9 = $var->getAttribute(\NumberFormatter::FRACTION_DIGITS);
  44. $expectedAttribute10 = $var->getAttribute(\NumberFormatter::MULTIPLIER);
  45. $expectedAttribute11 = $var->getAttribute(\NumberFormatter::GROUPING_SIZE);
  46. $expectedAttribute12 = $var->getAttribute(\NumberFormatter::ROUNDING_MODE);
  47. $expectedAttribute13 = number_format($var->getAttribute(\NumberFormatter::ROUNDING_INCREMENT), 1);
  48. $expectedAttribute14 = $var->getAttribute(\NumberFormatter::FORMAT_WIDTH);
  49. $expectedAttribute15 = $var->getAttribute(\NumberFormatter::PADDING_POSITION);
  50. $expectedAttribute16 = $var->getAttribute(\NumberFormatter::SECONDARY_GROUPING_SIZE);
  51. $expectedAttribute17 = $var->getAttribute(\NumberFormatter::SIGNIFICANT_DIGITS_USED);
  52. $expectedAttribute18 = $var->getAttribute(\NumberFormatter::MIN_SIGNIFICANT_DIGITS);
  53. $expectedAttribute19 = $var->getAttribute(\NumberFormatter::MAX_SIGNIFICANT_DIGITS);
  54. $expectedAttribute20 = $var->getAttribute(\NumberFormatter::LENIENT_PARSE);
  55. $expectedTextAttribute1 = $var->getTextAttribute(\NumberFormatter::POSITIVE_PREFIX);
  56. $expectedTextAttribute2 = $var->getTextAttribute(\NumberFormatter::POSITIVE_SUFFIX);
  57. $expectedTextAttribute3 = $var->getTextAttribute(\NumberFormatter::NEGATIVE_PREFIX);
  58. $expectedTextAttribute4 = $var->getTextAttribute(\NumberFormatter::NEGATIVE_SUFFIX);
  59. $expectedTextAttribute5 = $var->getTextAttribute(\NumberFormatter::PADDING_CHARACTER);
  60. $expectedTextAttribute6 = $var->getTextAttribute(\NumberFormatter::CURRENCY_CODE);
  61. $expectedTextAttribute7 = $var->getTextAttribute(\NumberFormatter::DEFAULT_RULESET) ? 'true' : 'false';
  62. $expectedTextAttribute8 = $var->getTextAttribute(\NumberFormatter::PUBLIC_RULESETS) ? 'true' : 'false';
  63. $expectedSymbol1 = $var->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
  64. $expectedSymbol2 = $var->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
  65. $expectedSymbol3 = $var->getSymbol(\NumberFormatter::PATTERN_SEPARATOR_SYMBOL);
  66. $expectedSymbol4 = $var->getSymbol(\NumberFormatter::PERCENT_SYMBOL);
  67. $expectedSymbol5 = $var->getSymbol(\NumberFormatter::ZERO_DIGIT_SYMBOL);
  68. $expectedSymbol6 = $var->getSymbol(\NumberFormatter::DIGIT_SYMBOL);
  69. $expectedSymbol7 = $var->getSymbol(\NumberFormatter::MINUS_SIGN_SYMBOL);
  70. $expectedSymbol8 = $var->getSymbol(\NumberFormatter::PLUS_SIGN_SYMBOL);
  71. $expectedSymbol9 = $var->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
  72. $expectedSymbol10 = $var->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL);
  73. $expectedSymbol11 = $var->getSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL);
  74. $expectedSymbol12 = $var->getSymbol(\NumberFormatter::EXPONENTIAL_SYMBOL);
  75. $expectedSymbol13 = $var->getSymbol(\NumberFormatter::PERMILL_SYMBOL);
  76. $expectedSymbol14 = $var->getSymbol(\NumberFormatter::PAD_ESCAPE_SYMBOL);
  77. $expectedSymbol15 = $var->getSymbol(\NumberFormatter::INFINITY_SYMBOL);
  78. $expectedSymbol16 = $var->getSymbol(\NumberFormatter::NAN_SYMBOL);
  79. $expectedSymbol17 = $var->getSymbol(\NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL);
  80. $expectedSymbol18 = $var->getSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL);
  81. $expected = <<<EOTXT
  82. NumberFormatter {
  83. locale: "$expectedLocale"
  84. pattern: "$expectedPattern"
  85. attributes: {
  86. PARSE_INT_ONLY: $expectedAttribute1
  87. GROUPING_USED: $expectedAttribute2
  88. DECIMAL_ALWAYS_SHOWN: $expectedAttribute3
  89. MAX_INTEGER_DIGITS: $expectedAttribute4
  90. MIN_INTEGER_DIGITS: $expectedAttribute5
  91. INTEGER_DIGITS: $expectedAttribute6
  92. MAX_FRACTION_DIGITS: $expectedAttribute7
  93. MIN_FRACTION_DIGITS: $expectedAttribute8
  94. FRACTION_DIGITS: $expectedAttribute9
  95. MULTIPLIER: $expectedAttribute10
  96. GROUPING_SIZE: $expectedAttribute11
  97. ROUNDING_MODE: $expectedAttribute12
  98. ROUNDING_INCREMENT: $expectedAttribute13
  99. FORMAT_WIDTH: $expectedAttribute14
  100. PADDING_POSITION: $expectedAttribute15
  101. SECONDARY_GROUPING_SIZE: $expectedAttribute16
  102. SIGNIFICANT_DIGITS_USED: $expectedAttribute17
  103. MIN_SIGNIFICANT_DIGITS: $expectedAttribute18
  104. MAX_SIGNIFICANT_DIGITS: $expectedAttribute19
  105. LENIENT_PARSE: $expectedAttribute20
  106. }
  107. text_attributes: {
  108. POSITIVE_PREFIX: "$expectedTextAttribute1"
  109. POSITIVE_SUFFIX: "$expectedTextAttribute2"
  110. NEGATIVE_PREFIX: "$expectedTextAttribute3"
  111. NEGATIVE_SUFFIX: "$expectedTextAttribute4"
  112. PADDING_CHARACTER: "$expectedTextAttribute5"
  113. CURRENCY_CODE: "$expectedTextAttribute6"
  114. DEFAULT_RULESET: $expectedTextAttribute7
  115. PUBLIC_RULESETS: $expectedTextAttribute8
  116. }
  117. symbols: {
  118. DECIMAL_SEPARATOR_SYMBOL: "$expectedSymbol1"
  119. GROUPING_SEPARATOR_SYMBOL: "$expectedSymbol2"
  120. PATTERN_SEPARATOR_SYMBOL: "$expectedSymbol3"
  121. PERCENT_SYMBOL: "$expectedSymbol4"
  122. ZERO_DIGIT_SYMBOL: "$expectedSymbol5"
  123. DIGIT_SYMBOL: "$expectedSymbol6"
  124. MINUS_SIGN_SYMBOL: "$expectedSymbol7"
  125. PLUS_SIGN_SYMBOL: "$expectedSymbol8"
  126. CURRENCY_SYMBOL: "$expectedSymbol9"
  127. INTL_CURRENCY_SYMBOL: "$expectedSymbol10"
  128. MONETARY_SEPARATOR_SYMBOL: "$expectedSymbol11"
  129. EXPONENTIAL_SYMBOL: "$expectedSymbol12"
  130. PERMILL_SYMBOL: "$expectedSymbol13"
  131. PAD_ESCAPE_SYMBOL: "$expectedSymbol14"
  132. INFINITY_SYMBOL: "$expectedSymbol15"
  133. NAN_SYMBOL: "$expectedSymbol16"
  134. SIGNIFICANT_DIGIT_SYMBOL: "$expectedSymbol17"
  135. MONETARY_GROUPING_SEPARATOR_SYMBOL: "$expectedSymbol18"
  136. }
  137. }
  138. EOTXT;
  139. $this->assertDumpEquals($expected, $var);
  140. }
  141. public function testCastIntlTimeZoneWithDST()
  142. {
  143. $var = \IntlTimeZone::createTimeZone('America/Los_Angeles');
  144. $expectedDisplayName = $var->getDisplayName();
  145. $expectedDSTSavings = $var->getDSTSavings();
  146. $expectedID = $var->getID();
  147. $expectedRawOffset = $var->getRawOffset();
  148. $expected = <<<EOTXT
  149. IntlTimeZone {
  150. display_name: "$expectedDisplayName"
  151. id: "$expectedID"
  152. raw_offset: $expectedRawOffset
  153. dst_savings: $expectedDSTSavings
  154. }
  155. EOTXT;
  156. $this->assertDumpEquals($expected, $var);
  157. }
  158. public function testCastIntlTimeZoneWithoutDST()
  159. {
  160. $var = \IntlTimeZone::createTimeZone('Asia/Bangkok');
  161. $expectedDisplayName = $var->getDisplayName();
  162. $expectedID = $var->getID();
  163. $expectedRawOffset = $var->getRawOffset();
  164. $expected = <<<EOTXT
  165. IntlTimeZone {
  166. display_name: "$expectedDisplayName"
  167. id: "$expectedID"
  168. raw_offset: $expectedRawOffset
  169. }
  170. EOTXT;
  171. $this->assertDumpEquals($expected, $var);
  172. }
  173. public function testCastIntlCalendar()
  174. {
  175. $var = \IntlCalendar::createInstance('America/Los_Angeles', 'en');
  176. $expectedType = $var->getType();
  177. $expectedFirstDayOfWeek = $var->getFirstDayOfWeek();
  178. $expectedMinimalDaysInFirstWeek = $var->getMinimalDaysInFirstWeek();
  179. $expectedRepeatedWallTimeOption = $var->getRepeatedWallTimeOption();
  180. $expectedSkippedWallTimeOption = $var->getSkippedWallTimeOption();
  181. $expectedTime = $var->getTime().'.0';
  182. $expectedInDaylightTime = $var->inDaylightTime() ? 'true' : 'false';
  183. $expectedIsLenient = $var->isLenient() ? 'true' : 'false';
  184. $expectedTimeZone = $var->getTimeZone();
  185. $expectedTimeZoneDisplayName = $expectedTimeZone->getDisplayName();
  186. $expectedTimeZoneID = $expectedTimeZone->getID();
  187. $expectedTimeZoneRawOffset = $expectedTimeZone->getRawOffset();
  188. $expectedTimeZoneDSTSavings = $expectedTimeZone->getDSTSavings();
  189. $expected = <<<EOTXT
  190. IntlGregorianCalendar {
  191. type: "$expectedType"
  192. first_day_of_week: $expectedFirstDayOfWeek
  193. minimal_days_in_first_week: $expectedMinimalDaysInFirstWeek
  194. repeated_wall_time_option: $expectedRepeatedWallTimeOption
  195. skipped_wall_time_option: $expectedSkippedWallTimeOption
  196. time: $expectedTime
  197. in_daylight_time: $expectedInDaylightTime
  198. is_lenient: $expectedIsLenient
  199. time_zone: IntlTimeZone {
  200. display_name: "$expectedTimeZoneDisplayName"
  201. id: "$expectedTimeZoneID"
  202. raw_offset: $expectedTimeZoneRawOffset
  203. dst_savings: $expectedTimeZoneDSTSavings
  204. }
  205. }
  206. EOTXT;
  207. $this->assertDumpEquals($expected, $var);
  208. }
  209. public function testCastDateFormatter()
  210. {
  211. $var = new \IntlDateFormatter('en', \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::TRADITIONAL);
  212. $expectedLocale = $var->getLocale();
  213. $expectedPattern = $var->getPattern();
  214. $expectedCalendar = $var->getCalendar();
  215. $expectedTimeZoneId = $var->getTimeZoneId();
  216. $expectedTimeType = $var->getTimeType();
  217. $expectedDateType = $var->getDateType();
  218. $expectedCalendarObject = $var->getCalendarObject();
  219. $expectedCalendarObjectType = $expectedCalendarObject->getType();
  220. $expectedCalendarObjectFirstDayOfWeek = $expectedCalendarObject->getFirstDayOfWeek();
  221. $expectedCalendarObjectMinimalDaysInFirstWeek = $expectedCalendarObject->getMinimalDaysInFirstWeek();
  222. $expectedCalendarObjectRepeatedWallTimeOption = $expectedCalendarObject->getRepeatedWallTimeOption();
  223. $expectedCalendarObjectSkippedWallTimeOption = $expectedCalendarObject->getSkippedWallTimeOption();
  224. $expectedCalendarObjectTime = $expectedCalendarObject->getTime().'.0';
  225. $expectedCalendarObjectInDaylightTime = $expectedCalendarObject->inDaylightTime() ? 'true' : 'false';
  226. $expectedCalendarObjectIsLenient = $expectedCalendarObject->isLenient() ? 'true' : 'false';
  227. $expectedCalendarObjectTimeZone = $expectedCalendarObject->getTimeZone();
  228. $expectedCalendarObjectTimeZoneDisplayName = $expectedCalendarObjectTimeZone->getDisplayName();
  229. $expectedCalendarObjectTimeZoneID = $expectedCalendarObjectTimeZone->getID();
  230. $expectedCalendarObjectTimeZoneRawOffset = $expectedCalendarObjectTimeZone->getRawOffset();
  231. $expectedCalendarObjectTimeZoneDSTSavings = $expectedCalendarObjectTimeZone->getDSTSavings();
  232. $expectedTimeZone = $var->getTimeZone();
  233. $expectedTimeZoneDisplayName = $expectedTimeZone->getDisplayName();
  234. $expectedTimeZoneID = $expectedTimeZone->getID();
  235. $expectedTimeZoneRawOffset = $expectedTimeZone->getRawOffset();
  236. $expectedTimeZoneDSTSavings = $expectedTimeZone->getDSTSavings();
  237. $expected = <<<EOTXT
  238. IntlDateFormatter {
  239. locale: "$expectedLocale"
  240. pattern: "$expectedPattern"
  241. calendar: $expectedCalendar
  242. time_zone_id: "$expectedTimeZoneId"
  243. time_type: $expectedTimeType
  244. date_type: $expectedDateType
  245. calendar_object: IntlGregorianCalendar {
  246. type: "$expectedCalendarObjectType"
  247. first_day_of_week: $expectedCalendarObjectFirstDayOfWeek
  248. minimal_days_in_first_week: $expectedCalendarObjectMinimalDaysInFirstWeek
  249. repeated_wall_time_option: $expectedCalendarObjectRepeatedWallTimeOption
  250. skipped_wall_time_option: $expectedCalendarObjectSkippedWallTimeOption
  251. time: $expectedCalendarObjectTime
  252. in_daylight_time: $expectedCalendarObjectInDaylightTime
  253. is_lenient: $expectedCalendarObjectIsLenient
  254. time_zone: IntlTimeZone {
  255. display_name: "$expectedCalendarObjectTimeZoneDisplayName"
  256. id: "$expectedCalendarObjectTimeZoneID"
  257. raw_offset: $expectedCalendarObjectTimeZoneRawOffset
  258. dst_savings: $expectedCalendarObjectTimeZoneDSTSavings
  259. }
  260. }
  261. time_zone: IntlTimeZone {
  262. display_name: "$expectedTimeZoneDisplayName"
  263. id: "$expectedTimeZoneID"
  264. raw_offset: $expectedTimeZoneRawOffset
  265. dst_savings: $expectedTimeZoneDSTSavings
  266. }
  267. }
  268. EOTXT;
  269. $this->assertDumpEquals($expected, $var);
  270. }
  271. }