QuoteUpdaterTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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\Gateway\Config\PayPal\Config;
  9. use Magento\Braintree\Model\Paypal\Helper\QuoteUpdater;
  10. use Magento\Braintree\Model\Ui\PayPal\ConfigProvider;
  11. use Magento\Braintree\Observer\DataAssignObserver;
  12. use Magento\Quote\Api\CartRepositoryInterface;
  13. use Magento\Quote\Api\Data\CartExtensionInterface;
  14. use Magento\Quote\Model\Quote;
  15. use Magento\Quote\Model\Quote\Address;
  16. use Magento\Quote\Model\Quote\Payment;
  17. use PHPUnit_Framework_MockObject_MockObject as MockObject;
  18. /**
  19. * Class QuoteUpdaterTest
  20. *
  21. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  22. */
  23. class QuoteUpdaterTest extends \PHPUnit\Framework\TestCase
  24. {
  25. const TEST_NONCE = '3ede7045-2aea-463e-9754-cd658ffeeb48';
  26. /**
  27. * @var Config|MockObject
  28. */
  29. private $config;
  30. /**
  31. * @var CartRepositoryInterface|MockObject
  32. */
  33. private $quoteRepository;
  34. /**
  35. * @var Address|MockObject
  36. */
  37. private $billingAddress;
  38. /**
  39. * @var Address|MockObject
  40. */
  41. private $shippingAddress;
  42. /**
  43. * @var QuoteUpdater
  44. */
  45. private $quoteUpdater;
  46. /**
  47. * @inheritdoc
  48. */
  49. protected function setUp()
  50. {
  51. $this->config = $this->getMockBuilder(Config::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->quoteRepository = $this->getMockBuilder(CartRepositoryInterface::class)
  55. ->getMockForAbstractClass();
  56. $this->billingAddress = $this->getMockBuilder(Address::class)
  57. ->setMethods(
  58. [
  59. 'setLastname',
  60. 'setFirstname',
  61. 'setEmail',
  62. 'setCollectShippingRates',
  63. 'setStreet',
  64. 'setCity',
  65. 'setRegionCode',
  66. 'setCountryId',
  67. 'setPostcode',
  68. 'setShouldIgnoreValidation',
  69. 'getEmail'
  70. ]
  71. )
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->shippingAddress = $this->getMockBuilder(Address::class)
  75. ->setMethods(
  76. [
  77. 'setLastname',
  78. 'setFirstname',
  79. 'setEmail',
  80. 'setCollectShippingRates',
  81. 'setStreet',
  82. 'setCity',
  83. 'setRegionCode',
  84. 'setCountryId',
  85. 'setPostcode',
  86. 'setShouldIgnoreValidation'
  87. ]
  88. )
  89. ->disableOriginalConstructor()
  90. ->getMock();
  91. $this->quoteUpdater = new QuoteUpdater(
  92. $this->config,
  93. $this->quoteRepository
  94. );
  95. }
  96. /**
  97. * Checks if quote details can be update by the response from Braintree.
  98. *
  99. * @throws \Magento\Framework\Exception\LocalizedException
  100. */
  101. public function testExecute(): void
  102. {
  103. $details = $this->getDetails();
  104. $quote = $this->getQuoteMock();
  105. $payment = $this->getPaymentMock();
  106. $quote->method('getPayment')
  107. ->willReturn($payment);
  108. $payment->method('setMethod')
  109. ->with(ConfigProvider::PAYPAL_CODE);
  110. $payment->method('setAdditionalInformation')
  111. ->with(DataAssignObserver::PAYMENT_METHOD_NONCE, self::TEST_NONCE);
  112. $this->updateQuoteStep($quote, $details);
  113. $this->quoteUpdater->execute(self::TEST_NONCE, $details, $quote);
  114. }
  115. /**
  116. * Disables quote's addresses validation.
  117. *
  118. * @return void
  119. */
  120. private function disabledQuoteAddressValidationStep(): void
  121. {
  122. $this->billingAddress->method('setShouldIgnoreValidation')
  123. ->with(true);
  124. $this->shippingAddress->method('setShouldIgnoreValidation')
  125. ->with(true);
  126. $this->billingAddress->method('getEmail')
  127. ->willReturn('bt_buyer_us@paypal.com');
  128. }
  129. /**
  130. * Gets quote's details.
  131. *
  132. * @return array
  133. */
  134. private function getDetails(): array
  135. {
  136. return [
  137. 'email' => 'bt_buyer_us@paypal.com',
  138. 'payerId' => 'FAKE_PAYER_ID',
  139. 'firstName' => 'John',
  140. 'lastName' => 'Doe',
  141. 'phone' => '312-123-4567',
  142. 'countryCode' => 'US',
  143. 'shippingAddress' => [
  144. 'streetAddress' => '123 Division Street',
  145. 'extendedAddress' => 'Apt. #1',
  146. 'locality' => 'Chicago',
  147. 'region' => 'IL',
  148. 'postalCode' => '60618',
  149. 'countryCodeAlpha2' => 'US',
  150. 'recipientName' => 'John Doe',
  151. ],
  152. 'billingAddress' => [
  153. 'streetAddress' => '123 Billing Street',
  154. 'extendedAddress' => 'Apt. #1',
  155. 'locality' => 'Chicago',
  156. 'region' => 'IL',
  157. 'postalCode' => '60618',
  158. 'countryCodeAlpha2' => 'US',
  159. ],
  160. ];
  161. }
  162. /**
  163. * Updates shipping address details.
  164. *
  165. * @param array $details
  166. */
  167. private function updateShippingAddressStep(array $details): void
  168. {
  169. $this->shippingAddress->method('setLastname')
  170. ->with($details['lastName']);
  171. $this->shippingAddress->method('setFirstname')
  172. ->with($details['firstName']);
  173. $this->shippingAddress->method('setEmail')
  174. ->with($details['email']);
  175. $this->shippingAddress->method('setCollectShippingRates')
  176. ->with(true);
  177. $this->updateAddressDataStep($this->shippingAddress, $details['shippingAddress']);
  178. }
  179. /**
  180. * Updates address details.
  181. *
  182. * @param MockObject $address
  183. * @param array $addressData
  184. */
  185. private function updateAddressDataStep(MockObject $address, array $addressData): void
  186. {
  187. $address->method('setStreet')
  188. ->with([$addressData['streetAddress'], $addressData['extendedAddress']]);
  189. $address->method('setCity')
  190. ->with($addressData['locality']);
  191. $address->method('setRegionCode')
  192. ->with($addressData['region']);
  193. $address->method('setCountryId')
  194. ->with($addressData['countryCodeAlpha2']);
  195. $address->method('setPostcode')
  196. ->with($addressData['postalCode']);
  197. }
  198. /**
  199. * Updates quote's address details.
  200. *
  201. * @param MockObject $quoteMock
  202. * @param array $details
  203. */
  204. private function updateQuoteAddressStep(MockObject $quoteMock, array $details): void
  205. {
  206. $quoteMock->expects(self::exactly(2))
  207. ->method('getIsVirtual')
  208. ->willReturn(false);
  209. $this->updateShippingAddressStep($details);
  210. $this->updateBillingAddressStep($details);
  211. }
  212. /**
  213. * Updates billing address details.
  214. *
  215. * @param array $details
  216. */
  217. private function updateBillingAddressStep(array $details): void
  218. {
  219. $this->config->method('isRequiredBillingAddress')
  220. ->willReturn(true);
  221. $this->updateAddressDataStep($this->billingAddress, $details['billingAddress']);
  222. $this->billingAddress->method('setLastname')
  223. ->with($details['lastName']);
  224. $this->billingAddress->method('setFirstname')
  225. ->with($details['firstName']);
  226. $this->billingAddress->method('setEmail')
  227. ->with($details['email']);
  228. }
  229. /**
  230. * Updates quote details.
  231. *
  232. * @param MockObject $quote
  233. * @param array $details
  234. */
  235. private function updateQuoteStep(MockObject $quote, array $details): void
  236. {
  237. $quote->method('setMayEditShippingAddress')
  238. ->with(false);
  239. $quote->method('setMayEditShippingMethod')
  240. ->with(true);
  241. $quote->method('getShippingAddress')
  242. ->willReturn($this->shippingAddress);
  243. $quote->expects(self::exactly(2))
  244. ->method('getBillingAddress')
  245. ->willReturn($this->billingAddress);
  246. $this->updateQuoteAddressStep($quote, $details);
  247. $this->disabledQuoteAddressValidationStep();
  248. $quote->method('collectTotals');
  249. $this->quoteRepository->method('save')
  250. ->with($quote);
  251. }
  252. /**
  253. * Creates a mock for Quote object.
  254. *
  255. * @return Quote|MockObject
  256. */
  257. private function getQuoteMock(): MockObject
  258. {
  259. $quote = $this->getMockBuilder(Quote::class)
  260. ->setMethods(
  261. [
  262. 'getIsVirtual',
  263. 'getPayment',
  264. 'setMayEditShippingAddress',
  265. 'setMayEditShippingMethod',
  266. 'collectTotals',
  267. 'getShippingAddress',
  268. 'getBillingAddress',
  269. 'getExtensionAttributes'
  270. ]
  271. )
  272. ->disableOriginalConstructor()
  273. ->getMock();
  274. $cartExtension = $this->getMockBuilder(CartExtensionInterface::class)
  275. ->setMethods(['setShippingAssignments'])
  276. ->disableOriginalConstructor()
  277. ->getMockForAbstractClass();
  278. $quote->method('getExtensionAttributes')
  279. ->willReturn($cartExtension);
  280. return $quote;
  281. }
  282. /**
  283. * Creates a mock for Payment object.
  284. *
  285. * @return Payment|MockObject
  286. */
  287. private function getPaymentMock(): MockObject
  288. {
  289. return $this->getMockBuilder(Payment::class)
  290. ->disableOriginalConstructor()
  291. ->getMock();
  292. }
  293. }