|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Sales\Test\Unit\Model\Order; |
| 9 | + |
| 10 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 11 | +use Magento\Framework\Event\ManagerInterface; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 13 | +use Magento\Sales\Api\Data\OrderInterface; |
| 14 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 15 | +use Magento\Sales\Model\Order\CustomerAssignment; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test for Magento\Sales\Model\Order\CustomerAssignment class. |
| 19 | + */ |
| 20 | +class CustomerAssigmentTest extends \PHPUnit\Framework\TestCase |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var CustomerAssignment |
| 24 | + */ |
| 25 | + private $customerAssignment; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var OrderInterface|\PHPUnit\Framework\MockObject\MockObject |
| 29 | + */ |
| 30 | + private $orderMock; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var CustomerInterface|\PHPUnit\Framework\MockObject\MockObject |
| 34 | + */ |
| 35 | + private $customerMock; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var OrderRepositoryInterface|\PHPUnit\Framework\MockObject\MockObject |
| 39 | + */ |
| 40 | + private $orderRepositoryMock; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ManagerInterface|\PHPUnit\Framework\MockObject\MockObject |
| 44 | + */ |
| 45 | + private $eventManagerMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * Tests 'execute' method. |
| 49 | + * |
| 50 | + * @dataProvider executeDataProvider |
| 51 | + * @param array $data |
| 52 | + */ |
| 53 | + public function testExecute(array $data): void |
| 54 | + { |
| 55 | + $this->configureOrderMock($data); |
| 56 | + $this->configureCustomerMock($data); |
| 57 | + $this->orderRepositoryMock->expects($this->once())->method('save')->with($this->orderMock); |
| 58 | + $this->eventManagerMock->expects($this->once())->method('dispatch')->with( |
| 59 | + 'sales_order_customer_assign_after', |
| 60 | + [ |
| 61 | + 'order' => $this->orderMock, |
| 62 | + 'customer' => $this->customerMock |
| 63 | + ] |
| 64 | + ); |
| 65 | + |
| 66 | + $this->customerAssignment->execute($this->orderMock, $this->customerMock); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * |
| 71 | + * Data provider for testExecute. |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + public function executeDataProvider(): array |
| 75 | + { |
| 76 | + return [ |
| 77 | + [ |
| 78 | + [ |
| 79 | + 'customerId' => 1, |
| 80 | + 'customerIsGuest' => false, |
| 81 | + 'customerEmail' => 'customerEmail', |
| 82 | + 'customerFirstname' => 'customerFirstname', |
| 83 | + 'customerLastname' => 'customerLastname', |
| 84 | + 'customerMiddlename' => 'customerMiddlename', |
| 85 | + 'customerPrefix' => 'customerPrefix', |
| 86 | + 'customerSuffix' => 'customerSuffix', |
| 87 | + 'customerGroupId' => 'customerGroupId', |
| 88 | + ], |
| 89 | + ], |
| 90 | + ]; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + protected function setUp() |
| 97 | + { |
| 98 | + $objectManager = new ObjectManager($this); |
| 99 | + $this->orderMock = $this->createMock(OrderInterface::class); |
| 100 | + $this->customerMock = $this->createMock(CustomerInterface::class); |
| 101 | + $this->orderRepositoryMock = $this->createMock(OrderRepositoryInterface::class); |
| 102 | + $this->eventManagerMock = $this->createMock(ManagerInterface::class); |
| 103 | + $this->customerAssignment = $objectManager->getObject( |
| 104 | + CustomerAssignment::class, |
| 105 | + [ |
| 106 | + 'eventManager' => $this->eventManagerMock, |
| 107 | + 'orderRepository' => $this->orderRepositoryMock |
| 108 | + ] |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Set up order mock. |
| 114 | + * |
| 115 | + * @param array $data |
| 116 | + */ |
| 117 | + private function configureOrderMock(array $data): void |
| 118 | + { |
| 119 | + $this->orderMock->expects($this->once())->method('setCustomerId')->with($data['customerId']) |
| 120 | + ->willReturn($this->orderMock); |
| 121 | + $this->orderMock->expects($this->once())->method('setCustomerIsGuest')->with($data['customerIsGuest']) |
| 122 | + ->willReturn($this->orderMock); |
| 123 | + $this->orderMock->expects($this->once())->method('setCustomerEmail')->with($data['customerEmail']) |
| 124 | + ->willReturn($this->orderMock); |
| 125 | + $this->orderMock->expects($this->once())->method('setCustomerFirstname')->with($data['customerFirstname']) |
| 126 | + ->willReturn($this->orderMock); |
| 127 | + $this->orderMock->expects($this->once())->method('setCustomerLastname')->with($data['customerLastname']) |
| 128 | + ->willReturn($this->orderMock); |
| 129 | + $this->orderMock->expects($this->once())->method('setCustomerMiddlename')->with($data['customerMiddlename']) |
| 130 | + ->willReturn($this->orderMock); |
| 131 | + $this->orderMock->expects($this->once())->method('setCustomerPrefix')->with($data['customerPrefix']) |
| 132 | + ->willReturn($this->orderMock); |
| 133 | + $this->orderMock->expects($this->once())->method('setCustomerSuffix')->with($data['customerSuffix']) |
| 134 | + ->willReturn($this->orderMock); |
| 135 | + $this->orderMock->expects($this->once())->method('setCustomerGroupId')->with($data['customerGroupId']) |
| 136 | + ->willReturn($this->orderMock); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Set up customer mock. |
| 141 | + * |
| 142 | + * @param array $data |
| 143 | + */ |
| 144 | + private function configureCustomerMock(array $data): void |
| 145 | + { |
| 146 | + $this->customerMock->expects($this->once())->method('getId')->willReturn($data['customerId']); |
| 147 | + $this->customerMock->expects($this->once())->method('getEmail')->willReturn($data['customerEmail']); |
| 148 | + $this->customerMock->expects($this->once())->method('getFirstname')->willReturn($data['customerFirstname']); |
| 149 | + $this->customerMock->expects($this->once())->method('getLastname')->willReturn($data['customerLastname']); |
| 150 | + $this->customerMock->expects($this->once())->method('getMiddlename')->willReturn($data['customerMiddlename']); |
| 151 | + $this->customerMock->expects($this->once())->method('getPrefix')->willReturn($data['customerPrefix']); |
| 152 | + $this->customerMock->expects($this->once())->method('getSuffix')->willReturn($data['customerSuffix']); |
| 153 | + $this->customerMock->expects($this->once())->method('getGroupId')->willReturn($data['customerGroupId']); |
| 154 | + } |
| 155 | +} |
0 commit comments