Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddressUpdateTest.php
Go to the documentation of this file.
1 <?php
8 
9 class AddressUpdateTest extends \PHPUnit\Framework\TestCase
10 {
14  private $model;
15 
19  private $gripPoolMock;
20 
24  private $attributeMock;
25 
26  protected function setUp()
27  {
28  $this->gripPoolMock = $this->createMock(\Magento\Sales\Model\ResourceModel\GridPool::class);
29  $this->attributeMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Attribute::class);
30  $this->model = new \Magento\Sales\Model\Order\Invoice\Plugin\AddressUpdate(
31  $this->gripPoolMock,
32  $this->attributeMock
33  );
34  }
35 
36  public function testAfterProcess()
37  {
38  $billingId = 100;
39  $shippingId = 200;
40  $orderId = 50;
41 
42  $orderMock = $this->createPartialMock(
43  \Magento\Sales\Model\Order::class,
44  ['hasInvoices', 'getBillingAddress', 'getShippingAddress', 'getInvoiceCollection', 'getId']
45  );
46 
47  $shippingMock = $this->createMock(\Magento\Sales\Model\Order\Address::class);
48  $shippingMock->expects($this->once())->method('getId')->willReturn($shippingId);
49 
50  $billingMock = $this->createMock(\Magento\Sales\Model\Order\Address::class);
51  $billingMock->expects($this->once())->method('getId')->willReturn($billingId);
52 
53  $invoiceCollectionMock = $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Invoice\Collection::class);
54  $invoiceMock = $this->createMock(\Magento\Sales\Model\Order\Invoice::class);
55  $invoiceCollectionMock->expects($this->once())->method('getItems')->willReturn([$invoiceMock]);
56 
57  $orderMock->expects($this->once())->method('hasInvoices')->willReturn(true);
58  $orderMock->expects($this->once())->method('getBillingAddress')->willReturn($billingMock);
59  $orderMock->expects($this->once())->method('getShippingAddress')->willReturn($shippingMock);
60  $orderMock->expects($this->once())->method('getInvoiceCollection')->willReturn($invoiceCollectionMock);
61  $orderMock->expects($this->once())->method('getId')->willReturn($orderId);
62 
63  $invoiceMock->expects($this->once())->method('getBillingAddressId')->willReturn(null);
64  $invoiceMock->expects($this->once())->method('getShippingAddressId')->willReturn(null);
65  $invoiceMock->expects($this->once())->method('setShippingAddressId')->with($shippingId)->willReturnSelf();
66  $invoiceMock->expects($this->once())->method('setBillingAddressId')->with($billingId)->willReturnSelf();
67 
68  $this->attributeMock->expects($this->once())
69  ->method('saveAttribute')
70  ->with($invoiceMock, ['billing_address_id', 'shipping_address_id'])
71  ->willReturnSelf();
72 
73  $this->gripPoolMock->expects($this->once())->method('refreshByOrderId')->with($orderId)->willReturnSelf();
74 
75  $this->model->afterProcess(
76  $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Handler\Address::class),
77  $this->createMock(\Magento\Sales\Model\ResourceModel\Order\Handler\Address::class),
78  $orderMock
79  );
80  }
81 }