OverviewTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Multishipping\Test\Unit\Block\Checkout;
  8. use Magento\Multishipping\Block\Checkout\Overview;
  9. use Magento\Quote\Model\Quote\Address;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class OverviewTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var Overview
  17. */
  18. protected $model;
  19. /**
  20. * @var \PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $priceCurrencyMock;
  23. /**
  24. * @var \PHPUnit_Framework_MockObject_MockObject
  25. */
  26. protected $addressMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject
  29. */
  30. protected $totalsReaderMock;
  31. /**
  32. * @var \PHPUnit_Framework_MockObject_MockObject
  33. */
  34. protected $totalsCollectorMock;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject
  37. */
  38. protected $checkoutMock;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. protected $quoteMock;
  43. /**
  44. * @var \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $urlBuilderMock;
  47. protected function setUp()
  48. {
  49. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  50. $this->addressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
  51. 'getShippingMethod',
  52. 'getShippingRateByCode',
  53. 'getAllVisibleItems',
  54. 'getTotals',
  55. 'getAddressType',
  56. '__wakeup'
  57. ]);
  58. $this->priceCurrencyMock =
  59. $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
  60. $this->totalsReaderMock = $this->createMock(\Magento\Quote\Model\Quote\TotalsReader::class);
  61. $this->totalsCollectorMock = $this->createMock(\Magento\Quote\Model\Quote\TotalsCollector::class);
  62. $this->checkoutMock =
  63. $this->createMock(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
  64. $this->quoteMock = $this->createMock(\Magento\Quote\Model\Quote::class);
  65. $this->urlBuilderMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  66. $this->model = $objectManager->getObject(
  67. \Magento\Multishipping\Block\Checkout\Overview::class,
  68. [
  69. 'priceCurrency' => $this->priceCurrencyMock,
  70. 'totalsCollector' => $this->totalsCollectorMock,
  71. 'totalsReader' => $this->totalsReaderMock,
  72. 'multishipping' => $this->checkoutMock,
  73. 'urlBuilder' => $this->urlBuilderMock
  74. ]
  75. );
  76. }
  77. public function testGetShippingRateByCode()
  78. {
  79. $rateMock = $this->createMock(\Magento\Quote\Model\Quote\Address\Rate::class);
  80. $this->addressMock->expects($this->once())
  81. ->method('getShippingMethod')->will($this->returnValue('shipping method'));
  82. $this->addressMock->expects($this->once())
  83. ->method('getShippingRateByCode')
  84. ->with('shipping method')
  85. ->willReturn($rateMock);
  86. $this->assertEquals($rateMock, $this->model->getShippingAddressRate($this->addressMock));
  87. }
  88. public function testGetShippingRateByCodeWithEmptyRate()
  89. {
  90. $this->addressMock->expects($this->once())
  91. ->method('getShippingMethod')->will($this->returnValue('shipping method'));
  92. $this->addressMock->expects($this->once())
  93. ->method('getShippingRateByCode')
  94. ->with('shipping method')
  95. ->willReturn(false);
  96. $this->assertFalse($this->model->getShippingAddressRate($this->addressMock));
  97. }
  98. public function testGetShippingAddressItems()
  99. {
  100. $this->addressMock->expects($this->once())->method('getAllVisibleItems')->willReturn(['expected array']);
  101. $this->assertEquals(['expected array'], $this->model->getShippingAddressItems($this->addressMock));
  102. }
  103. public function testGetShippingAddressTotals()
  104. {
  105. $totalMock = $this->createPartialMock(
  106. \Magento\Sales\Model\Order\Total::class,
  107. ['getCode', 'setTitle', '__wakeup']
  108. );
  109. $totalMock->expects($this->once())->method('getCode')->willReturn('grand_total');
  110. $this->addressMock->expects($this->once())->method('getAddressType')->willReturn(Address::TYPE_BILLING);
  111. $this->addressMock->expects($this->once())->method('getTotals')->willReturn([$totalMock]);
  112. $totalMock->expects($this->once())->method('setTitle')->with('Total');
  113. $this->assertEquals([$totalMock], $this->model->getShippingAddressTotals($this->addressMock));
  114. }
  115. public function testGetShippingAddressTotalsWithNotBillingAddress()
  116. {
  117. $totalMock = $this->createPartialMock(
  118. \Magento\Sales\Model\Order\Total::class,
  119. ['getCode', 'setTitle', '__wakeup']
  120. );
  121. $totalMock->expects($this->once())->method('getCode')->willReturn('grand_total');
  122. $this->addressMock->expects($this->once())->method('getAddressType')->willReturn('not billing');
  123. $this->addressMock->expects($this->once())->method('getTotals')->willReturn([$totalMock]);
  124. $totalMock->expects($this->once())->method('setTitle')->with('Total for this address');
  125. $this->assertEquals([$totalMock], $this->model->getShippingAddressTotals($this->addressMock));
  126. }
  127. /**
  128. * @param \PHPUnit_Framework_MockObject_MockObject $address
  129. * @return \PHPUnit_Framework_MockObject_MockObject
  130. */
  131. protected function getTotalsMock($address)
  132. {
  133. $totalMock = $this->createPartialMock(\Magento\Sales\Model\Order\Total::class, [
  134. 'getCode',
  135. 'setTitle',
  136. '__wakeup'
  137. ]);
  138. $totalsAddressMock = $this->createMock(\Magento\Quote\Model\Quote\Address\Total::class);
  139. $this->checkoutMock->expects($this->once())->method('getQuote')->willReturn($this->quoteMock);
  140. $this->totalsCollectorMock
  141. ->expects($this->once())
  142. ->method('collectAddressTotals')
  143. ->with($this->quoteMock, $address)->willReturn($totalsAddressMock);
  144. $totalsAddressMock->expects($this->once())->method('getData')->willReturn([]);
  145. $this->totalsReaderMock
  146. ->expects($this->once())
  147. ->method('fetch')
  148. ->with($this->quoteMock, [])
  149. ->willReturn([$totalMock]);
  150. return $totalMock;
  151. }
  152. public function testGetVirtualProductEditUrl()
  153. {
  154. $url = 'http://example.com';
  155. $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('checkout/cart', [])->willReturn($url);
  156. $this->assertEquals($url, $this->model->getVirtualProductEditUrl());
  157. }
  158. }