DataTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Test\Unit\Helper;
  7. use Magento\Framework\Pricing\Helper\Data;
  8. use Magento\Framework\Pricing\PriceCurrencyInterface;
  9. class DataTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  13. */
  14. protected $objectManager;
  15. /**
  16. * @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $priceCurrencyMock;
  19. protected function setUp()
  20. {
  21. $this->priceCurrencyMock = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
  22. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  23. }
  24. /**
  25. * @param string $amount
  26. * @param bool $format
  27. * @param bool $includeContainer
  28. * @param string $result
  29. * @dataProvider currencyDataProvider
  30. */
  31. public function testCurrency($amount, $format, $includeContainer, $result)
  32. {
  33. if ($format) {
  34. $this->priceCurrencyMock->expects($this->once())
  35. ->method('convertAndFormat')
  36. ->with($amount, $includeContainer)
  37. ->will($this->returnValue($result));
  38. } else {
  39. $this->priceCurrencyMock->expects($this->once())
  40. ->method('convert')
  41. ->with($amount)
  42. ->will($this->returnValue($result));
  43. }
  44. $helper = $this->getHelper(['priceCurrency' => $this->priceCurrencyMock]);
  45. $this->assertEquals($result, $helper->currency($amount, $format, $includeContainer));
  46. }
  47. /**
  48. * @return array
  49. */
  50. public function currencyDataProvider()
  51. {
  52. return [
  53. ['amount' => '100', 'format' => true, 'includeContainer' => true, 'result' => '100grn.'],
  54. ['amount' => '115', 'format' => true, 'includeContainer' => false, 'result' => '1150'],
  55. ['amount' => '120', 'format' => false, 'includeContainer' => null, 'result' => '1200'],
  56. ];
  57. }
  58. /**
  59. * @param string $amount
  60. * @param string $store
  61. * @param bool $format
  62. * @param bool $includeContainer
  63. * @param string $result
  64. * @dataProvider currencyByStoreDataProvider
  65. */
  66. public function testCurrencyByStore($amount, $store, $format, $includeContainer, $result)
  67. {
  68. if ($format) {
  69. $this->priceCurrencyMock->expects($this->once())
  70. ->method('convertAndFormat')
  71. ->with($amount, $includeContainer, PriceCurrencyInterface::DEFAULT_PRECISION, $store)
  72. ->will($this->returnValue($result));
  73. } else {
  74. $this->priceCurrencyMock->expects($this->once())
  75. ->method('convert')
  76. ->with($amount, $store)
  77. ->will($this->returnValue($result));
  78. }
  79. $helper = $this->getHelper(['priceCurrency' => $this->priceCurrencyMock]);
  80. $this->assertEquals($result, $helper->currencyByStore($amount, $store, $format, $includeContainer));
  81. }
  82. /**
  83. * @return array
  84. */
  85. public function currencyByStoreDataProvider()
  86. {
  87. return [
  88. ['amount' => '10', 'store' => 1, 'format' => true, 'includeContainer' => true, 'result' => '10grn.'],
  89. ['amount' => '115', 'store' => 4, 'format' => true, 'includeContainer' => false, 'result' => '1150'],
  90. ['amount' => '120', 'store' => 5, 'format' => false, 'includeContainer' => null, 'result' => '1200'],
  91. ];
  92. }
  93. /**
  94. * Get helper instance
  95. *
  96. * @param array $arguments
  97. * @return Data
  98. */
  99. private function getHelper($arguments)
  100. {
  101. return $this->objectManager->getObject(\Magento\Framework\Pricing\Helper\Data::class, $arguments);
  102. }
  103. }