Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IntegrationServiceTest.php
Go to the documentation of this file.
1 <?php
10 
12 
13 class IntegrationServiceTest extends \PHPUnit\Framework\TestCase
14 {
16 
17  const VALUE_INTEGRATION_NAME = 'Integration Name';
18 
19  const VALUE_INTEGRATION_ANOTHER_NAME = 'Another Integration Name';
20 
22 
24 
25  const VALUE_INTEGRATION_ENDPOINT = 'http://magento.ll/endpoint';
26 
28 
30  private $_integrationFactory;
31 
33  private $_integrationMock;
34 
36  private $_emptyIntegrationMock;
37 
39  private $_service;
40 
42  private $_integrationData;
43 
44  protected function setUp()
45  {
46  $this->_integrationFactory = $this->getMockBuilder(\Magento\Integration\Model\IntegrationFactory::class)
47  ->disableOriginalConstructor()
48  ->setMethods(['create'])
49  ->getMock();
50  $this->_integrationMock = $this->getMockBuilder(
51  \Magento\Integration\Model\Integration::class
52  )->disableOriginalConstructor()->setMethods(
53  [
54  'getData',
55  'getId',
56  'getName',
57  'getEmail',
58  'getEndpoint',
59  'load',
60  'loadByName',
61  'save',
62  'delete',
63  '__wakeup',
64  ]
65  )->getMock();
66  $this->_integrationData = [
72  ];
73  $this->_integrationFactory->expects(
74  $this->any()
75  )->method(
76  'create'
77  )->will(
78  $this->returnValue($this->_integrationMock)
79  );
80 
81  $oauthConsumerHelper = $this->getMockBuilder(
82  \Magento\Integration\Api\OauthServiceInterface::class
83  )->disableOriginalConstructor()->getMock();
84  $oauthConsumer = $this->getMockBuilder(
85  \Magento\Integration\Model\Oauth\Consumer::class
86  )->disableOriginalConstructor()->getMock();
87  $oauthConsumerHelper->expects(
88  $this->any()
89  )->method(
90  'createConsumer'
91  )->will(
92  $this->returnValue($oauthConsumer)
93  );
94  $oauthConsumerHelper->expects($this->any())->method('loadConsumer')->will($this->returnValue($oauthConsumer));
95 
96  $this->_service = new \Magento\Integration\Model\IntegrationService(
97  $this->_integrationFactory,
98  $oauthConsumerHelper
99  );
100  $this->_emptyIntegrationMock = $this->getMockBuilder(
101  \Magento\Integration\Model\Integration::class
102  )->disableOriginalConstructor()->setMethods(
103  [
104  'getData',
105  'getId',
106  'getName',
107  'getEmail',
108  'getEndpoint',
109  'load',
110  'loadByName',
111  'save',
112  'delete',
113  '__wakeup',
114  ]
115  )->getMock();
116  $this->_emptyIntegrationMock->expects($this->any())->method('getId')->will($this->returnValue(null));
117  }
118 
119  public function testCreateSuccess()
120  {
121  $this->_integrationMock->expects(
122  $this->any()
123  )->method(
124  'getId'
125  )->will(
126  $this->returnValue(self::VALUE_INTEGRATION_ID)
127  );
128  $this->_integrationMock->expects(
129  $this->any()
130  )->method(
131  'getData'
132  )->will(
133  $this->returnValue($this->_integrationData)
134  );
135  $this->_integrationMock->expects(
136  $this->any()
137  )->method(
138  'load'
139  )->with(
140  self::VALUE_INTEGRATION_NAME,
141  'name'
142  )->will(
143  $this->returnValue($this->_emptyIntegrationMock)
144  );
145  $this->_integrationMock->expects($this->any())->method('save')->will($this->returnSelf());
146  $this->_setValidIntegrationData();
147  $resultData = $this->_service->create($this->_integrationData)->getData();
148  $this->assertSame($this->_integrationData, $resultData);
149  }
150 
156  {
157  $this->_integrationMock->expects(
158  $this->any()
159  )->method(
160  'getId'
161  )->will(
162  $this->returnValue(self::VALUE_INTEGRATION_ID)
163  );
164  $this->_integrationMock->expects(
165  $this->any()
166  )->method(
167  'getData'
168  )->will(
169  $this->returnValue($this->_integrationData)
170  );
171  $this->_integrationMock->expects(
172  $this->any()
173  )->method(
174  'load'
175  )->with(
176  self::VALUE_INTEGRATION_NAME,
177  'name'
178  )->will(
179  $this->returnValue($this->_integrationMock)
180  );
181  $this->_integrationMock->expects($this->never())->method('save')->will($this->returnSelf());
182  $this->_service->create($this->_integrationData);
183  }
184 
185  public function testUpdateSuccess()
186  {
187  $this->_integrationMock->expects(
188  $this->any()
189  )->method(
190  'getId'
191  )->will(
192  $this->returnValue(self::VALUE_INTEGRATION_ID)
193  );
194  $this->_integrationMock->expects(
195  $this->any()
196  )->method(
197  'getData'
198  )->will(
199  $this->returnValue($this->_integrationData)
200  );
201  $this->_integrationMock->expects(
202  $this->at(0)
203  )->method(
204  'load'
205  )->with(
206  self::VALUE_INTEGRATION_ID
207  )->will(
208  $this->returnValue($this->_integrationMock)
209  );
210  $this->_integrationMock->expects($this->once())->method('save')->will($this->returnSelf());
211  $this->_setValidIntegrationData();
212  $integrationData = $this->_service->update($this->_integrationData)->getData();
213  $this->assertEquals($this->_integrationData, $integrationData);
214  }
215 
217  {
218  $this->_integrationMock->expects(
219  $this->any()
220  )->method(
221  'getId'
222  )->will(
223  $this->returnValue(self::VALUE_INTEGRATION_ID)
224  );
225  $this->_integrationMock->expects(
226  $this->any()
227  )->method(
228  'load'
229  )->will(
230  $this->onConsecutiveCalls($this->_integrationMock, $this->_emptyIntegrationMock)
231  );
232  $this->_integrationMock->expects($this->once())->method('save')->will($this->returnSelf());
233  $this->_setValidIntegrationData();
234  $integrationData = [
235  'integration_id' => self::VALUE_INTEGRATION_ID,
238  'endpoint' => self::VALUE_INTEGRATION_ENDPOINT,
239  ];
240  $this->_integrationMock->expects($this->any())->method('getData')->will($this->returnValue($integrationData));
241 
242  $updatedData = $this->_service->update($integrationData)->getData();
243  $this->assertEquals($integrationData, $updatedData);
244  }
245 
250  public function testUpdateException()
251  {
252  $this->_integrationMock->expects(
253  $this->any()
254  )->method(
255  'getId'
256  )->will(
257  $this->returnValue(self::VALUE_INTEGRATION_ID)
258  );
259  $this->_integrationMock->expects(
260  $this->any()
261  )->method(
262  'load'
263  )->will(
264  $this->onConsecutiveCalls($this->_integrationMock, $this->_getAnotherIntegrationMock())
265  );
266  $this->_integrationMock->expects($this->never())->method('save')->will($this->returnSelf());
267  $this->_setValidIntegrationData();
268  $integrationData = [
269  'integration_id' => self::VALUE_INTEGRATION_ID,
272  'endpoint' => self::VALUE_INTEGRATION_ENDPOINT,
273  ];
274  $this->_service->update($integrationData);
275  }
276 
277  public function testGet()
278  {
279  $this->_integrationMock->expects(
280  $this->any()
281  )->method(
282  'getId'
283  )->will(
284  $this->returnValue(self::VALUE_INTEGRATION_ID)
285  );
286  $this->_integrationMock->expects(
287  $this->any()
288  )->method(
289  'getData'
290  )->will(
291  $this->returnValue($this->_integrationData)
292  );
293  $this->_integrationMock->expects($this->once())->method('load')->will($this->returnSelf());
294  $this->_integrationMock->expects($this->never())->method('save');
295  $integrationData = $this->_service->get(self::VALUE_INTEGRATION_ID)->getData();
296  $this->assertEquals($this->_integrationData, $integrationData);
297  }
298 
303  public function testGetException()
304  {
305  $this->_integrationMock->expects($this->any())->method('getId')->will($this->returnValue(null));
306  $this->_integrationMock->expects($this->once())->method('load')->will($this->returnSelf());
307  $this->_integrationMock->expects($this->never())->method('save');
308  $this->_service->get(self::VALUE_INTEGRATION_ID)->getData();
309  }
310 
311  public function testFindByName()
312  {
313  $this->_integrationMock->expects(
314  $this->any()
315  )->method(
316  'load'
317  )->with(
318  self::VALUE_INTEGRATION_NAME,
319  'name'
320  )->will(
321  $this->returnValue($this->_integrationMock)
322  );
323  $this->_integrationMock->expects(
324  $this->any()
325  )->method(
326  'getData'
327  )->will(
328  $this->returnValue($this->_integrationData)
329  );
330  $integration = $this->_service->findByName(self::VALUE_INTEGRATION_NAME);
331  $this->assertEquals($this->_integrationData[Integration::NAME], $integration->getData()[Integration::NAME]);
332  }
333 
334  public function testFindByNameNotFound()
335  {
336  $this->_integrationMock->expects(
337  $this->any()
338  )->method(
339  'load'
340  )->with(
341  self::VALUE_INTEGRATION_NAME,
342  'name'
343  )->will(
344  $this->returnValue($this->_emptyIntegrationMock)
345  );
346  $this->_emptyIntegrationMock->expects($this->any())->method('getData')->will($this->returnValue(null));
347  $integration = $this->_service->findByName(self::VALUE_INTEGRATION_NAME);
348  $this->assertNull($integration->getData());
349  }
350 
351  public function testDelete()
352  {
353  $this->_integrationMock->expects(
354  $this->once()
355  )->method(
356  'getId'
357  )->will(
358  $this->returnValue(self::VALUE_INTEGRATION_ID)
359  );
360  $this->_integrationMock->expects(
361  $this->once()
362  )->method(
363  'load'
364  )->with(
365  self::VALUE_INTEGRATION_ID
366  )->will(
367  $this->returnValue($this->_integrationMock)
368  );
369  $this->_integrationMock->expects(
370  $this->once()
371  )->method(
372  'delete'
373  )->will(
374  $this->returnValue($this->_integrationMock)
375  );
376  $this->_integrationMock->expects(
377  $this->any()
378  )->method(
379  'getData'
380  )->will(
381  $this->returnValue($this->_integrationData)
382  );
383  $integrationData = $this->_service->delete(self::VALUE_INTEGRATION_ID);
384  $this->assertEquals($this->_integrationData[Integration::ID], $integrationData[Integration::ID]);
385  }
386 
391  public function testDeleteException()
392  {
393  $this->_integrationMock->expects($this->any())->method('getId')->will($this->returnValue(null));
394  $this->_integrationMock->expects($this->once())->method('load')->will($this->returnSelf());
395  $this->_integrationMock->expects($this->never())->method('delete');
396  $this->_service->delete(self::VALUE_INTEGRATION_ID);
397  }
398 
399  public function testFindByConsumerId()
400  {
401  $this->_integrationMock->expects(
402  $this->any()
403  )->method(
404  'getData'
405  )->will(
406  $this->returnValue($this->_integrationData)
407  );
408 
409  $this->_integrationMock->expects(
410  $this->once()
411  )->method(
412  'load'
413  )->with(
414  self::VALUE_INTEGRATION_CONSUMER_ID,
415  'consumer_id'
416  )->will(
417  $this->returnValue($this->_integrationMock)
418  );
419 
420  $integration = $this->_service->findByConsumerId(self::VALUE_INTEGRATION_CONSUMER_ID);
421  $this->assertEquals($this->_integrationData[Integration::NAME], $integration->getData()[Integration::NAME]);
422  }
423 
425  {
426  $this->_emptyIntegrationMock->expects($this->any())->method('getData')->will($this->returnValue(null));
427 
428  $this->_integrationMock->expects(
429  $this->once()
430  )->method(
431  'load'
432  )->with(
433  self::VALUE_INTEGRATION_CONSUMER_ID,
434  'consumer_id'
435  )->will(
436  $this->returnValue($this->_emptyIntegrationMock)
437  );
438 
439  $integration = $this->_service->findByConsumerId(1);
440  $this->assertNull($integration->getData());
441  }
442 
446  private function _setValidIntegrationData()
447  {
448  $this->_integrationMock->expects(
449  $this->any()
450  )->method(
451  'getName'
452  )->will(
453  $this->returnValue(self::VALUE_INTEGRATION_NAME)
454  );
455  $this->_integrationMock->expects(
456  $this->any()
457  )->method(
458  'getEmail'
459  )->will(
460  $this->returnValue(self::VALUE_INTEGRATION_EMAIL)
461  );
462  $this->_integrationMock->expects(
463  $this->any()
464  )->method(
465  'getEndpoint'
466  )->will(
467  $this->returnValue(self::VALUE_INTEGRATION_ENDPOINT)
468  );
469  }
470 
478  private function _getAnotherIntegrationMock(
479  $name = self::VALUE_INTEGRATION_NAME,
480  $integrationId = self::VALUE_INTEGRATION_ID
481  ) {
482  $integrationMock = $this->getMockBuilder(
483  \Magento\Integration\Model\Integration::class
484  )->disableOriginalConstructor()->setMethods(
485  [
486  'getData',
487  'getId',
488  'getName',
489  'getEmail',
490  'getEndpoint',
491  'load',
492  'loadByName',
493  'save',
494  'delete',
495  '__wakeup',
496  ]
497  )->getMock();
498  $integrationMock->expects($this->any())->method('getId')->will($this->returnValue($integrationId));
499  $integrationMock->expects($this->any())->method('getName')->will($this->returnValue($name));
500  $integrationMock->expects(
501  $this->any()
502  )->method(
503  'getEmail'
504  )->will(
505  $this->returnValue(self::VALUE_INTEGRATION_EMAIL)
506  );
507  $integrationMock->expects(
508  $this->any()
509  )->method(
510  'getEndpoint'
511  )->will(
512  $this->returnValue(self::VALUE_INTEGRATION_ENDPOINT)
513  );
514  return $integrationMock;
515  }
516 }
if(!isset($_GET['name'])) $name
Definition: log.php:14