ShippingMethodConverterTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Quote\Test\Unit\Model\Cart;
  8. use Magento\Quote\Model\Cart\ShippingMethodConverter;
  9. class ShippingMethodConverterTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var ShippingMethodConverter
  13. */
  14. protected $converter;
  15. /**
  16. * @var \PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $shippingMethodDataFactoryMock;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $storeManagerMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $rateModelMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $currencyMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $storeMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $shippingMethodMock;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $taxHelper;
  43. protected function setUp()
  44. {
  45. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  46. $this->shippingMethodDataFactoryMock = $this->createPartialMock(
  47. \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory::class,
  48. ['create']
  49. );
  50. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  51. $this->currencyMock = $this->createMock(\Magento\Directory\Model\Currency::class);
  52. $this->shippingMethodMock = $this->createPartialMock(
  53. \Magento\Quote\Model\Cart\ShippingMethod::class,
  54. [
  55. 'create',
  56. 'setCarrierCode',
  57. 'setMethodCode',
  58. 'setCarrierTitle',
  59. 'setMethodTitle',
  60. 'setAmount',
  61. 'setBaseAmount',
  62. 'setAvailable',
  63. 'setPriceExclTax',
  64. 'setPriceInclTax'
  65. ]
  66. );
  67. $this->rateModelMock = $this->createPartialMock(
  68. \Magento\Quote\Model\Quote\Address\Rate::class,
  69. [
  70. 'getPrice',
  71. 'getCarrier',
  72. 'getMethod',
  73. 'getCarrierTitle',
  74. 'getMethodTitle',
  75. '__wakeup',
  76. 'getAddress'
  77. ]
  78. );
  79. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  80. $this->taxHelper = $this->createMock(\Magento\Tax\Helper\Data::class);
  81. $this->converter = $objectManager->getObject(
  82. \Magento\Quote\Model\Cart\ShippingMethodConverter::class,
  83. [
  84. 'shippingMethodDataFactory' => $this->shippingMethodDataFactoryMock,
  85. 'storeManager' => $this->storeManagerMock,
  86. 'taxHelper' => $this->taxHelper
  87. ]
  88. );
  89. }
  90. public function testModelToDataObject()
  91. {
  92. $customerTaxClassId = 100;
  93. $shippingPriceExclTax = 1000;
  94. $shippingPriceInclTax = 1500;
  95. $price = 90.12;
  96. $this->storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($this->storeMock));
  97. $this->storeMock->expects($this->once())
  98. ->method('getBaseCurrency')
  99. ->will($this->returnValue($this->currencyMock));
  100. $this->rateModelMock->expects($this->once())->method('getCarrier')->will($this->returnValue('CARRIER_CODE'));
  101. $this->rateModelMock->expects($this->once())->method('getMethod')->will($this->returnValue('METHOD_CODE'));
  102. $this->rateModelMock->expects($this->any())->method('getPrice')->will($this->returnValue($price));
  103. $this->currencyMock->expects($this->at(0))
  104. ->method('convert')->with($price, 'USD')->willReturn(100.12);
  105. $this->currencyMock->expects($this->at(1))
  106. ->method('convert')->with($shippingPriceExclTax, 'USD')->willReturn($shippingPriceExclTax);
  107. $this->currencyMock->expects($this->at(2))
  108. ->method('convert')->with($shippingPriceInclTax, 'USD')->willReturn($shippingPriceInclTax);
  109. $this->rateModelMock->expects($this->once())
  110. ->method('getCarrierTitle')->will($this->returnValue('CARRIER_TITLE'));
  111. $this->rateModelMock->expects($this->once())
  112. ->method('getMethodTitle')->will($this->returnValue('METHOD_TITLE'));
  113. $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  114. $addressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
  115. $this->rateModelMock->expects($this->exactly(4))->method('getAddress')->willReturn($addressMock);
  116. $addressMock->expects($this->exactly(2))->method('getQuote')->willReturn($quoteMock);
  117. $quoteMock->expects($this->exactly(2))->method('getCustomerTaxClassId')->willReturn($customerTaxClassId);
  118. $this->shippingMethodDataFactoryMock->expects($this->once())
  119. ->method('create')
  120. ->will($this->returnValue($this->shippingMethodMock));
  121. $this->shippingMethodMock->expects($this->once())
  122. ->method('setCarrierCode')
  123. ->with('CARRIER_CODE')
  124. ->will($this->returnValue($this->shippingMethodMock));
  125. $this->shippingMethodMock->expects($this->once())
  126. ->method('setMethodCode')
  127. ->with('METHOD_CODE')
  128. ->will($this->returnValue($this->shippingMethodMock));
  129. $this->shippingMethodMock->expects($this->once())
  130. ->method('setCarrierTitle')
  131. ->with('CARRIER_TITLE')
  132. ->will($this->returnValue($this->shippingMethodMock));
  133. $this->shippingMethodMock->expects($this->once())
  134. ->method('setMethodTitle')
  135. ->with('METHOD_TITLE')
  136. ->will($this->returnValue($this->shippingMethodMock));
  137. $this->shippingMethodMock->expects($this->once())
  138. ->method('setAmount')
  139. ->with('100.12')
  140. ->will($this->returnValue($this->shippingMethodMock));
  141. $this->shippingMethodMock->expects($this->once())
  142. ->method('setBaseAmount')
  143. ->with('90.12')
  144. ->will($this->returnValue($this->shippingMethodMock));
  145. $this->shippingMethodMock->expects($this->once())
  146. ->method('setAvailable')
  147. ->with(true)
  148. ->will($this->returnValue($this->shippingMethodMock));
  149. $this->shippingMethodMock->expects($this->once())
  150. ->method('setPriceExclTax')
  151. ->with($shippingPriceExclTax)
  152. ->will($this->returnValue($this->shippingMethodMock));
  153. $this->shippingMethodMock->expects($this->once())
  154. ->method('setPriceInclTax')
  155. ->with($shippingPriceInclTax)
  156. ->will($this->returnValue($this->shippingMethodMock));
  157. $this->taxHelper->expects($this->at(0))
  158. ->method('getShippingPrice')
  159. ->with($price, false, $addressMock, $customerTaxClassId)
  160. ->willReturn($shippingPriceExclTax);
  161. $this->taxHelper->expects($this->at(1))
  162. ->method('getShippingPrice')
  163. ->with($price, true, $addressMock, $customerTaxClassId)
  164. ->willReturn($shippingPriceInclTax);
  165. $this->assertEquals(
  166. $this->shippingMethodMock,
  167. $this->converter->modelToDataObject($this->rateModelMock, 'USD')
  168. );
  169. }
  170. }