IntegrationManagerTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Analytics\Test\Unit\Model;
  7. use Magento\Integration\Api\IntegrationServiceInterface;
  8. use Magento\Config\Model\Config;
  9. use Magento\Integration\Model\Integration;
  10. use Magento\Analytics\Model\IntegrationManager;
  11. use Magento\Integration\Api\OauthServiceInterface;
  12. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  13. class IntegrationManagerTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var IntegrationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $integrationServiceMock;
  19. /**
  20. * @var OauthServiceInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $oauthServiceMock;
  23. /**
  24. * @var Config|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $configMock;
  27. /**
  28. * @var Integration|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $integrationMock;
  31. /**
  32. * @var IntegrationManager
  33. */
  34. private $integrationManager;
  35. public function setUp()
  36. {
  37. $objectManagerHelper = new ObjectManagerHelper($this);
  38. $this->integrationServiceMock = $this->getMockBuilder(IntegrationServiceInterface::class)
  39. ->getMock();
  40. $this->configMock = $this->getMockBuilder(Config::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->oauthServiceMock = $this->getMockBuilder(OauthServiceInterface::class)
  44. ->getMock();
  45. $this->integrationMock = $this->getMockBuilder(Integration::class)
  46. ->disableOriginalConstructor()
  47. ->setMethods([
  48. 'getId',
  49. 'getConsumerId'
  50. ])
  51. ->getMock();
  52. $this->integrationManager = $objectManagerHelper->getObject(
  53. IntegrationManager::class,
  54. [
  55. 'integrationService' => $this->integrationServiceMock,
  56. 'oauthService' => $this->oauthServiceMock,
  57. 'config' => $this->configMock
  58. ]
  59. );
  60. }
  61. /**
  62. * @param string $status
  63. *
  64. * @return array
  65. */
  66. private function getIntegrationUserData($status)
  67. {
  68. return [
  69. 'name' => 'ma-integration-user',
  70. 'status' => $status,
  71. 'all_resources' => false,
  72. 'resource' => [
  73. 'Magento_Analytics::analytics',
  74. 'Magento_Analytics::analytics_api'
  75. ],
  76. ];
  77. }
  78. /**
  79. * @return void
  80. */
  81. public function testActivateIntegrationSuccess()
  82. {
  83. $this->integrationServiceMock->expects($this->once())
  84. ->method('findByName')
  85. ->with('ma-integration-user')
  86. ->willReturn($this->integrationMock);
  87. $this->integrationMock->expects($this->exactly(2))
  88. ->method('getId')
  89. ->willReturn(100500);
  90. $integrationData = $this->getIntegrationUserData(Integration::STATUS_ACTIVE);
  91. $integrationData['integration_id'] = 100500;
  92. $this->configMock->expects($this->exactly(2))
  93. ->method('getConfigDataValue')
  94. ->with('analytics/integration_name', null, null)
  95. ->willReturn('ma-integration-user');
  96. $this->integrationServiceMock->expects($this->once())
  97. ->method('update')
  98. ->with($integrationData);
  99. $this->assertTrue($this->integrationManager->activateIntegration());
  100. }
  101. /**
  102. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  103. */
  104. public function testActivateIntegrationFailureNoSuchEntity()
  105. {
  106. $this->integrationServiceMock->expects($this->once())
  107. ->method('findByName')
  108. ->with('ma-integration-user')
  109. ->willReturn($this->integrationMock);
  110. $this->integrationMock->expects($this->once())
  111. ->method('getId')
  112. ->willReturn(null);
  113. $this->configMock->expects($this->once())
  114. ->method('getConfigDataValue')
  115. ->with('analytics/integration_name', null, null)
  116. ->willReturn('ma-integration-user');
  117. $this->integrationServiceMock->expects($this->never())
  118. ->method('update');
  119. $this->integrationManager->activateIntegration();
  120. }
  121. /**
  122. * @dataProvider integrationIdDataProvider
  123. *
  124. * @param int|null $integrationId If null integration is absent.
  125. * @return void
  126. */
  127. public function testGetTokenNewIntegration($integrationId)
  128. {
  129. $this->configMock->expects($this->atLeastOnce())
  130. ->method('getConfigDataValue')
  131. ->with('analytics/integration_name', null, null)
  132. ->willReturn('ma-integration-user');
  133. $this->integrationServiceMock->expects($this->once())
  134. ->method('findByName')
  135. ->with('ma-integration-user')
  136. ->willReturn($this->integrationMock);
  137. $this->integrationMock->expects($this->once())
  138. ->method('getConsumerId')
  139. ->willReturn(100500);
  140. $this->integrationMock->expects($this->once())
  141. ->method('getId')
  142. ->willReturn($integrationId);
  143. if (!$integrationId) {
  144. $this->integrationServiceMock
  145. ->expects($this->once())
  146. ->method('create')
  147. ->with($this->getIntegrationUserData(Integration::STATUS_INACTIVE))
  148. ->willReturn($this->integrationMock);
  149. }
  150. $this->oauthServiceMock->expects($this->at(0))
  151. ->method('getAccessToken')
  152. ->with(100500)
  153. ->willReturn(false);
  154. $this->oauthServiceMock->expects($this->at(2))
  155. ->method('getAccessToken')
  156. ->with(100500)
  157. ->willReturn('IntegrationToken');
  158. $this->oauthServiceMock->expects($this->once())
  159. ->method('createAccessToken')
  160. ->with(100500, true)
  161. ->willReturn(true);
  162. $this->assertEquals('IntegrationToken', $this->integrationManager->generateToken());
  163. }
  164. /**
  165. * @dataProvider integrationIdDataProvider
  166. *
  167. * @param int|null $integrationId If null integration is absent.
  168. * @return void
  169. */
  170. public function testGetTokenExistingIntegration($integrationId)
  171. {
  172. $this->configMock->expects($this->atLeastOnce())
  173. ->method('getConfigDataValue')
  174. ->with('analytics/integration_name', null, null)
  175. ->willReturn('ma-integration-user');
  176. $this->integrationServiceMock->expects($this->once())
  177. ->method('findByName')
  178. ->with('ma-integration-user')
  179. ->willReturn($this->integrationMock);
  180. $this->integrationMock->expects($this->once())
  181. ->method('getConsumerId')
  182. ->willReturn(100500);
  183. $this->integrationMock->expects($this->once())
  184. ->method('getId')
  185. ->willReturn($integrationId);
  186. if (!$integrationId) {
  187. $this->integrationServiceMock
  188. ->expects($this->once())
  189. ->method('create')
  190. ->with($this->getIntegrationUserData(Integration::STATUS_INACTIVE))
  191. ->willReturn($this->integrationMock);
  192. }
  193. $this->oauthServiceMock->expects($this->once())
  194. ->method('getAccessToken')
  195. ->with(100500)
  196. ->willReturn('IntegrationToken');
  197. $this->oauthServiceMock->expects($this->never())
  198. ->method('createAccessToken');
  199. $this->assertEquals('IntegrationToken', $this->integrationManager->generateToken());
  200. }
  201. /**
  202. * @return array
  203. */
  204. public function integrationIdDataProvider()
  205. {
  206. return [
  207. [1],
  208. [null],
  209. ];
  210. }
  211. }