123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\Sales\Api;
- use Magento\Customer\Api\AccountManagementInterface;
- use Magento\Customer\Api\Data\AddressInterface;
- use Magento\Customer\Api\Data\CustomerInterface;
- use Magento\Customer\Api\Data\CustomerInterfaceFactory;
- use Magento\Sales\Api\Data\OrderAddressInterface;
- use Magento\Sales\Model\Order;
- use Magento\Sales\Model\OrderFactory;
- use Magento\TestFramework\Helper\Bootstrap;
- use PHPUnit\Framework\TestCase;
- /**
- * Test for Magento\Sales\Api\OrderCustomerDelegateInterface class.
- *
- * @magentoAppIsolation enabled
- */
- class OrderCustomerDelegateInterfaceTest extends TestCase
- {
- /**
- * @var OrderCustomerDelegateInterface
- */
- private $delegate;
- /**
- * @var OrderRepositoryInterface
- */
- private $orderRepository;
- /**
- * @var CustomerInterfaceFactory
- */
- private $customerFactory;
- /**
- * @var AccountManagementInterface
- */
- private $accountManagement;
- /**
- * @var OrderFactory
- */
- private $orderFactory;
- /**
- * {@inheritdoc}
- */
- protected function setUp()
- {
- $this->delegate = Bootstrap::getObjectManager()->get(
- OrderCustomerDelegateInterface::class
- );
- $this->orderRepository = Bootstrap::getObjectManager()->get(
- OrderRepositoryInterface::class
- );
- $this->customerFactory = Bootstrap::getObjectManager()->get(
- CustomerInterfaceFactory::class
- );
- $this->accountManagement = Bootstrap::getObjectManager()->get(
- AccountManagementInterface::class
- );
- $this->orderFactory = Bootstrap::getObjectManager()->get(
- OrderFactory::class
- );
- }
- /**
- * @param OrderAddressInterface $orderAddress
- * @param AddressInterface $address
- *
- * @return void
- */
- private function compareAddresses(
- OrderAddressInterface $orderAddress,
- AddressInterface $address
- ): void {
- $this->assertEquals(
- $orderAddress->getFirstname(),
- $address->getFirstname()
- );
- $this->assertEquals(
- $orderAddress->getLastname(),
- $address->getLastname()
- );
- $this->assertEquals(
- $orderAddress->getCompany(),
- $address->getCompany()
- );
- $this->assertEquals(
- $orderAddress->getStreet(),
- $address->getStreet()
- );
- $this->assertEquals(
- $orderAddress->getCity(),
- $address->getCity()
- );
- if (!$address->getRegionId()) {
- $this->assertEmpty($address->getRegionId());
- } else {
- $this->assertEquals(
- $orderAddress->getRegionId(),
- $address->getRegionId()
- );
- }
- $this->assertEquals(
- $orderAddress->getPostcode(),
- $address->getPostcode()
- );
- $this->assertEquals(
- $orderAddress->getCountryId(),
- $address->getCountryId()
- );
- $this->assertEquals(
- $orderAddress->getTelephone(),
- $address->getTelephone()
- );
- }
- /**
- * @magentoDbIsolation enabled
- * @magentoAppIsolation enabled
- * @magentoDataFixture Magento/Sales/_files/order.php
- * @return void
- */
- public function testDelegateNew(): void
- {
- $orderAutoincrementId = '100000001';
- /** @var Order $orderModel */
- $orderModel = $this->orderFactory->create();
- $orderModel->loadByIncrementId($orderAutoincrementId);
- $orderId = (int)$orderModel->getId();
- unset($orderModel);
- $this->delegate->delegateNew($orderId);
- //Saving new customer with prepared data from order.
- /** @var CustomerInterface $customer */
- $customer = $this->customerFactory->create();
- $customer->setWebsiteId(1)
- ->setEmail('customer_order_delegate@example.com')
- ->setGroupId(1)
- ->setStoreId(1)
- ->setPrefix('Mr.')
- ->setFirstname('John')
- ->setMiddlename('A')
- ->setLastname('Smith')
- ->setSuffix('Esq.')
- ->setTaxvat('12')
- ->setGender(0);
- $createdCustomer = $this->accountManagement->createAccount(
- $customer,
- '12345abcD'
- );
- //Testing that addresses from order and the order itself are assigned
- //to customer.
- $order = $this->orderRepository->get($orderId);
- $this->assertCount(1, $createdCustomer->getAddresses());
- $this->assertNotNull($createdCustomer->getDefaultBilling());
- $this->assertNotNull($createdCustomer->getDefaultShipping());
- foreach ($createdCustomer->getAddresses() as $address) {
- $this->assertTrue(
- $address->isDefaultBilling() || $address->isDefaultShipping()
- );
- if ($address->isDefaultBilling()) {
- $this->compareAddresses($order->getBillingAddress(), $address);
- } elseif ($address->isDefaultShipping()) {
- $this->compareAddresses($order->getShippingAddress(), $address);
- }
- }
- $this->assertEquals($order->getCustomerId(), $createdCustomer->getId());
- }
- /**
- * @magentoDbIsolation enabled
- * @magentoAppIsolation enabled
- * @magentoDataFixture Magento/Sales/_files/order_different_addresses.php
- * @return void
- */
- public function testDelegateNewDifferentAddresses(): void
- {
- $orderAutoincrementId = '100000001';
- /** @var Order $orderModel */
- $orderModel = $this->orderFactory->create();
- $orderModel->loadByIncrementId($orderAutoincrementId);
- $orderId = (int)$orderModel->getId();
- unset($orderModel);
- $this->delegate->delegateNew($orderId);
- //Saving new customer with prepared data from order.
- /** @var CustomerInterface $customer */
- $customer = $this->customerFactory->create();
- $customer->setWebsiteId(1)
- ->setEmail('customer_order_delegate@example.com')
- ->setGroupId(1)
- ->setStoreId(1)
- ->setPrefix('Mr.')
- ->setFirstname('John')
- ->setMiddlename('A')
- ->setLastname('Smith')
- ->setSuffix('Esq.')
- ->setTaxvat('12')
- ->setGender(0);
- $createdCustomer = $this->accountManagement->createAccount(
- $customer,
- '12345abcD'
- );
- //Testing that addresses from order and the order itself are assigned
- //to customer.
- $order = $this->orderRepository->get($orderId);
- $this->assertCount(2, $createdCustomer->getAddresses());
- $this->assertNotNull($createdCustomer->getDefaultBilling());
- $this->assertNotNull($createdCustomer->getDefaultShipping());
- foreach ($createdCustomer->getAddresses() as $address) {
- $this->assertTrue(
- $address->isDefaultBilling() || $address->isDefaultShipping()
- );
- if ($address->isDefaultBilling()) {
- $this->compareAddresses($order->getBillingAddress(), $address);
- } elseif ($address->isDefaultShipping()) {
- $this->compareAddresses($order->getShippingAddress(), $address);
- }
- }
- $this->assertEquals($order->getCustomerId(), $createdCustomer->getId());
- }
- }
|