Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VatValidatorTest.php
Go to the documentation of this file.
1 <?php
7 
8 class VatValidatorTest extends \PHPUnit\Framework\TestCase
9 {
13  protected $model;
14 
19 
23  protected $customerVatMock;
24 
28  protected $quoteAddressMock;
29 
33  protected $storeMock;
34 
38  protected $testData;
39 
43  protected $validationResult;
44 
45  protected function setUp()
46  {
47  $this->customerAddressMock = $this->createMock(\Magento\Customer\Helper\Address::class);
48  $this->customerVatMock = $this->createMock(\Magento\Customer\Model\Vat::class);
49  $this->customerVatMock->expects($this->any())
50  ->method('getMerchantCountryCode')
51  ->willReturn('merchantCountryCode');
52  $this->customerVatMock->expects($this->any())
53  ->method('getMerchantVatNumber')
54  ->willReturn('merchantVatNumber');
55 
56  $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
57 
58  $this->quoteAddressMock = $this->createPartialMock(\Magento\Quote\Model\Quote\Address::class, [
59  'getCountryId',
60  'getVatId',
61  'getValidatedCountryCode',
62  'getValidatedVatNumber',
63  'getVatIsValid',
64  'getVatRequestId',
65  'getVatRequestDate',
66  'getVatRequestSuccess',
67  'getAddressType',
68  'save',
69  '__wakeup'
70  ]);
71 
72  $this->testData = [
73  'is_valid' => true,
74  'request_identifier' => 'test_request_identifier',
75  'request_date' => 'test_request_date',
76  'request_success' => true,
77  ];
78 
79  $this->quoteAddressMock->expects(
80  $this->any()
81  )->method(
82  'getVatIsValid'
83  )->will(
84  $this->returnValue($this->testData['is_valid'])
85  );
86  $this->quoteAddressMock->expects(
87  $this->any()
88  )->method(
89  'getVatRequestId'
90  )->will(
91  $this->returnValue($this->testData['request_identifier'])
92  );
93  $this->quoteAddressMock->expects(
94  $this->any()
95  )->method(
96  'getVatRequestDate'
97  )->will(
98  $this->returnValue($this->testData['request_date'])
99  );
100  $this->quoteAddressMock->expects(
101  $this->any()
102  )->method(
103  'getVatRequestSuccess'
104  )->will(
105  $this->returnValue($this->testData['request_success'])
106  );
107  $this->quoteAddressMock->expects($this->any())->method('getCountryId')->will($this->returnValue('en'));
108  $this->quoteAddressMock->expects($this->any())->method('getVatId')->will($this->returnValue('testVatID'));
109 
110  $this->validationResult = new \Magento\Framework\DataObject($this->testData);
111 
112  $this->model = new \Magento\Quote\Observer\Frontend\Quote\Address\VatValidator(
113  $this->customerAddressMock,
114  $this->customerVatMock
115  );
116  }
117 
119  {
120  $this->customerVatMock->expects($this->never())->method('checkVatNumber');
121 
122  $this->customerAddressMock->expects(
123  $this->once()
124  )->method(
125  'hasValidateOnEachTransaction'
126  )->with(
127  $this->storeMock
128  )->will(
129  $this->returnValue(false)
130  );
131 
132  $this->quoteAddressMock->expects(
133  $this->any()
134  )->method(
135  'getValidatedCountryCode'
136  )->will(
137  $this->returnValue('en')
138  );
139 
140  $this->quoteAddressMock->expects(
141  $this->any()
142  )->method(
143  'getValidatedVatNumber'
144  )->will(
145  $this->returnValue('testVatID')
146  );
147 
148  $this->quoteAddressMock->expects($this->never())->method('save');
149 
150  $this->assertEquals(
151  $this->validationResult,
152  $this->model->validate($this->quoteAddressMock, $this->storeMock)
153  );
154  }
155 
157  {
158  $this->customerVatMock->expects(
159  $this->once()
160  )->method(
161  'checkVatNumber'
162  )->with(
163  'en',
164  'testVatID',
165  'merchantCountryCode',
166  'merchantVatNumber'
167  )->will(
168  $this->returnValue($this->validationResult)
169  );
170 
171  $this->customerAddressMock->expects(
172  $this->once()
173  )->method(
174  'hasValidateOnEachTransaction'
175  )->with(
176  $this->storeMock
177  )->will(
178  $this->returnValue(true)
179  );
180 
181  $this->quoteAddressMock->expects(
182  $this->any()
183  )->method(
184  'getValidatedCountryCode'
185  )->will(
186  $this->returnValue('en')
187  );
188 
189  $this->quoteAddressMock->expects(
190  $this->any()
191  )->method(
192  'getValidatedVatNumber'
193  )->will(
194  $this->returnValue('testVatID')
195  );
196 
197  $this->quoteAddressMock->expects($this->once())->method('save');
198 
199  $this->assertEquals(
200  $this->validationResult,
201  $this->model->validate($this->quoteAddressMock, $this->storeMock)
202  );
203  }
204 
206  {
207  $this->customerVatMock->expects(
208  $this->once()
209  )->method(
210  'checkVatNumber'
211  )->with(
212  'en',
213  'testVatID',
214  'merchantCountryCode',
215  'merchantVatNumber'
216  )->will(
217  $this->returnValue($this->validationResult)
218  );
219 
220  $this->customerAddressMock->expects(
221  $this->once()
222  )->method(
223  'hasValidateOnEachTransaction'
224  )->with(
225  $this->storeMock
226  )->will(
227  $this->returnValue(false)
228  );
229 
230  $this->quoteAddressMock->expects(
231  $this->any()
232  )->method(
233  'getValidatedCountryCode'
234  )->will(
235  $this->returnValue('someCountryCode')
236  );
237 
238  $this->quoteAddressMock->expects($this->any())->method('getVatId')->will($this->returnValue('testVatID'));
239 
240  $this->quoteAddressMock->expects($this->once())->method('save');
241 
242  $this->assertEquals(
243  $this->validationResult,
244  $this->model->validate($this->quoteAddressMock, $this->storeMock)
245  );
246  }
247 
249  {
250  $this->customerVatMock->expects(
251  $this->once()
252  )->method(
253  'checkVatNumber'
254  )->with(
255  'en',
256  'testVatID',
257  'merchantCountryCode',
258  'merchantVatNumber'
259  )->will(
260  $this->returnValue($this->validationResult)
261  );
262 
263  $this->customerAddressMock->expects(
264  $this->once()
265  )->method(
266  'hasValidateOnEachTransaction'
267  )->with(
268  $this->storeMock
269  )->will(
270  $this->returnValue(false)
271  );
272 
273  $this->quoteAddressMock->expects(
274  $this->any()
275  )->method(
276  'getValidatedCountryCode'
277  )->will(
278  $this->returnValue('en')
279  );
280 
281  $this->quoteAddressMock->expects($this->any())->method('getVatId')->will($this->returnValue('someVatID'));
282 
283  $this->quoteAddressMock->expects($this->once())->method('save');
284 
285  $this->assertEquals(
286  $this->validationResult,
287  $this->model->validate($this->quoteAddressMock, $this->storeMock)
288  );
289  }
290 
292  {
293  $this->customerAddressMock->expects(
294  $this->any()
295  )->method(
296  'isVatValidationEnabled'
297  )->will(
298  $this->returnValue(true)
299  );
300 
301  $this->customerAddressMock->expects(
302  $this->any()
303  )->method(
304  'getTaxCalculationAddressType'
305  )->will(
306  $this->returnValue(\Magento\Customer\Model\Address\AbstractAddress::TYPE_BILLING)
307  );
308 
309  $this->quoteAddressMock->expects(
310  $this->any()
311  )->method(
312  'getAddressType'
313  )->will(
314  $this->returnValue(\Magento\Customer\Model\Address\AbstractAddress::TYPE_SHIPPING)
315  );
316 
317  $result = $this->model->isEnabled($this->quoteAddressMock, $this->storeMock);
318  $this->assertFalse($result);
319  }
320 
322  {
323  $this->customerAddressMock->expects(
324  $this->any()
325  )->method(
326  'isVatValidationEnabled'
327  )->will(
328  $this->returnValue(true)
329  );
330  $result = $this->model->isEnabled($this->quoteAddressMock, $this->storeMock);
331  $this->assertTrue($result);
332  }
333 }