Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BeforeAddressSaveObserverTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Customer\Helper\Address as HelperAddress;
14 
15 class BeforeAddressSaveObserverTest extends \PHPUnit\Framework\TestCase
16 {
20  protected $model;
21 
25  protected $registry;
26 
30  protected $customerMock;
31 
35  protected $helperAddress;
36 
37  protected function setUp()
38  {
39  $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
40  ->disableOriginalConstructor()
41  ->getMock();
42 
43  $this->helperAddress = $this->getMockBuilder(\Magento\Customer\Helper\Address::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46 
47  $this->model = new BeforeAddressSaveObserver(
48  $this->helperAddress,
49  $this->registry
50  );
51  }
52 
54  {
55  $customerAddressId = 1;
56 
57  $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $address->expects($this->exactly(2))
61  ->method('getId')
62  ->willReturn($customerAddressId);
63 
64  $observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
65  ->disableOriginalConstructor()
66  ->setMethods([
67  'getCustomerAddress',
68  ])
69  ->getMock();
70  $observer->expects($this->once())
71  ->method('getCustomerAddress')
72  ->willReturn($address);
73 
74  $this->registry->expects($this->once())
75  ->method('registry')
77  ->willReturn(true);
78  $this->registry->expects($this->once())
79  ->method('unregister')
81  ->willReturnSelf();
82  $this->registry->expects($this->once())
83  ->method('register')
85  ->willReturnSelf();
86 
87  $this->model->execute($observer);
88  }
89 
97  $configAddressType,
98  $isDefaultBilling,
99  $isDefaultShipping
100  ) {
101  $customerAddressId = null;
102 
103  $address = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
104  ->disableOriginalConstructor()
105  ->setMethods(['getId', 'getIsDefaultBilling', 'getIsDefaultShipping', 'setForceProcess'])
106  ->getMock();
107  $address->expects($this->once())
108  ->method('getId')
109  ->willReturn($customerAddressId);
110  $address->expects($this->any())
111  ->method('getIsDefaultBilling')
112  ->willReturn($isDefaultBilling);
113  $address->expects($this->any())
114  ->method('getIsDefaultShipping')
115  ->willReturn($isDefaultShipping);
116  $address->expects($this->any())
117  ->method('setForceProcess')
118  ->with(true)
119  ->willReturnSelf();
120 
121  $observer = $this->getMockBuilder(\Magento\Framework\Event\Observer::class)
122  ->disableOriginalConstructor()
123  ->setMethods([
124  'getCustomerAddress',
125  ])
126  ->getMock();
127  $observer->expects($this->once())
128  ->method('getCustomerAddress')
129  ->willReturn($address);
130 
131  $this->helperAddress->expects($this->once())
132  ->method('getTaxCalculationAddressType')
133  ->willReturn($configAddressType);
134 
135  $this->registry->expects($this->once())
136  ->method('registry')
138  ->willReturn(true);
139  $this->registry->expects($this->once())
140  ->method('unregister')
142  ->willReturnSelf();
143  $this->registry->expects($this->any())
144  ->method('register')
145  ->willReturnMap([
146  [BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS, $customerAddressId, false, $this->registry],
147  [BeforeAddressSaveObserver::VIV_CURRENTLY_SAVED_ADDRESS, 'new_address', false, $this->registry],
148  ]);
149 
150  $this->model->execute($observer);
151  }
152 
157  {
158  return [
159  [
160  'TaxCalculationAddressType' => AbstractAddress::TYPE_BILLING,
161  'isDefaultBilling' => true,
162  'isDefaultShipping' => false,
163  ],
164  [
165  'TaxCalculationAddressType' => AbstractAddress::TYPE_SHIPPING,
166  'isDefaultBilling' => false,
167  'isDefaultShipping' => true,
168  ],
169  ];
170  }
171 }
$address
Definition: customer.php:38
testBeforeAddressSaveWithoutCustomerAddressId( $configAddressType, $isDefaultBilling, $isDefaultShipping)