ShippingMethodUpdaterTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Test\Unit\Model\Paypal\Helper;
  7. use Magento\Quote\Model\Quote;
  8. use Magento\Quote\Model\Quote\Address;
  9. use Magento\Quote\Api\CartRepositoryInterface;
  10. use Magento\Braintree\Gateway\Config\PayPal\Config;
  11. use Magento\Braintree\Model\Paypal\Helper\ShippingMethodUpdater;
  12. /**
  13. * Class ShippingMethodUpdaterTest
  14. *
  15. * @see \Magento\Braintree\Model\Paypal\Helper\ShippingMethodUpdater
  16. */
  17. class ShippingMethodUpdaterTest extends \PHPUnit\Framework\TestCase
  18. {
  19. const TEST_SHIPPING_METHOD = 'test-shipping-method';
  20. const TEST_EMAIL = 'test@test.loc';
  21. /**
  22. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $configMock;
  25. /**
  26. * @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $quoteRepositoryMock;
  29. /**
  30. * @var Address|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $shippingAddressMock;
  33. /**
  34. * @var ShippingMethodUpdater
  35. */
  36. private $shippingMethodUpdater;
  37. protected function setUp()
  38. {
  39. $this->configMock = $this->getMockBuilder(Config::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->quoteRepositoryMock = $this->getMockBuilder(CartRepositoryInterface::class)
  43. ->getMockForAbstractClass();
  44. $this->shippingAddressMock = $this->getMockBuilder(Address::class)
  45. ->setMethods(
  46. [
  47. 'setShouldIgnoreValidation',
  48. 'getShippingMethod',
  49. 'setShippingMethod',
  50. 'setCollectShippingRates'
  51. ]
  52. )->disableOriginalConstructor()
  53. ->getMock();
  54. $this->shippingMethodUpdater = new ShippingMethodUpdater(
  55. $this->configMock,
  56. $this->quoteRepositoryMock
  57. );
  58. }
  59. /**
  60. * @expectedException \InvalidArgumentException
  61. * @expectedExceptionMessage The "shippingMethod" field does not exists.
  62. */
  63. public function testExecuteException()
  64. {
  65. $quoteMock = $this->getQuoteMock();
  66. $this->shippingMethodUpdater->execute('', $quoteMock);
  67. }
  68. public function testExecute()
  69. {
  70. $quoteMock = $this->getQuoteMock();
  71. $quoteMock->expects(self::exactly(2))
  72. ->method('getIsVirtual')
  73. ->willReturn(false);
  74. $quoteMock->expects(self::exactly(2))
  75. ->method('getShippingAddress')
  76. ->willReturn($this->shippingAddressMock);
  77. $this->shippingAddressMock->expects(self::once())
  78. ->method('getShippingMethod')
  79. ->willReturn(self::TEST_SHIPPING_METHOD . '-bad');
  80. $this->disabledQuoteAddressValidationStep($quoteMock);
  81. $this->shippingAddressMock->expects(self::once())
  82. ->method('setShippingMethod')
  83. ->willReturn(self::TEST_SHIPPING_METHOD);
  84. $this->shippingAddressMock->expects(self::once())
  85. ->method('setCollectShippingRates')
  86. ->willReturn(true);
  87. $quoteMock->expects(self::once())
  88. ->method('collectTotals');
  89. $this->quoteRepositoryMock->expects(self::once())
  90. ->method('save')
  91. ->with($quoteMock);
  92. $this->shippingMethodUpdater->execute(self::TEST_SHIPPING_METHOD, $quoteMock);
  93. }
  94. /**
  95. * @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
  96. */
  97. private function disabledQuoteAddressValidationStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
  98. {
  99. $billingAddressMock = $this->getBillingAddressMock($quoteMock);
  100. $billingAddressMock->expects(self::once())
  101. ->method('setShouldIgnoreValidation')
  102. ->with(true)
  103. ->willReturnSelf();
  104. $this->shippingAddressMock->expects(self::once())
  105. ->method('setShouldIgnoreValidation')
  106. ->with(true)
  107. ->willReturnSelf();
  108. $billingAddressMock->expects(self::at(1))
  109. ->method('getEmail')
  110. ->willReturn(self::TEST_EMAIL);
  111. $billingAddressMock->expects(self::never())
  112. ->method('setSameAsBilling');
  113. }
  114. /**
  115. * @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
  116. * @return Address|\PHPUnit_Framework_MockObject_MockObject
  117. */
  118. private function getBillingAddressMock(\PHPUnit_Framework_MockObject_MockObject $quoteMock)
  119. {
  120. if (!isset($this->billingAddressMock)) {
  121. $this->billingAddressMock = $this->getMockBuilder(Address::class)
  122. ->setMethods(['setShouldIgnoreValidation', 'getEmail', 'setSameAsBilling'])
  123. ->disableOriginalConstructor()
  124. ->getMock();
  125. }
  126. $quoteMock->expects(self::any())
  127. ->method('getBillingAddress')
  128. ->willReturn($this->billingAddressMock);
  129. return $this->billingAddressMock;
  130. }
  131. /**
  132. * @return Quote|\PHPUnit_Framework_MockObject_MockObject
  133. */
  134. private function getQuoteMock()
  135. {
  136. return $this->getMockBuilder(Quote::class)
  137. ->setMethods(
  138. [
  139. 'collectTotals',
  140. 'getBillingAddress',
  141. 'getShippingAddress',
  142. 'getIsVirtual'
  143. ]
  144. )->disableOriginalConstructor()
  145. ->getMock();
  146. }
  147. }