CurrencyTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Directory\Test\Unit\Model;
  7. use Magento\Directory\Model\Currency;
  8. class CurrencyTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var Currency
  12. */
  13. protected $currency;
  14. protected $currencyCode = 'USD';
  15. /**
  16. * @var \Magento\Framework\Locale\CurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $localeCurrencyMock;
  19. protected function setUp()
  20. {
  21. $this->localeCurrencyMock = $this->createMock(\Magento\Framework\Locale\CurrencyInterface::class);
  22. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  23. $this->currency = $objectManager->getObject(
  24. \Magento\Directory\Model\Currency::class,
  25. [
  26. 'localeCurrency' => $this->localeCurrencyMock,
  27. 'data' => [
  28. 'currency_code' => $this->currencyCode,
  29. ]
  30. ]
  31. );
  32. }
  33. public function testGetCurrencySymbol()
  34. {
  35. $currencySymbol = '$';
  36. $currencyMock = $this->getMockBuilder(\Magento\Framework\Currency::class)
  37. ->disableOriginalConstructor()
  38. ->getMock();
  39. $currencyMock->expects($this->once())
  40. ->method('getSymbol')
  41. ->willReturn($currencySymbol);
  42. $this->localeCurrencyMock->expects($this->once())
  43. ->method('getCurrency')
  44. ->with($this->currencyCode)
  45. ->willReturn($currencyMock);
  46. $this->assertEquals($currencySymbol, $this->currency->getCurrencySymbol());
  47. }
  48. /**
  49. * @dataProvider getOutputFormatDataProvider
  50. * @param $withCurrency
  51. * @param $noCurrency
  52. * @param $expected
  53. */
  54. public function testGetOutputFormat($withCurrency, $noCurrency, $expected)
  55. {
  56. $currencyMock = $this->getMockBuilder(\Magento\Framework\Currency::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $currencyMock->expects($this->at(0))
  60. ->method('toCurrency')
  61. ->willReturn($withCurrency);
  62. $currencyMock->expects($this->at(1))
  63. ->method('toCurrency')
  64. ->willReturn($noCurrency);
  65. $this->localeCurrencyMock->expects($this->atLeastOnce())
  66. ->method('getCurrency')
  67. ->with($this->currencyCode)
  68. ->willReturn($currencyMock);
  69. $this->assertEquals($expected, $this->currency->getOutputFormat());
  70. }
  71. /**
  72. * Return data sets for testGetCurrencySymbol()
  73. *
  74. * @return array
  75. */
  76. public function getOutputFormatDataProvider()
  77. {
  78. return [
  79. 'no_unicode' => [
  80. 'withCurrency' => '$0.00',
  81. 'noCurrency' => '0.00',
  82. 'expected' => '$%s',
  83. ],
  84. 'arabic_unicode' => [
  85. 'withCurrency' => json_decode('"\u200E"') . '$0.00',
  86. 'noCurrency' => json_decode('"\u200E"') . '0.00',
  87. 'expected' => json_decode('"\u200E"') . '$%s',
  88. ]
  89. ];
  90. }
  91. }