/app/code/Magento/Integration/Test/Unit/Helper/Oauth/ConsumerTest.php

https://gitlab.com/AlexandrSy/magento.xxx · PHP · 193 lines · 148 code · 30 blank · 15 comment · 0 complexity · 3dedc29cd8a0b7da6ec5770037987347 MD5 · raw file

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