OrderCustomerDelegateInterfaceTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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\Sales\Api;
  8. use Magento\Customer\Api\AccountManagementInterface;
  9. use Magento\Customer\Api\Data\AddressInterface;
  10. use Magento\Customer\Api\Data\CustomerInterface;
  11. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  12. use Magento\Sales\Api\Data\OrderAddressInterface;
  13. use Magento\Sales\Model\Order;
  14. use Magento\Sales\Model\OrderFactory;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. use PHPUnit\Framework\TestCase;
  17. /**
  18. * Test for Magento\Sales\Api\OrderCustomerDelegateInterface class.
  19. *
  20. * @magentoAppIsolation enabled
  21. */
  22. class OrderCustomerDelegateInterfaceTest extends TestCase
  23. {
  24. /**
  25. * @var OrderCustomerDelegateInterface
  26. */
  27. private $delegate;
  28. /**
  29. * @var OrderRepositoryInterface
  30. */
  31. private $orderRepository;
  32. /**
  33. * @var CustomerInterfaceFactory
  34. */
  35. private $customerFactory;
  36. /**
  37. * @var AccountManagementInterface
  38. */
  39. private $accountManagement;
  40. /**
  41. * @var OrderFactory
  42. */
  43. private $orderFactory;
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function setUp()
  48. {
  49. $this->delegate = Bootstrap::getObjectManager()->get(
  50. OrderCustomerDelegateInterface::class
  51. );
  52. $this->orderRepository = Bootstrap::getObjectManager()->get(
  53. OrderRepositoryInterface::class
  54. );
  55. $this->customerFactory = Bootstrap::getObjectManager()->get(
  56. CustomerInterfaceFactory::class
  57. );
  58. $this->accountManagement = Bootstrap::getObjectManager()->get(
  59. AccountManagementInterface::class
  60. );
  61. $this->orderFactory = Bootstrap::getObjectManager()->get(
  62. OrderFactory::class
  63. );
  64. }
  65. /**
  66. * @param OrderAddressInterface $orderAddress
  67. * @param AddressInterface $address
  68. *
  69. * @return void
  70. */
  71. private function compareAddresses(
  72. OrderAddressInterface $orderAddress,
  73. AddressInterface $address
  74. ): void {
  75. $this->assertEquals(
  76. $orderAddress->getFirstname(),
  77. $address->getFirstname()
  78. );
  79. $this->assertEquals(
  80. $orderAddress->getLastname(),
  81. $address->getLastname()
  82. );
  83. $this->assertEquals(
  84. $orderAddress->getCompany(),
  85. $address->getCompany()
  86. );
  87. $this->assertEquals(
  88. $orderAddress->getStreet(),
  89. $address->getStreet()
  90. );
  91. $this->assertEquals(
  92. $orderAddress->getCity(),
  93. $address->getCity()
  94. );
  95. if (!$address->getRegionId()) {
  96. $this->assertEmpty($address->getRegionId());
  97. } else {
  98. $this->assertEquals(
  99. $orderAddress->getRegionId(),
  100. $address->getRegionId()
  101. );
  102. }
  103. $this->assertEquals(
  104. $orderAddress->getPostcode(),
  105. $address->getPostcode()
  106. );
  107. $this->assertEquals(
  108. $orderAddress->getCountryId(),
  109. $address->getCountryId()
  110. );
  111. $this->assertEquals(
  112. $orderAddress->getTelephone(),
  113. $address->getTelephone()
  114. );
  115. }
  116. /**
  117. * @magentoDbIsolation enabled
  118. * @magentoAppIsolation enabled
  119. * @magentoDataFixture Magento/Sales/_files/order.php
  120. * @return void
  121. */
  122. public function testDelegateNew(): void
  123. {
  124. $orderAutoincrementId = '100000001';
  125. /** @var Order $orderModel */
  126. $orderModel = $this->orderFactory->create();
  127. $orderModel->loadByIncrementId($orderAutoincrementId);
  128. $orderId = (int)$orderModel->getId();
  129. unset($orderModel);
  130. $this->delegate->delegateNew($orderId);
  131. //Saving new customer with prepared data from order.
  132. /** @var CustomerInterface $customer */
  133. $customer = $this->customerFactory->create();
  134. $customer->setWebsiteId(1)
  135. ->setEmail('customer_order_delegate@example.com')
  136. ->setGroupId(1)
  137. ->setStoreId(1)
  138. ->setPrefix('Mr.')
  139. ->setFirstname('John')
  140. ->setMiddlename('A')
  141. ->setLastname('Smith')
  142. ->setSuffix('Esq.')
  143. ->setTaxvat('12')
  144. ->setGender(0);
  145. $createdCustomer = $this->accountManagement->createAccount(
  146. $customer,
  147. '12345abcD'
  148. );
  149. //Testing that addresses from order and the order itself are assigned
  150. //to customer.
  151. $order = $this->orderRepository->get($orderId);
  152. $this->assertCount(1, $createdCustomer->getAddresses());
  153. $this->assertNotNull($createdCustomer->getDefaultBilling());
  154. $this->assertNotNull($createdCustomer->getDefaultShipping());
  155. foreach ($createdCustomer->getAddresses() as $address) {
  156. $this->assertTrue(
  157. $address->isDefaultBilling() || $address->isDefaultShipping()
  158. );
  159. if ($address->isDefaultBilling()) {
  160. $this->compareAddresses($order->getBillingAddress(), $address);
  161. } elseif ($address->isDefaultShipping()) {
  162. $this->compareAddresses($order->getShippingAddress(), $address);
  163. }
  164. }
  165. $this->assertEquals($order->getCustomerId(), $createdCustomer->getId());
  166. }
  167. /**
  168. * @magentoDbIsolation enabled
  169. * @magentoAppIsolation enabled
  170. * @magentoDataFixture Magento/Sales/_files/order_different_addresses.php
  171. * @return void
  172. */
  173. public function testDelegateNewDifferentAddresses(): void
  174. {
  175. $orderAutoincrementId = '100000001';
  176. /** @var Order $orderModel */
  177. $orderModel = $this->orderFactory->create();
  178. $orderModel->loadByIncrementId($orderAutoincrementId);
  179. $orderId = (int)$orderModel->getId();
  180. unset($orderModel);
  181. $this->delegate->delegateNew($orderId);
  182. //Saving new customer with prepared data from order.
  183. /** @var CustomerInterface $customer */
  184. $customer = $this->customerFactory->create();
  185. $customer->setWebsiteId(1)
  186. ->setEmail('customer_order_delegate@example.com')
  187. ->setGroupId(1)
  188. ->setStoreId(1)
  189. ->setPrefix('Mr.')
  190. ->setFirstname('John')
  191. ->setMiddlename('A')
  192. ->setLastname('Smith')
  193. ->setSuffix('Esq.')
  194. ->setTaxvat('12')
  195. ->setGender(0);
  196. $createdCustomer = $this->accountManagement->createAccount(
  197. $customer,
  198. '12345abcD'
  199. );
  200. //Testing that addresses from order and the order itself are assigned
  201. //to customer.
  202. $order = $this->orderRepository->get($orderId);
  203. $this->assertCount(2, $createdCustomer->getAddresses());
  204. $this->assertNotNull($createdCustomer->getDefaultBilling());
  205. $this->assertNotNull($createdCustomer->getDefaultShipping());
  206. foreach ($createdCustomer->getAddresses() as $address) {
  207. $this->assertTrue(
  208. $address->isDefaultBilling() || $address->isDefaultShipping()
  209. );
  210. if ($address->isDefaultBilling()) {
  211. $this->compareAddresses($order->getBillingAddress(), $address);
  212. } elseif ($address->isDefaultShipping()) {
  213. $this->compareAddresses($order->getShippingAddress(), $address);
  214. }
  215. }
  216. $this->assertEquals($order->getCustomerId(), $createdCustomer->getId());
  217. }
  218. }