Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OrderCustomerDelegateInterfaceTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
8 namespace Magento\Sales\Api;
9 
13 use Magento\Customer\Api\Data\CustomerInterfaceFactory;
16 use Magento\Sales\Model\OrderFactory;
18 use PHPUnit\Framework\TestCase;
19 
25 class OrderCustomerDelegateInterfaceTest extends TestCase
26 {
30  private $delegate;
31 
35  private $orderRepository;
36 
40  private $customerFactory;
41 
45  private $accountManagement;
46 
50  private $orderFactory;
51 
55  protected function setUp()
56  {
57  $this->delegate = Bootstrap::getObjectManager()->get(
58  OrderCustomerDelegateInterface::class
59  );
60  $this->orderRepository = Bootstrap::getObjectManager()->get(
61  OrderRepositoryInterface::class
62  );
63  $this->customerFactory = Bootstrap::getObjectManager()->get(
64  CustomerInterfaceFactory::class
65  );
66  $this->accountManagement = Bootstrap::getObjectManager()->get(
67  AccountManagementInterface::class
68  );
69  $this->orderFactory = Bootstrap::getObjectManager()->get(
70  OrderFactory::class
71  );
72  }
73 
80  private function compareAddresses(
81  OrderAddressInterface $orderAddress,
83  ): void {
84  $this->assertEquals(
85  $orderAddress->getFirstname(),
86  $address->getFirstname()
87  );
88  $this->assertEquals(
89  $orderAddress->getLastname(),
90  $address->getLastname()
91  );
92  $this->assertEquals(
93  $orderAddress->getCompany(),
94  $address->getCompany()
95  );
96  $this->assertEquals(
97  $orderAddress->getStreet(),
98  $address->getStreet()
99  );
100  $this->assertEquals(
101  $orderAddress->getCity(),
102  $address->getCity()
103  );
104  if (!$address->getRegionId()) {
105  $this->assertEmpty($address->getRegionId());
106  } else {
107  $this->assertEquals(
108  $orderAddress->getRegionId(),
109  $address->getRegionId()
110  );
111  }
112  $this->assertEquals(
113  $orderAddress->getPostcode(),
114  $address->getPostcode()
115  );
116  $this->assertEquals(
117  $orderAddress->getCountryId(),
118  $address->getCountryId()
119  );
120  $this->assertEquals(
121  $orderAddress->getTelephone(),
122  $address->getTelephone()
123  );
124  }
125 
132  public function testDelegateNew(): void
133  {
134  $orderAutoincrementId = '100000001';
136  $orderModel = $this->orderFactory->create();
137  $orderModel->loadByIncrementId($orderAutoincrementId);
138  $orderId = (int)$orderModel->getId();
139  unset($orderModel);
140 
141  $this->delegate->delegateNew($orderId);
142 
143  //Saving new customer with prepared data from order.
145  $customer = $this->customerFactory->create();
146  $customer->setWebsiteId(1)
147  ->setEmail('[email protected]')
148  ->setGroupId(1)
149  ->setStoreId(1)
150  ->setPrefix('Mr.')
151  ->setFirstname('John')
152  ->setMiddlename('A')
153  ->setLastname('Smith')
154  ->setSuffix('Esq.')
155  ->setTaxvat('12')
156  ->setGender(0);
157  $createdCustomer = $this->accountManagement->createAccount(
158  $customer,
159  '12345abcD'
160  );
161 
162  //Testing that addresses from order and the order itself are assigned
163  //to customer.
164  $order = $this->orderRepository->get($orderId);
165  $this->assertCount(1, $createdCustomer->getAddresses());
166  $this->assertNotNull($createdCustomer->getDefaultBilling());
167  $this->assertNotNull($createdCustomer->getDefaultShipping());
168  foreach ($createdCustomer->getAddresses() as $address) {
169  $this->assertTrue(
170  $address->isDefaultBilling() || $address->isDefaultShipping()
171  );
172  if ($address->isDefaultBilling()) {
173  $this->compareAddresses($order->getBillingAddress(), $address);
174  } elseif ($address->isDefaultShipping()) {
175  $this->compareAddresses($order->getShippingAddress(), $address);
176  }
177  }
178  $this->assertEquals($order->getCustomerId(), $createdCustomer->getId());
179  }
180 
187  public function testDelegateNewDifferentAddresses(): void
188  {
189  $orderAutoincrementId = '100000001';
191  $orderModel = $this->orderFactory->create();
192  $orderModel->loadByIncrementId($orderAutoincrementId);
193  $orderId = (int)$orderModel->getId();
194  unset($orderModel);
195 
196  $this->delegate->delegateNew($orderId);
197 
198  //Saving new customer with prepared data from order.
200  $customer = $this->customerFactory->create();
201  $customer->setWebsiteId(1)
202  ->setEmail('[email protected]')
203  ->setGroupId(1)
204  ->setStoreId(1)
205  ->setPrefix('Mr.')
206  ->setFirstname('John')
207  ->setMiddlename('A')
208  ->setLastname('Smith')
209  ->setSuffix('Esq.')
210  ->setTaxvat('12')
211  ->setGender(0);
212  $createdCustomer = $this->accountManagement->createAccount(
213  $customer,
214  '12345abcD'
215  );
216 
217  //Testing that addresses from order and the order itself are assigned
218  //to customer.
219  $order = $this->orderRepository->get($orderId);
220  $this->assertCount(2, $createdCustomer->getAddresses());
221  $this->assertNotNull($createdCustomer->getDefaultBilling());
222  $this->assertNotNull($createdCustomer->getDefaultShipping());
223  foreach ($createdCustomer->getAddresses() as $address) {
224  $this->assertTrue(
225  $address->isDefaultBilling() || $address->isDefaultShipping()
226  );
227  if ($address->isDefaultBilling()) {
228  $this->compareAddresses($order->getBillingAddress(), $address);
229  } elseif ($address->isDefaultShipping()) {
230  $this->compareAddresses($order->getShippingAddress(), $address);
231  }
232  }
233 
234  $this->assertEquals($order->getCustomerId(), $createdCustomer->getId());
235  }
236 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$customer
Definition: customers.php:11
$order
Definition: order.php:55
$address
Definition: customer.php:38