IntegrationServiceTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. <?php
  2. /**
  3. * Test for \Magento\Integration\Model\IntegrationService
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Integration\Test\Unit\Model;
  9. use Magento\Integration\Model\Integration;
  10. class IntegrationServiceTest extends \PHPUnit\Framework\TestCase
  11. {
  12. const VALUE_INTEGRATION_ID = 1;
  13. const VALUE_INTEGRATION_NAME = 'Integration Name';
  14. const VALUE_INTEGRATION_ANOTHER_NAME = 'Another Integration Name';
  15. const VALUE_INTEGRATION_EMAIL = 'test@magento.com';
  16. const VALUE_INTEGRATION_SETUP_BACKEND = 0;
  17. const VALUE_INTEGRATION_ENDPOINT = 'http://magento.ll/endpoint';
  18. const VALUE_INTEGRATION_CONSUMER_ID = 1;
  19. /** @var \PHPUnit_Framework_MockObject_MockObject */
  20. private $_integrationFactory;
  21. /** @var \PHPUnit_Framework_MockObject_MockObject */
  22. private $_integrationMock;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject */
  24. private $_emptyIntegrationMock;
  25. /** @var \Magento\Integration\Model\IntegrationService */
  26. private $_service;
  27. /** @var array */
  28. private $_integrationData;
  29. protected function setUp()
  30. {
  31. $this->_integrationFactory = $this->getMockBuilder(\Magento\Integration\Model\IntegrationFactory::class)
  32. ->disableOriginalConstructor()
  33. ->setMethods(['create'])
  34. ->getMock();
  35. $this->_integrationMock = $this->getMockBuilder(
  36. \Magento\Integration\Model\Integration::class
  37. )->disableOriginalConstructor()->setMethods(
  38. [
  39. 'getData',
  40. 'getId',
  41. 'getName',
  42. 'getEmail',
  43. 'getEndpoint',
  44. 'load',
  45. 'loadByName',
  46. 'save',
  47. 'delete',
  48. '__wakeup',
  49. ]
  50. )->getMock();
  51. $this->_integrationData = [
  52. Integration::ID => self::VALUE_INTEGRATION_ID,
  53. Integration::NAME => self::VALUE_INTEGRATION_NAME,
  54. Integration::EMAIL => self::VALUE_INTEGRATION_EMAIL,
  55. Integration::EMAIL => self::VALUE_INTEGRATION_ENDPOINT,
  56. Integration::SETUP_TYPE => self::VALUE_INTEGRATION_SETUP_BACKEND,
  57. ];
  58. $this->_integrationFactory->expects(
  59. $this->any()
  60. )->method(
  61. 'create'
  62. )->will(
  63. $this->returnValue($this->_integrationMock)
  64. );
  65. $oauthConsumerHelper = $this->getMockBuilder(
  66. \Magento\Integration\Api\OauthServiceInterface::class
  67. )->disableOriginalConstructor()->getMock();
  68. $oauthConsumer = $this->getMockBuilder(
  69. \Magento\Integration\Model\Oauth\Consumer::class
  70. )->disableOriginalConstructor()->getMock();
  71. $oauthConsumerHelper->expects(
  72. $this->any()
  73. )->method(
  74. 'createConsumer'
  75. )->will(
  76. $this->returnValue($oauthConsumer)
  77. );
  78. $oauthConsumerHelper->expects($this->any())->method('loadConsumer')->will($this->returnValue($oauthConsumer));
  79. $this->_service = new \Magento\Integration\Model\IntegrationService(
  80. $this->_integrationFactory,
  81. $oauthConsumerHelper
  82. );
  83. $this->_emptyIntegrationMock = $this->getMockBuilder(
  84. \Magento\Integration\Model\Integration::class
  85. )->disableOriginalConstructor()->setMethods(
  86. [
  87. 'getData',
  88. 'getId',
  89. 'getName',
  90. 'getEmail',
  91. 'getEndpoint',
  92. 'load',
  93. 'loadByName',
  94. 'save',
  95. 'delete',
  96. '__wakeup',
  97. ]
  98. )->getMock();
  99. $this->_emptyIntegrationMock->expects($this->any())->method('getId')->will($this->returnValue(null));
  100. }
  101. public function testCreateSuccess()
  102. {
  103. $this->_integrationMock->expects(
  104. $this->any()
  105. )->method(
  106. 'getId'
  107. )->will(
  108. $this->returnValue(self::VALUE_INTEGRATION_ID)
  109. );
  110. $this->_integrationMock->expects(
  111. $this->any()
  112. )->method(
  113. 'getData'
  114. )->will(
  115. $this->returnValue($this->_integrationData)
  116. );
  117. $this->_integrationMock->expects(
  118. $this->any()
  119. )->method(
  120. 'load'
  121. )->with(
  122. self::VALUE_INTEGRATION_NAME,
  123. 'name'
  124. )->will(
  125. $this->returnValue($this->_emptyIntegrationMock)
  126. );
  127. $this->_integrationMock->expects($this->any())->method('save')->will($this->returnSelf());
  128. $this->_setValidIntegrationData();
  129. $resultData = $this->_service->create($this->_integrationData)->getData();
  130. $this->assertSame($this->_integrationData, $resultData);
  131. }
  132. /**
  133. * @expectedException \Magento\Framework\Exception\IntegrationException
  134. * @expectedExceptionMessage The integration with name "Integration Name" exists.
  135. */
  136. public function testCreateIntegrationAlreadyExistsException()
  137. {
  138. $this->_integrationMock->expects(
  139. $this->any()
  140. )->method(
  141. 'getId'
  142. )->will(
  143. $this->returnValue(self::VALUE_INTEGRATION_ID)
  144. );
  145. $this->_integrationMock->expects(
  146. $this->any()
  147. )->method(
  148. 'getData'
  149. )->will(
  150. $this->returnValue($this->_integrationData)
  151. );
  152. $this->_integrationMock->expects(
  153. $this->any()
  154. )->method(
  155. 'load'
  156. )->with(
  157. self::VALUE_INTEGRATION_NAME,
  158. 'name'
  159. )->will(
  160. $this->returnValue($this->_integrationMock)
  161. );
  162. $this->_integrationMock->expects($this->never())->method('save')->will($this->returnSelf());
  163. $this->_service->create($this->_integrationData);
  164. }
  165. public function testUpdateSuccess()
  166. {
  167. $this->_integrationMock->expects(
  168. $this->any()
  169. )->method(
  170. 'getId'
  171. )->will(
  172. $this->returnValue(self::VALUE_INTEGRATION_ID)
  173. );
  174. $this->_integrationMock->expects(
  175. $this->any()
  176. )->method(
  177. 'getData'
  178. )->will(
  179. $this->returnValue($this->_integrationData)
  180. );
  181. $this->_integrationMock->expects(
  182. $this->at(0)
  183. )->method(
  184. 'load'
  185. )->with(
  186. self::VALUE_INTEGRATION_ID
  187. )->will(
  188. $this->returnValue($this->_integrationMock)
  189. );
  190. $this->_integrationMock->expects($this->once())->method('save')->will($this->returnSelf());
  191. $this->_setValidIntegrationData();
  192. $integrationData = $this->_service->update($this->_integrationData)->getData();
  193. $this->assertEquals($this->_integrationData, $integrationData);
  194. }
  195. public function testUpdateSuccessNameChanged()
  196. {
  197. $this->_integrationMock->expects(
  198. $this->any()
  199. )->method(
  200. 'getId'
  201. )->will(
  202. $this->returnValue(self::VALUE_INTEGRATION_ID)
  203. );
  204. $this->_integrationMock->expects(
  205. $this->any()
  206. )->method(
  207. 'load'
  208. )->will(
  209. $this->onConsecutiveCalls($this->_integrationMock, $this->_emptyIntegrationMock)
  210. );
  211. $this->_integrationMock->expects($this->once())->method('save')->will($this->returnSelf());
  212. $this->_setValidIntegrationData();
  213. $integrationData = [
  214. 'integration_id' => self::VALUE_INTEGRATION_ID,
  215. 'name' => self::VALUE_INTEGRATION_ANOTHER_NAME,
  216. 'email' => self::VALUE_INTEGRATION_EMAIL,
  217. 'endpoint' => self::VALUE_INTEGRATION_ENDPOINT,
  218. ];
  219. $this->_integrationMock->expects($this->any())->method('getData')->will($this->returnValue($integrationData));
  220. $updatedData = $this->_service->update($integrationData)->getData();
  221. $this->assertEquals($integrationData, $updatedData);
  222. }
  223. /**
  224. * @expectedException \Magento\Framework\Exception\IntegrationException
  225. * @expectedExceptionMessage The integration with name "Another Integration Name" exists.
  226. */
  227. public function testUpdateException()
  228. {
  229. $this->_integrationMock->expects(
  230. $this->any()
  231. )->method(
  232. 'getId'
  233. )->will(
  234. $this->returnValue(self::VALUE_INTEGRATION_ID)
  235. );
  236. $this->_integrationMock->expects(
  237. $this->any()
  238. )->method(
  239. 'load'
  240. )->will(
  241. $this->onConsecutiveCalls($this->_integrationMock, $this->_getAnotherIntegrationMock())
  242. );
  243. $this->_integrationMock->expects($this->never())->method('save')->will($this->returnSelf());
  244. $this->_setValidIntegrationData();
  245. $integrationData = [
  246. 'integration_id' => self::VALUE_INTEGRATION_ID,
  247. 'name' => self::VALUE_INTEGRATION_ANOTHER_NAME,
  248. 'email' => self::VALUE_INTEGRATION_EMAIL,
  249. 'endpoint' => self::VALUE_INTEGRATION_ENDPOINT,
  250. ];
  251. $this->_service->update($integrationData);
  252. }
  253. public function testGet()
  254. {
  255. $this->_integrationMock->expects(
  256. $this->any()
  257. )->method(
  258. 'getId'
  259. )->will(
  260. $this->returnValue(self::VALUE_INTEGRATION_ID)
  261. );
  262. $this->_integrationMock->expects(
  263. $this->any()
  264. )->method(
  265. 'getData'
  266. )->will(
  267. $this->returnValue($this->_integrationData)
  268. );
  269. $this->_integrationMock->expects($this->once())->method('load')->will($this->returnSelf());
  270. $this->_integrationMock->expects($this->never())->method('save');
  271. $integrationData = $this->_service->get(self::VALUE_INTEGRATION_ID)->getData();
  272. $this->assertEquals($this->_integrationData, $integrationData);
  273. }
  274. /**
  275. * @expectedException \Magento\Framework\Exception\IntegrationException
  276. * @expectedExceptionMessage The integration with ID "1" doesn't exist.
  277. */
  278. public function testGetException()
  279. {
  280. $this->_integrationMock->expects($this->any())->method('getId')->will($this->returnValue(null));
  281. $this->_integrationMock->expects($this->once())->method('load')->will($this->returnSelf());
  282. $this->_integrationMock->expects($this->never())->method('save');
  283. $this->_service->get(self::VALUE_INTEGRATION_ID)->getData();
  284. }
  285. public function testFindByName()
  286. {
  287. $this->_integrationMock->expects(
  288. $this->any()
  289. )->method(
  290. 'load'
  291. )->with(
  292. self::VALUE_INTEGRATION_NAME,
  293. 'name'
  294. )->will(
  295. $this->returnValue($this->_integrationMock)
  296. );
  297. $this->_integrationMock->expects(
  298. $this->any()
  299. )->method(
  300. 'getData'
  301. )->will(
  302. $this->returnValue($this->_integrationData)
  303. );
  304. $integration = $this->_service->findByName(self::VALUE_INTEGRATION_NAME);
  305. $this->assertEquals($this->_integrationData[Integration::NAME], $integration->getData()[Integration::NAME]);
  306. }
  307. public function testFindByNameNotFound()
  308. {
  309. $this->_integrationMock->expects(
  310. $this->any()
  311. )->method(
  312. 'load'
  313. )->with(
  314. self::VALUE_INTEGRATION_NAME,
  315. 'name'
  316. )->will(
  317. $this->returnValue($this->_emptyIntegrationMock)
  318. );
  319. $this->_emptyIntegrationMock->expects($this->any())->method('getData')->will($this->returnValue(null));
  320. $integration = $this->_service->findByName(self::VALUE_INTEGRATION_NAME);
  321. $this->assertNull($integration->getData());
  322. }
  323. public function testDelete()
  324. {
  325. $this->_integrationMock->expects(
  326. $this->once()
  327. )->method(
  328. 'getId'
  329. )->will(
  330. $this->returnValue(self::VALUE_INTEGRATION_ID)
  331. );
  332. $this->_integrationMock->expects(
  333. $this->once()
  334. )->method(
  335. 'load'
  336. )->with(
  337. self::VALUE_INTEGRATION_ID
  338. )->will(
  339. $this->returnValue($this->_integrationMock)
  340. );
  341. $this->_integrationMock->expects(
  342. $this->once()
  343. )->method(
  344. 'delete'
  345. )->will(
  346. $this->returnValue($this->_integrationMock)
  347. );
  348. $this->_integrationMock->expects(
  349. $this->any()
  350. )->method(
  351. 'getData'
  352. )->will(
  353. $this->returnValue($this->_integrationData)
  354. );
  355. $integrationData = $this->_service->delete(self::VALUE_INTEGRATION_ID);
  356. $this->assertEquals($this->_integrationData[Integration::ID], $integrationData[Integration::ID]);
  357. }
  358. /**
  359. * @expectedException \Magento\Framework\Exception\IntegrationException
  360. * @expectedExceptionMessage The integration with ID "1" doesn't exist.
  361. */
  362. public function testDeleteException()
  363. {
  364. $this->_integrationMock->expects($this->any())->method('getId')->will($this->returnValue(null));
  365. $this->_integrationMock->expects($this->once())->method('load')->will($this->returnSelf());
  366. $this->_integrationMock->expects($this->never())->method('delete');
  367. $this->_service->delete(self::VALUE_INTEGRATION_ID);
  368. }
  369. public function testFindByConsumerId()
  370. {
  371. $this->_integrationMock->expects(
  372. $this->any()
  373. )->method(
  374. 'getData'
  375. )->will(
  376. $this->returnValue($this->_integrationData)
  377. );
  378. $this->_integrationMock->expects(
  379. $this->once()
  380. )->method(
  381. 'load'
  382. )->with(
  383. self::VALUE_INTEGRATION_CONSUMER_ID,
  384. 'consumer_id'
  385. )->will(
  386. $this->returnValue($this->_integrationMock)
  387. );
  388. $integration = $this->_service->findByConsumerId(self::VALUE_INTEGRATION_CONSUMER_ID);
  389. $this->assertEquals($this->_integrationData[Integration::NAME], $integration->getData()[Integration::NAME]);
  390. }
  391. public function testFindByConsumerIdNotFound()
  392. {
  393. $this->_emptyIntegrationMock->expects($this->any())->method('getData')->will($this->returnValue(null));
  394. $this->_integrationMock->expects(
  395. $this->once()
  396. )->method(
  397. 'load'
  398. )->with(
  399. self::VALUE_INTEGRATION_CONSUMER_ID,
  400. 'consumer_id'
  401. )->will(
  402. $this->returnValue($this->_emptyIntegrationMock)
  403. );
  404. $integration = $this->_service->findByConsumerId(1);
  405. $this->assertNull($integration->getData());
  406. }
  407. /**
  408. * Set valid integration data
  409. */
  410. private function _setValidIntegrationData()
  411. {
  412. $this->_integrationMock->expects(
  413. $this->any()
  414. )->method(
  415. 'getName'
  416. )->will(
  417. $this->returnValue(self::VALUE_INTEGRATION_NAME)
  418. );
  419. $this->_integrationMock->expects(
  420. $this->any()
  421. )->method(
  422. 'getEmail'
  423. )->will(
  424. $this->returnValue(self::VALUE_INTEGRATION_EMAIL)
  425. );
  426. $this->_integrationMock->expects(
  427. $this->any()
  428. )->method(
  429. 'getEndpoint'
  430. )->will(
  431. $this->returnValue(self::VALUE_INTEGRATION_ENDPOINT)
  432. );
  433. }
  434. /**
  435. * Create mock integration
  436. *
  437. * @param string $name
  438. * @param int $integrationId
  439. * @return mixed
  440. */
  441. private function _getAnotherIntegrationMock(
  442. $name = self::VALUE_INTEGRATION_NAME,
  443. $integrationId = self::VALUE_INTEGRATION_ID
  444. ) {
  445. $integrationMock = $this->getMockBuilder(
  446. \Magento\Integration\Model\Integration::class
  447. )->disableOriginalConstructor()->setMethods(
  448. [
  449. 'getData',
  450. 'getId',
  451. 'getName',
  452. 'getEmail',
  453. 'getEndpoint',
  454. 'load',
  455. 'loadByName',
  456. 'save',
  457. 'delete',
  458. '__wakeup',
  459. ]
  460. )->getMock();
  461. $integrationMock->expects($this->any())->method('getId')->will($this->returnValue($integrationId));
  462. $integrationMock->expects($this->any())->method('getName')->will($this->returnValue($name));
  463. $integrationMock->expects(
  464. $this->any()
  465. )->method(
  466. 'getEmail'
  467. )->will(
  468. $this->returnValue(self::VALUE_INTEGRATION_EMAIL)
  469. );
  470. $integrationMock->expects(
  471. $this->any()
  472. )->method(
  473. 'getEndpoint'
  474. )->will(
  475. $this->returnValue(self::VALUE_INTEGRATION_ENDPOINT)
  476. );
  477. return $integrationMock;
  478. }
  479. }