FormatTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Locale\Test\Unit;
  7. class FormatTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\Locale\Format
  11. */
  12. protected $formatModel;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Locale\ResolverInterface
  15. */
  16. protected $localeResolver;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ScopeInterface
  19. */
  20. protected $scope;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ScopeResolverInterface
  23. */
  24. protected $scopeResolver;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Directory\Model\Currency
  27. */
  28. protected $currency;
  29. /**
  30. * {@inheritDoc}
  31. */
  32. protected function setUp()
  33. {
  34. $this->currency = $this->getMockBuilder(\Magento\Directory\Model\Currency::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->scope = $this->getMockBuilder(\Magento\Framework\App\ScopeInterface::class)
  38. ->setMethods(['getCurrentCurrency'])
  39. ->getMockForAbstractClass();
  40. $this->scopeResolver = $this->getMockBuilder(\Magento\Framework\App\ScopeResolverInterface::class)
  41. ->setMethods(['getScope'])
  42. ->getMockForAbstractClass();
  43. $this->scopeResolver->expects($this->any())
  44. ->method('getScope')
  45. ->willReturn($this->scope);
  46. $this->localeResolver = $this->getMockBuilder(\Magento\Framework\Locale\ResolverInterface::class)
  47. ->getMock();
  48. /** @var \Magento\Directory\Model\CurrencyFactory|\PHPUnit_Framework_MockObject_MockObject $currencyFactory */
  49. $currencyFactory = $this->getMockBuilder(\Magento\Directory\Model\CurrencyFactory::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->formatModel = new \Magento\Framework\Locale\Format(
  53. $this->scopeResolver,
  54. $this->localeResolver,
  55. $currencyFactory
  56. );
  57. }
  58. /**
  59. * @param string $localeCode
  60. * @param array $expectedResult
  61. * @dataProvider getPriceFormatDataProvider
  62. */
  63. public function testGetPriceFormat($localeCode, array $expectedResult): void
  64. {
  65. $this->scope->expects($this->once())
  66. ->method('getCurrentCurrency')
  67. ->willReturn($this->currency);
  68. $result = $this->formatModel->getPriceFormat($localeCode);
  69. $intersection = array_intersect_assoc($result, $expectedResult);
  70. $this->assertCount(count($expectedResult), $intersection);
  71. }
  72. /**
  73. *
  74. * @return array
  75. */
  76. public function getPriceFormatDataProvider(): array
  77. {
  78. return [
  79. ['en_US', ['decimalSymbol' => '.', 'groupSymbol' => ',']],
  80. ['de_DE', ['decimalSymbol' => ',', 'groupSymbol' => '.']],
  81. ['de_CH', ['decimalSymbol' => '.', 'groupSymbol' => '\'']],
  82. ['uk_UA', ['decimalSymbol' => ',', 'groupSymbol' => ' ']]
  83. ];
  84. }
  85. /**
  86. *
  87. * @param mixed $value
  88. * @param float $expected
  89. * @dataProvider provideNumbers
  90. */
  91. public function testGetNumber($value, $expected): void
  92. {
  93. $this->assertEquals($expected, $this->formatModel->getNumber($value));
  94. }
  95. /**
  96. *
  97. * @return array
  98. */
  99. public function provideNumbers(): array
  100. {
  101. return [
  102. [' 2345.4356,1234', 23454356.1234],
  103. ['+23,3452.123', 233452.123],
  104. ['12343', 12343],
  105. ['-9456km', -9456],
  106. ['0', 0],
  107. ['2 054,10', 2054.1],
  108. ['2046,45', 2046.45],
  109. ['2 054.52', 2054.52],
  110. ['2,46 GB', 2.46],
  111. ['2,054.00', 2054],
  112. ];
  113. }
  114. }