PriceTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Block\Checkout\Shipping;
  7. class PriceTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Tax\Block\Checkout\Shipping\Price
  11. */
  12. protected $priceObj;
  13. /**
  14. * @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $quote;
  17. /**
  18. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $store;
  21. /**
  22. * @var \Magento\Tax\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $taxHelper;
  25. /**
  26. * @var \Magento\Framework\Pricing\PriceCurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $priceCurrency;
  29. protected function setUp()
  30. {
  31. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  32. $this->priceCurrency = $this->getMockBuilder(
  33. \Magento\Framework\Pricing\PriceCurrencyInterface::class
  34. )->getMock();
  35. $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->quote = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
  39. ->disableOriginalConstructor()
  40. ->setMethods(['getStore', '__wakeup', 'getCustomerTaxClassId'])
  41. ->getMock();
  42. $this->quote->expects($this->any())
  43. ->method('getStore')
  44. ->will($this->returnValue($this->store));
  45. $checkoutSession = $this->getMockBuilder(\Magento\Checkout\Model\Session::class)
  46. ->disableOriginalConstructor()
  47. ->setMethods(['getQuote', '__wakeup'])
  48. ->getMock();
  49. $checkoutSession->expects($this->any())
  50. ->method('getQuote')
  51. ->will($this->returnValue($this->quote));
  52. $this->taxHelper = $this->getMockBuilder(\Magento\Tax\Helper\Data::class)
  53. ->disableOriginalConstructor()
  54. ->setMethods([
  55. 'getShippingPrice', 'displayShippingPriceIncludingTax', 'displayShippingBothPrices',
  56. ])
  57. ->getMock();
  58. $this->priceObj = $objectManager->getObject(
  59. \Magento\Tax\Block\Checkout\Shipping\Price::class,
  60. [
  61. 'checkoutSession' => $checkoutSession,
  62. 'taxHelper' => $this->taxHelper,
  63. 'priceCurrency' => $this->priceCurrency,
  64. ]
  65. );
  66. }
  67. /**
  68. * @param float $shippingPrice
  69. * @return \Magento\Quote\Model\Quote\Address\Rate|\PHPUnit_Framework_MockObject_MockObject
  70. */
  71. protected function setupShippingRate($shippingPrice)
  72. {
  73. $shippingRateMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Address\Rate::class)
  74. ->disableOriginalConstructor()
  75. ->setMethods(['getPrice', '__wakeup'])
  76. ->getMock();
  77. $shippingRateMock->expects($this->once())
  78. ->method('getPrice')
  79. ->will($this->returnValue($shippingPrice));
  80. return $shippingRateMock;
  81. }
  82. public function testGetShippingPriceExclTax()
  83. {
  84. $shippingPrice = 5;
  85. $shippingPriceExclTax = 4.5;
  86. $convertedPrice = "$4.50";
  87. $shippingRateMock = $this->setupShippingRate($shippingPrice);
  88. $this->taxHelper->expects($this->once())
  89. ->method('getShippingPrice')
  90. ->will($this->returnValue($shippingPriceExclTax));
  91. $this->priceCurrency->expects($this->once())
  92. ->method('convertAndFormat')
  93. ->with($this->logicalOr($shippingPriceExclTax, true, $this->store))
  94. ->willReturn($convertedPrice);
  95. $this->priceObj->setShippingRate($shippingRateMock);
  96. $this->assertEquals($convertedPrice, $this->priceObj->getShippingPriceExclTax());
  97. }
  98. public function testGetShippingPriceInclTax()
  99. {
  100. $shippingPrice = 5;
  101. $shippingPriceInclTax = 5.5;
  102. $convertedPrice = "$5.50";
  103. $shippingRateMock = $this->setupShippingRate($shippingPrice);
  104. $this->taxHelper->expects($this->once())
  105. ->method('getShippingPrice')
  106. ->will($this->returnValue($shippingPriceInclTax));
  107. $this->priceCurrency->expects($this->once())
  108. ->method('convertAndFormat')
  109. ->with($this->logicalOr($shippingPriceInclTax, true, $this->store))
  110. ->will($this->returnValue($convertedPrice));
  111. $this->priceObj->setShippingRate($shippingRateMock);
  112. $this->assertEquals($convertedPrice, $this->priceObj->getShippingPriceExclTax());
  113. }
  114. public function testDisplayShippingPriceInclTax()
  115. {
  116. $this->taxHelper->expects($this->once())
  117. ->method('displayShippingPriceIncludingTax');
  118. $this->priceObj->displayShippingPriceInclTax();
  119. }
  120. public function testDisplayShippingBothPrices()
  121. {
  122. $this->taxHelper->expects($this->once())
  123. ->method('displayShippingBothPrices');
  124. $this->priceObj->displayShippingBothPrices();
  125. }
  126. }