OrderPlaceTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Braintree\Test\Unit\Model\Paypal\Helper;
  8. use Magento\Braintree\Model\Paypal\Helper\OrderPlace;
  9. use Magento\Braintree\Model\Paypal\OrderCancellationService;
  10. use Magento\Checkout\Api\AgreementsValidatorInterface;
  11. use Magento\Checkout\Helper\Data;
  12. use Magento\Checkout\Model\Type\Onepage;
  13. use Magento\Customer\Model\Group;
  14. use Magento\Customer\Model\Session;
  15. use Magento\Quote\Api\CartManagementInterface;
  16. use Magento\Quote\Model\Quote;
  17. use Magento\Quote\Model\Quote\Address;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. /**
  20. * Class OrderPlaceTest
  21. *
  22. * @see \Magento\Braintree\Model\Paypal\Helper\OrderPlace
  23. *
  24. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  25. */
  26. class OrderPlaceTest extends \PHPUnit\Framework\TestCase
  27. {
  28. const TEST_EMAIL = 'test@test.loc';
  29. /**
  30. * @var CartManagementInterface|MockObject
  31. */
  32. private $cartManagement;
  33. /**
  34. * @var AgreementsValidatorInterface|MockObject
  35. */
  36. private $agreementsValidator;
  37. /**
  38. * @var Session|MockObject
  39. */
  40. private $customerSession;
  41. /**
  42. * @var Data|MockObject
  43. */
  44. private $checkoutHelper;
  45. /**
  46. * @var Address|MockObject
  47. */
  48. private $billingAddress;
  49. /**
  50. * @var OrderPlace
  51. */
  52. private $orderPlace;
  53. /**
  54. * @var OrderCancellationService|MockObject
  55. */
  56. private $orderCancellation;
  57. /**
  58. * @inheritdoc
  59. */
  60. protected function setUp()
  61. {
  62. $this->cartManagement = $this->getMockBuilder(CartManagementInterface::class)
  63. ->getMockForAbstractClass();
  64. $this->agreementsValidator = $this->getMockBuilder(AgreementsValidatorInterface::class)
  65. ->getMockForAbstractClass();
  66. $this->customerSession = $this->getMockBuilder(Session::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $this->checkoutHelper = $this->getMockBuilder(Data::class)
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $this->orderCancellation = $this->getMockBuilder(OrderCancellationService::class)
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $this->orderPlace = new OrderPlace(
  76. $this->cartManagement,
  77. $this->agreementsValidator,
  78. $this->customerSession,
  79. $this->checkoutHelper,
  80. $this->orderCancellation
  81. );
  82. }
  83. /**
  84. * Checks a scenario for a guest customer.
  85. *
  86. * @throws \Exception
  87. */
  88. public function testExecuteGuest()
  89. {
  90. $agreement = ['test', 'test'];
  91. $quoteMock = $this->getQuoteMock();
  92. $this->agreementsValidator->expects(self::once())
  93. ->method('isValid')
  94. ->willReturn(true);
  95. $this->getCheckoutMethodStep($quoteMock);
  96. $this->prepareGuestQuoteStep($quoteMock);
  97. $this->disabledQuoteAddressValidationStep($quoteMock);
  98. $quoteMock->expects(self::once())
  99. ->method('collectTotals');
  100. $quoteMock->expects(self::once())
  101. ->method('getId')
  102. ->willReturn(10);
  103. $this->cartManagement->expects(self::once())
  104. ->method('placeOrder')
  105. ->with(10);
  106. $this->orderPlace->execute($quoteMock, $agreement);
  107. }
  108. /**
  109. * Disables address validation.
  110. *
  111. * @param MockObject $quoteMock
  112. */
  113. private function disabledQuoteAddressValidationStep(MockObject $quoteMock)
  114. {
  115. $billingAddressMock = $this->getBillingAddressMock($quoteMock);
  116. $shippingAddressMock = $this->getMockBuilder(Address::class)
  117. ->setMethods(['setShouldIgnoreValidation'])
  118. ->disableOriginalConstructor()
  119. ->getMock();
  120. $quoteMock->method('getShippingAddress')
  121. ->willReturn($shippingAddressMock);
  122. $billingAddressMock->method('setShouldIgnoreValidation')
  123. ->with(true)
  124. ->willReturnSelf();
  125. $quoteMock->method('getIsVirtual')
  126. ->willReturn(false);
  127. $shippingAddressMock->method('setShouldIgnoreValidation')
  128. ->with(true)
  129. ->willReturnSelf();
  130. $billingAddressMock->method('getEmail')
  131. ->willReturn(self::TEST_EMAIL);
  132. $billingAddressMock->expects(self::never())
  133. ->method('setSameAsBilling');
  134. }
  135. /**
  136. * Prepares checkout step.
  137. *
  138. * @param MockObject $quoteMock
  139. */
  140. private function getCheckoutMethodStep(MockObject $quoteMock)
  141. {
  142. $this->customerSession->method('isLoggedIn')
  143. ->willReturn(false);
  144. $quoteMock->expects(self::at(1))
  145. ->method('getCheckoutMethod')
  146. ->willReturn(null);
  147. $this->checkoutHelper->method('isAllowedGuestCheckout')
  148. ->with($quoteMock)
  149. ->willReturn(true);
  150. $quoteMock->method('setCheckoutMethod')
  151. ->with(Onepage::METHOD_GUEST);
  152. $quoteMock->expects(self::at(2))
  153. ->method('getCheckoutMethod')
  154. ->willReturn(Onepage::METHOD_GUEST);
  155. }
  156. /**
  157. * Prepares quote.
  158. *
  159. * @param MockObject $quoteMock
  160. */
  161. private function prepareGuestQuoteStep(MockObject $quoteMock)
  162. {
  163. $billingAddressMock = $this->getBillingAddressMock($quoteMock);
  164. $quoteMock->expects(self::once())
  165. ->method('setCustomerId')
  166. ->with(null)
  167. ->willReturnSelf();
  168. $billingAddressMock->expects(self::at(0))
  169. ->method('getEmail')
  170. ->willReturn(self::TEST_EMAIL);
  171. $quoteMock->method('setCustomerEmail')
  172. ->with(self::TEST_EMAIL)
  173. ->willReturnSelf();
  174. $quoteMock->method('setCustomerIsGuest')
  175. ->with(true)
  176. ->willReturnSelf();
  177. $quoteMock->method('setCustomerGroupId')
  178. ->with(Group::NOT_LOGGED_IN_ID)
  179. ->willReturnSelf();
  180. }
  181. /**
  182. * Gets a mock object for a billing address entity.
  183. *
  184. * @param MockObject $quoteMock
  185. * @return Address|MockObject
  186. */
  187. private function getBillingAddressMock(MockObject $quoteMock)
  188. {
  189. if (!isset($this->billingAddress)) {
  190. $this->billingAddress = $this->getMockBuilder(Address::class)
  191. ->setMethods(['setShouldIgnoreValidation', 'getEmail', 'setSameAsBilling'])
  192. ->disableOriginalConstructor()
  193. ->getMock();
  194. }
  195. $quoteMock->method('getBillingAddress')
  196. ->willReturn($this->billingAddress);
  197. return $this->billingAddress;
  198. }
  199. /**
  200. * Gets a mock object for a quote.
  201. *
  202. * @return Quote|MockObject
  203. */
  204. private function getQuoteMock()
  205. {
  206. return $this->getMockBuilder(Quote::class)
  207. ->setMethods(
  208. [
  209. 'setCustomerId',
  210. 'setCustomerEmail',
  211. 'setCustomerIsGuest',
  212. 'setCustomerGroupId',
  213. 'getCheckoutMethod',
  214. 'setCheckoutMethod',
  215. 'collectTotals',
  216. 'getId',
  217. 'getBillingAddress',
  218. 'getShippingAddress',
  219. 'getIsVirtual'
  220. ]
  221. )->disableOriginalConstructor()
  222. ->getMock();
  223. }
  224. }