ConsumerTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Helper\Oauth;
  7. /**
  8. * Test for \Magento\Integration\Model\Oauth\Consumer
  9. *
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class ConsumerTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Store\Model\StoreManagerInterface */
  15. protected $_storeManagerMock;
  16. /** @var \Magento\Integration\Model\Oauth\ConsumerFactory */
  17. protected $_consumerFactory;
  18. /** @var \Magento\Integration\Model\Oauth\Consumer */
  19. protected $_consumerMock;
  20. /** @var \Magento\Framework\HTTP\ZendClient */
  21. protected $_httpClientMock;
  22. /** @var \Magento\Integration\Model\Oauth\TokenFactory */
  23. protected $_tokenFactory;
  24. /** @var \Magento\Integration\Model\Oauth\Token */
  25. protected $_tokenMock;
  26. /** @var \Magento\Store\Model\Store */
  27. protected $_storeMock;
  28. /** @var \Magento\Integration\Helper\Oauth\Data */
  29. protected $_dataHelper;
  30. /** @var \Magento\Integration\Api\OauthServiceInterface */
  31. protected $_oauthService;
  32. /** @var \Psr\Log\LoggerInterface */
  33. protected $_loggerMock;
  34. protected function setUp()
  35. {
  36. $this->_consumerFactory = $this->getMockBuilder(\Magento\Integration\Model\Oauth\ConsumerFactory::class)
  37. ->disableOriginalConstructor()
  38. ->setMethods(['create'])
  39. ->getMock();
  40. $this->_consumerMock = $this->getMockBuilder(
  41. \Magento\Integration\Model\Oauth\Consumer::class
  42. )->disableOriginalConstructor()->getMock();
  43. $this->_consumerFactory->expects(
  44. $this->any()
  45. )->method(
  46. 'create'
  47. )->will(
  48. $this->returnValue($this->_consumerMock)
  49. );
  50. $this->_tokenFactory = $this->getMockBuilder(
  51. \Magento\Integration\Model\Oauth\TokenFactory::class
  52. )->disableOriginalConstructor()->setMethods(['create'])->getMock();
  53. $this->_tokenMock = $this->getMockBuilder(
  54. \Magento\Integration\Model\Oauth\Token::class
  55. )->disableOriginalConstructor()->getMock();
  56. $this->_tokenFactory->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
  57. $this->_storeManagerMock = $this->getMockBuilder(
  58. \Magento\Store\Model\StoreManagerInterface::class
  59. )->disableOriginalConstructor()->getMockForAbstractClass();
  60. $this->_storeMock = $this->getMockBuilder(
  61. \Magento\Store\Model\Store::class
  62. )->disableOriginalConstructor()->getMock();
  63. $this->_storeManagerMock->expects(
  64. $this->any()
  65. )->method(
  66. 'getStore'
  67. )->will(
  68. $this->returnValue($this->_storeMock)
  69. );
  70. $this->_dataHelper = $this->getMockBuilder(
  71. \Magento\Integration\Helper\Oauth\Data::class
  72. )->disableOriginalConstructor()->getMock();
  73. $oauthHelperMock = $this->getMockBuilder(
  74. \Magento\Framework\Oauth\Helper\Oauth::class
  75. )->disableOriginalConstructor()->getMock();
  76. $tokenProviderMock = $this->getMockBuilder(
  77. \Magento\Integration\Model\Oauth\Token\Provider::class
  78. )->disableOriginalConstructor()->getMock();
  79. $this->_httpClientMock = $this->getMockBuilder(
  80. \Magento\Framework\HTTP\ZendClient::class
  81. )->disableOriginalConstructor()->getMock();
  82. $this->_loggerMock = $this->getMockBuilder(
  83. \Psr\Log\LoggerInterface::class
  84. )->getMock();
  85. $this->_oauthService = new \Magento\Integration\Model\OauthService(
  86. $this->_storeManagerMock,
  87. $this->_consumerFactory,
  88. $this->_tokenFactory,
  89. $this->_dataHelper,
  90. $this->_httpClientMock,
  91. $this->_loggerMock,
  92. $oauthHelperMock,
  93. $tokenProviderMock
  94. );
  95. }
  96. protected function tearDown()
  97. {
  98. unset($this->_storeManagerMock);
  99. unset($this->_consumerFactory);
  100. unset($this->_tokenFactory);
  101. unset($this->_dataHelper);
  102. unset($this->_httpClientMock);
  103. unset($this->_loggerMock);
  104. unset($this->_oauthService);
  105. }
  106. public function testCreateConsumer()
  107. {
  108. $key = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY);
  109. $secret = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_SECRET);
  110. $consumerData = ['name' => 'Integration Name', 'key' => $key, 'secret' => $secret];
  111. $this->_consumerMock->expects($this->once())->method('setData')->will($this->returnSelf());
  112. $this->_consumerMock->expects($this->once())->method('save')->will($this->returnSelf());
  113. /** @var \Magento\Integration\Model\Oauth\Consumer $consumer */
  114. $consumer = $this->_oauthService->createConsumer($consumerData);
  115. $this->assertEquals($consumer, $this->_consumerMock, 'Consumer object was expected to be returned');
  116. }
  117. public function testPostToConsumer()
  118. {
  119. $consumerId = 1;
  120. $key = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY);
  121. $secret = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_SECRET);
  122. $oauthVerifier = $this->_generateRandomString(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_TOKEN_VERIFIER);
  123. $consumerData = ['entity_id' => $consumerId, 'key' => $key, 'secret' => $secret];
  124. $this->_consumerMock->expects(
  125. $this->once()
  126. )->method(
  127. 'load'
  128. )->with(
  129. $this->equalTo($consumerId)
  130. )->will(
  131. $this->returnSelf()
  132. );
  133. $dateHelperMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\DateTime::class)
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $dateHelperMock->expects($this->any())->method('gmtDate');
  137. $dateHelper = new \ReflectionProperty(\Magento\Integration\Model\OauthService::class, '_dateHelper');
  138. $dateHelper->setAccessible(true);
  139. $dateHelper->setValue($this->_oauthService, $dateHelperMock);
  140. $this->_consumerMock->expects($this->once())->method('getId')->will($this->returnValue($consumerId));
  141. $this->_consumerMock->expects($this->once())->method('getData')->will($this->returnValue($consumerData));
  142. $this->_httpClientMock->expects(
  143. $this->once()
  144. )->method(
  145. 'setUri'
  146. )->with(
  147. 'http://www.magento.com'
  148. )->will(
  149. $this->returnSelf()
  150. );
  151. $this->_httpClientMock->expects($this->once())->method('setParameterPost')->will($this->returnSelf());
  152. $this->_tokenMock->expects(
  153. $this->once()
  154. )->method(
  155. 'createVerifierToken'
  156. )->with(
  157. $consumerId
  158. )->will(
  159. $this->returnSelf()
  160. );
  161. $this->_tokenMock->expects($this->any())->method('getVerifier')->will($this->returnValue($oauthVerifier));
  162. $this->_dataHelper->expects($this->once())->method('getConsumerPostMaxRedirects')->will($this->returnValue(5));
  163. $this->_dataHelper->expects($this->once())->method('getConsumerPostTimeout')->will($this->returnValue(120));
  164. $verifier = $this->_oauthService->postToConsumer($consumerId, 'http://www.magento.com');
  165. $this->assertEquals($oauthVerifier, $verifier, 'Checking Oauth Verifier');
  166. }
  167. /**
  168. * @param $length
  169. * @return bool|string
  170. */
  171. private function _generateRandomString($length)
  172. {
  173. return substr(
  174. str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 5)),
  175. 0,
  176. $length
  177. );
  178. }
  179. }