Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ShippingMethodConverterTest.php
Go to the documentation of this file.
1 <?php
9 
11 
12 class ShippingMethodConverterTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $converter;
18 
23 
27  protected $storeManagerMock;
28 
32  protected $rateModelMock;
33 
37  protected $currencyMock;
38 
42  protected $storeMock;
43 
48 
52  protected $taxHelper;
53 
54  protected function setUp()
55  {
56  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
57  $this->shippingMethodDataFactoryMock = $this->createPartialMock(
58  \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory::class,
59  ['create']
60  );
61  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
62  $this->currencyMock = $this->createMock(\Magento\Directory\Model\Currency::class);
63  $this->shippingMethodMock = $this->createPartialMock(
64  \Magento\Quote\Model\Cart\ShippingMethod::class,
65  [
66  'create',
67  'setCarrierCode',
68  'setMethodCode',
69  'setCarrierTitle',
70  'setMethodTitle',
71  'setAmount',
72  'setBaseAmount',
73  'setAvailable',
74  'setPriceExclTax',
75  'setPriceInclTax'
76  ]
77  );
78  $this->rateModelMock = $this->createPartialMock(
79  \Magento\Quote\Model\Quote\Address\Rate::class,
80  [
81  'getPrice',
82  'getCarrier',
83  'getMethod',
84  'getCarrierTitle',
85  'getMethodTitle',
86  '__wakeup',
87  'getAddress'
88  ]
89  );
90  $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
91  $this->taxHelper = $this->createMock(\Magento\Tax\Helper\Data::class);
92 
93  $this->converter = $objectManager->getObject(
94  \Magento\Quote\Model\Cart\ShippingMethodConverter::class,
95  [
96  'shippingMethodDataFactory' => $this->shippingMethodDataFactoryMock,
97  'storeManager' => $this->storeManagerMock,
98  'taxHelper' => $this->taxHelper
99  ]
100  );
101  }
102 
103  public function testModelToDataObject()
104  {
105  $customerTaxClassId = 100;
106  $shippingPriceExclTax = 1000;
107  $shippingPriceInclTax = 1500;
108  $price = 90.12;
109 
110  $this->storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($this->storeMock));
111  $this->storeMock->expects($this->once())
112  ->method('getBaseCurrency')
113  ->will($this->returnValue($this->currencyMock));
114 
115  $this->rateModelMock->expects($this->once())->method('getCarrier')->will($this->returnValue('CARRIER_CODE'));
116  $this->rateModelMock->expects($this->once())->method('getMethod')->will($this->returnValue('METHOD_CODE'));
117  $this->rateModelMock->expects($this->any())->method('getPrice')->will($this->returnValue($price));
118  $this->currencyMock->expects($this->at(0))
119  ->method('convert')->with($price, 'USD')->willReturn(100.12);
120  $this->currencyMock->expects($this->at(1))
121  ->method('convert')->with($shippingPriceExclTax, 'USD')->willReturn($shippingPriceExclTax);
122  $this->currencyMock->expects($this->at(2))
123  ->method('convert')->with($shippingPriceInclTax, 'USD')->willReturn($shippingPriceInclTax);
124 
125  $this->rateModelMock->expects($this->once())
126  ->method('getCarrierTitle')->will($this->returnValue('CARRIER_TITLE'));
127  $this->rateModelMock->expects($this->once())
128  ->method('getMethodTitle')->will($this->returnValue('METHOD_TITLE'));
129 
130  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
131  $addressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
132  $this->rateModelMock->expects($this->exactly(4))->method('getAddress')->willReturn($addressMock);
133 
134  $addressMock->expects($this->exactly(2))->method('getQuote')->willReturn($quoteMock);
135  $quoteMock->expects($this->exactly(2))->method('getCustomerTaxClassId')->willReturn($customerTaxClassId);
136 
137  $this->shippingMethodDataFactoryMock->expects($this->once())
138  ->method('create')
139  ->will($this->returnValue($this->shippingMethodMock));
140 
141  $this->shippingMethodMock->expects($this->once())
142  ->method('setCarrierCode')
143  ->with('CARRIER_CODE')
144  ->will($this->returnValue($this->shippingMethodMock));
145  $this->shippingMethodMock->expects($this->once())
146  ->method('setMethodCode')
147  ->with('METHOD_CODE')
148  ->will($this->returnValue($this->shippingMethodMock));
149  $this->shippingMethodMock->expects($this->once())
150  ->method('setCarrierTitle')
151  ->with('CARRIER_TITLE')
152  ->will($this->returnValue($this->shippingMethodMock));
153  $this->shippingMethodMock->expects($this->once())
154  ->method('setMethodTitle')
155  ->with('METHOD_TITLE')
156  ->will($this->returnValue($this->shippingMethodMock));
157  $this->shippingMethodMock->expects($this->once())
158  ->method('setAmount')
159  ->with('100.12')
160  ->will($this->returnValue($this->shippingMethodMock));
161  $this->shippingMethodMock->expects($this->once())
162  ->method('setBaseAmount')
163  ->with('90.12')
164  ->will($this->returnValue($this->shippingMethodMock));
165  $this->shippingMethodMock->expects($this->once())
166  ->method('setAvailable')
167  ->with(true)
168  ->will($this->returnValue($this->shippingMethodMock));
169  $this->shippingMethodMock->expects($this->once())
170  ->method('setPriceExclTax')
171  ->with($shippingPriceExclTax)
172  ->will($this->returnValue($this->shippingMethodMock));
173  $this->shippingMethodMock->expects($this->once())
174  ->method('setPriceInclTax')
175  ->with($shippingPriceInclTax)
176  ->will($this->returnValue($this->shippingMethodMock));
177 
178  $this->taxHelper->expects($this->at(0))
179  ->method('getShippingPrice')
180  ->with($price, false, $addressMock, $customerTaxClassId)
181  ->willReturn($shippingPriceExclTax);
182 
183  $this->taxHelper->expects($this->at(1))
184  ->method('getShippingPrice')
185  ->with($price, true, $addressMock, $customerTaxClassId)
186  ->willReturn($shippingPriceInclTax);
187 
188  $this->assertEquals(
189  $this->shippingMethodMock,
190  $this->converter->modelToDataObject($this->rateModelMock, 'USD')
191  );
192  }
193 }
$objectManager
Definition: bootstrap.php:17
$price