Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomerManagementTest.php
Go to the documentation of this file.
1 <?php
7 
12 class CustomerManagementTest extends \PHPUnit\Framework\TestCase
13 {
18 
23 
28 
33 
37  protected $quoteMock;
38 
42  protected $quoteAddressMock;
43 
47  protected $customerMock;
48 
53 
57  private $validatorFactoryMock;
58 
62  private $addressFactoryMock;
63 
64  protected function setUp()
65  {
66  $this->customerRepositoryMock = $this->getMockForAbstractClass(
67  \Magento\Customer\Api\CustomerRepositoryInterface::class,
68  [],
69  '',
70  false,
71  true,
72  true,
73  ['getById']
74  );
75  $this->customerAddressRepositoryMock = $this->getMockForAbstractClass(
76  \Magento\Customer\Api\AddressRepositoryInterface::class,
77  [],
78  '',
79  false,
80  true,
81  true,
82  ['getById']
83  );
84  $this->accountManagementMock = $this->getMockForAbstractClass(
85  \Magento\Customer\Api\AccountManagementInterface::class,
86  [],
87  '',
88  false,
89  true,
90  true,
91  []
92  );
93  $this->quoteMock = $this->createPartialMock(
94  \Magento\Quote\Model\Quote::class,
95  ['getId', 'getCustomer', 'getBillingAddress', 'getShippingAddress', 'setCustomer', 'getPasswordHash']
96  );
97  $this->quoteAddressMock = $this->createMock(\Magento\Quote\Model\Quote\Address::class);
98  $this->customerMock = $this->getMockForAbstractClass(
99  \Magento\Customer\Api\Data\CustomerInterface::class,
100  [],
101  '',
102  false,
103  true,
104  true,
105  ['getId', 'getDefaultBilling']
106  );
107  $this->customerAddressMock = $this->getMockForAbstractClass(
108  \Magento\Customer\Api\Data\AddressInterface::class,
109  [],
110  '',
111  false,
112  true,
113  true,
114  []
115  );
116  $this->addressFactoryMock = $this->getMockBuilder(\Magento\Customer\Model\AddressFactory::class)
117  ->disableOriginalConstructor()
118  ->setMethods(['create'])
119  ->getMock();
120  $this->validatorFactoryMock = $this->getMockBuilder(\Magento\Framework\Validator\Factory::class)
121  ->disableOriginalConstructor()
122  ->getMock();
123  $this->customerManagement = new \Magento\Quote\Model\CustomerManagement(
124  $this->customerRepositoryMock,
125  $this->customerAddressRepositoryMock,
126  $this->accountManagementMock,
127  $this->validatorFactoryMock,
128  $this->addressFactoryMock
129  );
130  }
131 
132  public function testPopulateCustomerInfo()
133  {
134  $this->quoteMock->expects($this->once())
135  ->method('getCustomer')
136  ->willReturn($this->customerMock);
137  $this->customerMock->expects($this->atLeastOnce())
138  ->method('getId')
139  ->willReturn(null);
140  $this->customerMock->expects($this->atLeastOnce())
141  ->method('getDefaultBilling')
142  ->willReturn(100500);
143  $this->quoteMock->expects($this->atLeastOnce())
144  ->method('getBillingAddress')
145  ->willReturn($this->quoteAddressMock);
146  $this->quoteMock->expects($this->atLeastOnce())
147  ->method('getShippingAddress')
148  ->willReturn($this->quoteAddressMock);
149  $this->quoteMock->expects($this->atLeastOnce())
150  ->method('setCustomer')
151  ->with($this->customerMock)
152  ->willReturnSelf();
153  $this->quoteMock->expects($this->once())
154  ->method('getPasswordHash')
155  ->willReturn('password hash');
156  $this->quoteAddressMock->expects($this->atLeastOnce())
157  ->method('getId')
158  ->willReturn(null);
159  $this->customerAddressRepositoryMock->expects($this->atLeastOnce())
160  ->method('getById')
161  ->with(100500)
162  ->willReturn($this->customerAddressMock);
163  $this->quoteAddressMock->expects($this->atLeastOnce())
164  ->method('importCustomerAddressData')
165  ->willReturnSelf();
166  $this->accountManagementMock->expects($this->once())
167  ->method('createAccountWithPasswordHash')
168  ->with($this->customerMock, 'password hash')
169  ->willReturn($this->customerMock);
170  $this->customerManagement->populateCustomerInfo($this->quoteMock);
171  }
172 
174  {
175  $this->quoteMock->expects($this->once())
176  ->method('getCustomer')
177  ->willReturn($this->customerMock);
178  $this->customerMock->expects($this->atLeastOnce())
179  ->method('getId')
180  ->willReturn(1);
181  $this->customerMock->expects($this->atLeastOnce())
182  ->method('getDefaultBilling')
183  ->willReturn(100500);
184  $this->quoteMock->expects($this->atLeastOnce())
185  ->method('getBillingAddress')
186  ->willReturn($this->quoteAddressMock);
187  $this->quoteMock->expects($this->atLeastOnce())
188  ->method('getShippingAddress')
189  ->willReturn($this->quoteAddressMock);
190  $this->quoteAddressMock->expects($this->atLeastOnce())
191  ->method('getId')
192  ->willReturn(null);
193  $this->customerAddressRepositoryMock->expects($this->atLeastOnce())
194  ->method('getById')
195  ->with(100500)
196  ->willReturn($this->customerAddressMock);
197  $this->quoteAddressMock->expects($this->atLeastOnce())
198  ->method('importCustomerAddressData')
199  ->willReturnSelf();
200  $this->customerManagement->populateCustomerInfo($this->quoteMock);
201  }
202 
203  public function testValidateAddresses()
204  {
205  $this->quoteMock
206  ->expects($this->exactly(2))
207  ->method('getBillingAddress')
208  ->willReturn($this->quoteAddressMock);
209  $this->quoteMock
210  ->expects($this->exactly(2))
211  ->method('getShippingAddress')
212  ->willReturn($this->quoteAddressMock);
213  $this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(2);
214  $this->customerAddressRepositoryMock
215  ->expects($this->any())
216  ->method('getById')
217  ->willReturn($this->customerAddressMock);
218  $validatorMock = $this->getMockBuilder(\Magento\Framework\Validator::class)
219  ->disableOriginalConstructor()
220  ->getMock();
221  $addressMock = $this->getMockBuilder(\Magento\Customer\Model\Address::class)
222  ->disableOriginalConstructor()
223  ->getMock();
224  $this->addressFactoryMock->expects($this->exactly(2))->method('create')->willReturn($addressMock);
225  $this->validatorFactoryMock
226  ->expects($this->exactly(2))
227  ->method('createValidator')
228  ->with('customer_address', 'save', null)
229  ->willReturn($validatorMock);
230  $validatorMock->expects($this->exactly(2))->method('isValid')->with($addressMock)->willReturn(true);
231  $this->customerManagement->validateAddresses($this->quoteMock);
232  }
233 
235  {
236  $this->quoteMock
237  ->expects($this->once())
238  ->method('getBillingAddress')
239  ->willReturn($this->quoteAddressMock);
240  $this->quoteMock
241  ->expects($this->once())
242  ->method('getShippingAddress')
243  ->willReturn($this->quoteAddressMock);
244  $this->quoteAddressMock->expects($this->any())->method('getCustomerAddressId')->willReturn(null);
245  $this->validatorFactoryMock
246  ->expects($this->never())
247  ->method('createValidator')
248  ->with('customer_address', 'save', null);
249  $this->customerManagement->validateAddresses($this->quoteMock);
250  }
251 }