Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
8 
12 
16 class DataTest extends \PHPUnit\Framework\TestCase
17 {
21  private $priceCurrency;
22 
26  private $helper;
27 
31  private $transportBuilder;
32 
36  private $translator;
37 
41  private $checkoutSession;
42 
46  private $scopeConfig;
47 
51  private $eventManager;
52 
56  protected function setUp()
57  {
58  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59  $className = \Magento\Checkout\Helper\Data::class;
60  $arguments = $objectManagerHelper->getConstructArguments($className);
62  $context = $arguments['context'];
63  $this->translator = $arguments['inlineTranslation'];
64  $this->eventManager = $context->getEventManager();
65  $this->scopeConfig = $context->getScopeConfig();
66  $this->scopeConfig->expects($this->any())
67  ->method('getValue')
68  ->willReturnMap(
69  [
70  [
71  'checkout/payment_failed/template',
73  8,
74  'fixture_email_template_payment_failed',
75  ],
76  [
77  'checkout/payment_failed/receiver',
79  8,
80  'sysadmin',
81  ],
82  [
83  'trans_email/ident_sysadmin/email',
85  8,
87  ],
88  [
89  'trans_email/ident_sysadmin/name',
91  8,
92  'System Administrator',
93  ],
94  [
95  'checkout/payment_failed/identity',
97  8,
99  ],
100  [
101  'carriers/ground/title',
103  null,
104  'Ground Shipping',
105  ],
106  [
107  'payment/fixture-payment-method/title',
109  null,
110  'Check Money Order',
111  ],
112  [
113  'checkout/options/onepage_checkout_enabled',
115  null,
116  'One Page Checkout',
117  ],
118  ]
119  );
120 
121  $this->checkoutSession = $arguments['checkoutSession'];
122  $arguments['localeDate']->expects($this->any())
123  ->method('formatDateTime')
124  ->willReturn('Oct 02, 2013');
125 
126  $this->transportBuilder = $arguments['transportBuilder'];
127 
128  $this->priceCurrency = $arguments['priceCurrency'];
129 
130  $this->helper = $objectManagerHelper->getObject($className, $arguments);
131  }
132 
136  public function testSendPaymentFailedEmail()
137  {
138  $quoteMock = $this->getMockBuilder(\Magento\Quote\Model\Quote::class)
139  ->setMethods(['getId'])
140  ->disableOriginalConstructor()
141  ->getMock();
142  $quoteMock->expects($this->any())->method('getId')->willReturn(1);
143 
144  $this->assertSame($this->helper, $this->helper->sendPaymentFailedEmail($quoteMock, 'test message'));
145  }
146 
150  public function testGetCheckout()
151  {
152  $this->assertEquals($this->checkoutSession, $this->helper->getCheckout());
153  }
154 
155  public function testGetQuote()
156  {
157  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
158  $this->checkoutSession->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
159  $this->assertEquals($quoteMock, $this->helper->getQuote());
160  }
161 
162  public function testFormatPrice()
163  {
164  $price = 5.5;
165  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
166  $storeMock = $this->createPartialMock(\Magento\Store\Model\Store::class, ['formatPrice', '__wakeup']);
167  $this->checkoutSession->expects($this->once())->method('getQuote')->will($this->returnValue($quoteMock));
168  $quoteMock->expects($this->once())->method('getStore')->will($this->returnValue($storeMock));
169  $this->priceCurrency->expects($this->once())->method('format')->will($this->returnValue('5.5'));
170  $this->assertEquals('5.5', $this->helper->formatPrice($price));
171  }
172 
173  public function testConvertPrice()
174  {
175  $price = 5.5;
176  $this->priceCurrency->expects($this->once())->method('convertAndFormat')->willReturn($price);
177  $this->assertEquals(5.5, $this->helper->convertPrice($price));
178  }
179 
180  public function testCanOnepageCheckout()
181  {
182  $this->scopeConfig->expects($this->once())->method('getValue')->with(
183  'checkout/options/onepage_checkout_enabled',
184  'store'
185  )->will($this->returnValue(true));
186  $this->assertTrue($this->helper->canOnepageCheckout());
187  }
188 
189  public function testIsContextCheckout()
190  {
191  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
192  $context = $objectManagerHelper->getObject(
193  \Magento\Framework\App\Helper\Context::class
194  );
195  $helper = $objectManagerHelper->getObject(
196  \Magento\Checkout\Helper\Data::class,
197  ['context' => $context]
198  );
199  $context->getRequest()->expects($this->once())->method('getParam')->with('context')->will(
200  $this->returnValue('checkout')
201  );
202  $this->assertTrue($helper->isContextCheckout());
203  }
204 
205  public function testIsCustomerMustBeLogged()
206  {
207  $this->scopeConfig->expects($this->once())->method('isSetFlag')->with(
208  'checkout/options/customer_must_be_logged',
210  )->will($this->returnValue(true));
211  $this->assertTrue($this->helper->isCustomerMustBeLogged());
212  }
213 
214  public function testGetPriceInclTax()
215  {
216  $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getPriceInclTax']);
217  $itemMock->expects($this->exactly(2))->method('getPriceInclTax')->will($this->returnValue(5.5));
218  $this->assertEquals(5.5, $this->helper->getPriceInclTax($itemMock));
219  }
220 
222  {
223  $qty = 1;
224  $taxAmount = 1;
225  $discountTaxCompensation = 1;
226  $rowTotal = 15;
227  $roundPrice = 17;
228  $expected = 17;
229  $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
230  $objectManagerHelper = new ObjectManager($this);
231  $helper = $objectManagerHelper->getObject(
232  \Magento\Checkout\Helper\Data::class,
233  [
234  'storeManager' => $storeManager,
235  'priceCurrency' => $this->priceCurrency,
236  ]
237  );
238  $itemMock = $this->createPartialMock(
239  \Magento\Framework\DataObject::class,
240  ['getPriceInclTax', 'getQty', 'getTaxAmount', 'getDiscountTaxCompensation', 'getRowTotal', 'getQtyOrdered']
241  );
242  $itemMock->expects($this->once())->method('getPriceInclTax')->will($this->returnValue(false));
243  $itemMock->expects($this->exactly(2))->method('getQty')->will($this->returnValue($qty));
244  $itemMock->expects($this->never())->method('getQtyOrdered');
245  $itemMock->expects($this->once())->method('getTaxAmount')->will($this->returnValue($taxAmount));
246  $itemMock->expects($this->once())
247  ->method('getDiscountTaxCompensation')->will($this->returnValue($discountTaxCompensation));
248  $itemMock->expects($this->once())->method('getRowTotal')->will($this->returnValue($rowTotal));
249  $this->priceCurrency->expects($this->once())->method('round')->with($roundPrice)->willReturn($roundPrice);
250  $this->assertEquals($expected, $helper->getPriceInclTax($itemMock));
251  }
252 
253  public function testGetSubtotalInclTax()
254  {
255  $rowTotalInclTax = 5.5;
256  $expected = 5.5;
257  $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getRowTotalInclTax']);
258  $itemMock->expects($this->exactly(2))->method('getRowTotalInclTax')->will($this->returnValue($rowTotalInclTax));
259  $this->assertEquals($expected, $this->helper->getSubtotalInclTax($itemMock));
260  }
261 
263  {
264  $taxAmount = 1;
265  $discountTaxCompensation = 1;
266  $rowTotal = 15;
267  $expected = 17;
268  $itemMock = $this->createPartialMock(
269  \Magento\Framework\DataObject::class,
270  ['getRowTotalInclTax', 'getTaxAmount', 'getDiscountTaxCompensation', 'getRowTotal']
271  );
272  $itemMock->expects($this->once())->method('getRowTotalInclTax')->will($this->returnValue(false));
273  $itemMock->expects($this->once())->method('getTaxAmount')->will($this->returnValue($taxAmount));
274  $itemMock->expects($this->once())
275  ->method('getDiscountTaxCompensation')->will($this->returnValue($discountTaxCompensation));
276  $itemMock->expects($this->once())->method('getRowTotal')->will($this->returnValue($rowTotal));
277  $this->assertEquals($expected, $this->helper->getSubtotalInclTax($itemMock));
278  }
279 
281  {
282  $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
283  $objectManagerHelper = new ObjectManager($this);
284  $helper = $objectManagerHelper->getObject(
285  \Magento\Checkout\Helper\Data::class,
286  [
287  'storeManager' => $storeManager,
288  'priceCurrency' => $this->priceCurrency,
289  ]
290  );
291  $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getQty']);
292  $itemMock->expects($this->once())->method('getQty');
293  $this->priceCurrency->expects($this->once())->method('round');
294  $helper->getPriceInclTax($itemMock);
295  }
296 
297  public function testGetBasePriceInclTax()
298  {
299  $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
300  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
301  $helper = $objectManagerHelper->getObject(
302  \Magento\Checkout\Helper\Data::class,
303  [
304  'storeManager' => $storeManager,
305  'priceCurrency' => $this->priceCurrency,
306  ]
307  );
308  $itemMock = $this->createPartialMock(\Magento\Framework\DataObject::class, ['getQty', 'getQtyOrdered']);
309  $itemMock->expects($this->once())->method('getQty')->will($this->returnValue(false));
310  $itemMock->expects($this->exactly(2))->method('getQtyOrdered')->will($this->returnValue(5.5));
311  $this->priceCurrency->expects($this->once())->method('round');
312  $helper->getBasePriceInclTax($itemMock);
313  }
314 
315  public function testGetBaseSubtotalInclTax()
316  {
317  $itemMock = $this->createPartialMock(
318  \Magento\Framework\DataObject::class,
319  ['getBaseTaxAmount', 'getBaseDiscountTaxCompensation', 'getBaseRowTotal']
320  );
321  $itemMock->expects($this->once())->method('getBaseTaxAmount');
322  $itemMock->expects($this->once())->method('getBaseDiscountTaxCompensation');
323  $itemMock->expects($this->once())->method('getBaseRowTotal');
324  $this->helper->getBaseSubtotalInclTax($itemMock);
325  }
326 
328  {
329  $quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
330  $store = null;
331  $quoteMock->expects($this->once())->method('getStoreId')->will($this->returnValue(1));
332  $this->scopeConfig->expects($this->once())
333  ->method('isSetFlag')
334  ->will($this->returnValue(true));
335  $this->assertTrue($this->helper->isAllowedGuestCheckout($quoteMock, $store));
336  }
337 }
$storeManager
$price
$arguments
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31