Skip to content

Commit

Permalink
#19230: Refactoring and unit test update.
Browse files Browse the repository at this point in the history
  • Loading branch information
p-bystritsky committed Aug 7, 2019
1 parent ae76c57 commit d8add8a
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 17 deletions.
18 changes: 13 additions & 5 deletions app/code/Magento/Sales/Model/Order/CustomerAssignment.php
Expand Up @@ -7,10 +7,10 @@

namespace Magento\Sales\Model\Order;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

/**
* Assign customer to order.
Expand Down Expand Up @@ -47,10 +47,18 @@ public function __construct(
* @param OrderInterface $order
* @param CustomerInterface $customer
*/
public function execute(OrderInterface $order, CustomerInterface $customer)/*: void*/
public function execute(OrderInterface $order, CustomerInterface $customer): void
{
$order->setCustomerId($customer->getId());
$order->setCustomerIsGuest(false);
$order->setCustomerId($customer->getId())
->setCustomerIsGuest(false)
->setCustomerEmail($customer->getEmail())
->setCustomerFirstname($customer->getFirstname())
->setCustomerLastname($customer->getLastname())
->setCustomerMiddlename($customer->getMiddlename())
->setCustomerPrefix($customer->getPrefix())
->setCustomerSuffix($customer->getSuffix())
->setCustomerGroupId($customer->getGroupId());

$this->orderRepository->save($order);

$this->eventManager->dispatch(
Expand Down
155 changes: 155 additions & 0 deletions app/code/Magento/Sales/Test/Unit/Model/Order/CustomerAssigmentTest.php
@@ -0,0 +1,155 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Sales\Test\Unit\Model\Order;

use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\CustomerAssignment;

/**
* Test for Magento\Sales\Model\Order\CustomerAssignment class.
*/
class CustomerAssigmentTest extends \PHPUnit\Framework\TestCase
{
/**
* @var CustomerAssignment
*/
private $customerAssignment;

/**
* @var OrderInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $orderMock;

/**
* @var CustomerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $customerMock;

/**
* @var OrderRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $orderRepositoryMock;

/**
* @var ManagerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $eventManagerMock;

/**
* Tests 'execute' method.
*
* @dataProvider executeDataProvider
* @param array $data
*/
public function testExecute(array $data): void
{
$this->configureOrderMock($data);
$this->configureCustomerMock($data);
$this->orderRepositoryMock->expects($this->once())->method('save')->with($this->orderMock);
$this->eventManagerMock->expects($this->once())->method('dispatch')->with(
'sales_order_customer_assign_after',
[
'order' => $this->orderMock,
'customer' => $this->customerMock
]
);

$this->customerAssignment->execute($this->orderMock, $this->customerMock);
}

/**
*
* Data provider for testExecute.
* @return array
*/
public function executeDataProvider(): array
{
return [
[
[
'customerId' => 1,
'customerIsGuest' => false,
'customerEmail' => 'customerEmail',
'customerFirstname' => 'customerFirstname',
'customerLastname' => 'customerLastname',
'customerMiddlename' => 'customerMiddlename',
'customerPrefix' => 'customerPrefix',
'customerSuffix' => 'customerSuffix',
'customerGroupId' => 'customerGroupId',
],
],
];
}

/**
* @return void
*/
protected function setUp()
{
$objectManager = new ObjectManager($this);
$this->orderMock = $this->createMock(OrderInterface::class);
$this->customerMock = $this->createMock(CustomerInterface::class);
$this->orderRepositoryMock = $this->createMock(OrderRepositoryInterface::class);
$this->eventManagerMock = $this->createMock(ManagerInterface::class);
$this->customerAssignment = $objectManager->getObject(
CustomerAssignment::class,
[
'eventManager' => $this->eventManagerMock,
'orderRepository' => $this->orderRepositoryMock
]
);
}

/**
* Set up order mock.
*
* @param array $data
*/
private function configureOrderMock(array $data): void
{
$this->orderMock->expects($this->once())->method('setCustomerId')->with($data['customerId'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerIsGuest')->with($data['customerIsGuest'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerEmail')->with($data['customerEmail'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerFirstname')->with($data['customerFirstname'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerLastname')->with($data['customerLastname'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerMiddlename')->with($data['customerMiddlename'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerPrefix')->with($data['customerPrefix'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerSuffix')->with($data['customerSuffix'])
->willReturn($this->orderMock);
$this->orderMock->expects($this->once())->method('setCustomerGroupId')->with($data['customerGroupId'])
->willReturn($this->orderMock);
}

/**
* Set up customer mock.
*
* @param array $data
*/
private function configureCustomerMock(array $data): void
{
$this->customerMock->expects($this->once())->method('getId')->willReturn($data['customerId']);
$this->customerMock->expects($this->once())->method('getEmail')->willReturn($data['customerEmail']);
$this->customerMock->expects($this->once())->method('getFirstname')->willReturn($data['customerFirstname']);
$this->customerMock->expects($this->once())->method('getLastname')->willReturn($data['customerLastname']);
$this->customerMock->expects($this->once())->method('getMiddlename')->willReturn($data['customerMiddlename']);
$this->customerMock->expects($this->once())->method('getPrefix')->willReturn($data['customerPrefix']);
$this->customerMock->expects($this->once())->method('getSuffix')->willReturn($data['customerSuffix']);
$this->customerMock->expects($this->once())->method('getGroupId')->willReturn($data['customerGroupId']);
}
}
Expand Up @@ -51,10 +51,11 @@ protected function setUp()
* Test assigning order to customer after issuing guest order
*
* @dataProvider getCustomerIds
* @param null|int $orderCustomerId
* @param null|int $customerId
* @return void
*/
public function testAssignOrderToCustomerAfterGuestOrder($customerId)
public function testAssignOrderToCustomerAfterGuestOrder($orderCustomerId, $customerId)
{
$orderId = 1;
/** @var Observer|PHPUnit_Framework_MockObject_MockObject $observerMock */
Expand All @@ -71,15 +72,18 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId)
->getMockForAbstractClass();
$observerMock->expects($this->once())->method('getEvent')->willReturn($eventMock);
$eventMock->expects($this->any())->method('getData')
->willReturnMap([
['delegate_data', null, ['__sales_assign_order_id' => $orderId]],
['customer_data_object', null, $customerMock]
]);
$orderMock->expects($this->once())->method('getCustomerId')->willReturn($customerId);
->willReturnMap(
[
['delegate_data', null, ['__sales_assign_order_id' => $orderId]],
['customer_data_object', null, $customerMock]
]
);
$orderMock->expects($this->once())->method('getCustomerId')->willReturn($orderCustomerId);
$this->orderRepositoryMock->expects($this->once())->method('get')->with($orderId)
->willReturn($orderMock);

if ($customerId) {
if (!$orderCustomerId) {
$customerMock->expects($this->once())->method('getId')->willReturn($customerId);
$this->assignmentMock->expects($this->once())->method('execute')->with($orderMock, $customerMock);
$this->sut->execute($observerMock);
return;
Expand All @@ -96,6 +100,9 @@ public function testAssignOrderToCustomerAfterGuestOrder($customerId)
*/
public function getCustomerIds()
{
return [[null, 1]];
return [
[null, 1],
[1, 1],
];
}
}
Expand Up @@ -6,14 +6,14 @@

namespace Magento\SalesRule\Model\Observer;

use Magento\Customer\Model\Data\Customer;
use Magento\Customer\Model\GroupManagement;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Sales\Model\Order;
use Magento\Customer\Model\GroupManagement;
use Magento\SalesRule\Api\CouponRepositoryInterface;
use Magento\SalesRule\Model\Coupon;
use Magento\SalesRule\Model\Rule;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Model\Data\Customer;
use Magento\TestFramework\Helper\Bootstrap;

/**
Expand Down Expand Up @@ -252,9 +252,8 @@ private function makeOrderWithCouponAsGuest(Coupon $coupon) : Order

/**
* @param Order $order
* @return Redirect
*/
private function delegateOrderToBeAssigned(Order $order): Redirect
private function delegateOrderToBeAssigned(Order $order)
{
$this->delegateCustomerService->delegateNew($order->getId());
}
Expand Down

0 comments on commit d8add8a

Please sign in to comment.