AccessTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Controller\Token;
  7. /**
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. */
  10. class AccessTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. protected $request;
  16. /**
  17. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. protected $response;
  20. /**
  21. * @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $context;
  24. /**
  25. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager $objectManagerHelper
  26. */
  27. protected $objectManagerHelper;
  28. /**
  29. * @var \Magento\Framework\Oauth\OauthInterface|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $frameworkOauthSvcMock;
  32. /**
  33. * @var \Magento\Integration\Api\OauthServiceInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $intOauthServiceMock;
  36. /**
  37. * @var \Magento\Integration\Api\IntegrationServiceInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. protected $integrationServiceMock;
  40. /**
  41. * @var \Magento\Framework\Oauth\Helper\Request|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. protected $helperMock;
  44. /**
  45. * @var \Magento\Integration\Controller\Token\Access
  46. */
  47. protected $accessAction;
  48. protected function setUp()
  49. {
  50. $this->request = $this->createPartialMock(\Magento\Framework\App\RequestInterface::class, [
  51. 'getMethod',
  52. 'getModuleName',
  53. 'setModuleName',
  54. 'getActionName',
  55. 'setActionName',
  56. 'getParam',
  57. 'setParams',
  58. 'getParams',
  59. 'getCookie',
  60. 'isSecure'
  61. ]);
  62. $this->response = $this->createMock(\Magento\Framework\App\Console\Response::class);
  63. /** @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  64. $objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  65. /** @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  66. $eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  67. /** @var \Magento\Framework\View\Layout\ProcessorInterface|\PHPUnit_Framework_MockObject_MockObject */
  68. $update = $this->createMock(\Magento\Framework\View\Layout\ProcessorInterface::class);
  69. /** @var \Magento\Framework\View\Layout|\PHPUnit_Framework_MockObject_MockObject */
  70. $layout = $this->createMock(\Magento\Framework\View\Layout::class);
  71. $layout->expects($this->any())->method('getUpdate')->will($this->returnValue($update));
  72. /** @var \Magento\Framework\View\Page\Config */
  73. $pageConfig = $this->createMock(\Magento\Framework\View\Page\Config::class);
  74. $pageConfig->expects($this->any())->method('addBodyClass')->will($this->returnSelf());
  75. /** @var \Magento\Framework\View\Result\Page|\PHPUnit_Framework_MockObject_MockObject */
  76. $page = $this->createPartialMock(
  77. \Magento\Framework\View\Result\Page::class,
  78. ['getConfig', 'initLayout', 'addPageLayoutHandles', 'getLayout']
  79. );
  80. $page->expects($this->any())->method('getConfig')->will($this->returnValue($pageConfig));
  81. $page->expects($this->any())->method('addPageLayoutHandles')->will($this->returnSelf());
  82. $page->expects($this->any())->method('getLayout')->will($this->returnValue($layout));
  83. /** @var \Magento\Framework\App\ViewInterface|\PHPUnit_Framework_MockObject_MockObject */
  84. $view = $this->createMock(\Magento\Framework\App\ViewInterface::class);
  85. $view->expects($this->any())->method('getLayout')->will($this->returnValue($layout));
  86. /** @var \Magento\Framework\Controller\ResultFactory|\PHPUnit_Framework_MockObject_MockObject */
  87. $resultFactory = $this->createMock(\Magento\Framework\Controller\ResultFactory::class);
  88. $resultFactory->expects($this->any())->method('create')->will($this->returnValue($page));
  89. $this->context = $this->createMock(\Magento\Backend\App\Action\Context::class);
  90. $this->context->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
  91. $this->context->expects($this->any())->method('getResponse')->will($this->returnValue($this->response));
  92. $this->context->expects($this->any())->method('getObjectManager')
  93. ->will($this->returnValue($objectManager));
  94. $this->context->expects($this->any())->method('getEventManager')->will($this->returnValue($eventManager));
  95. $this->context->expects($this->any())->method('getView')->will($this->returnValue($view));
  96. $this->context->expects($this->any())->method('getResultFactory')
  97. ->will($this->returnValue($resultFactory));
  98. $this->helperMock = $this->createMock(\Magento\Framework\Oauth\Helper\Request::class);
  99. $this->frameworkOauthSvcMock = $this->createMock(\Magento\Framework\Oauth\OauthInterface::class);
  100. $this->intOauthServiceMock = $this->createMock(\Magento\Integration\Api\OauthServiceInterface::class);
  101. $this->integrationServiceMock = $this->createMock(\Magento\Integration\Api\IntegrationServiceInterface::class);
  102. /** @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager $objectManagerHelper */
  103. $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  104. $this->accessAction = $this->objectManagerHelper->getObject(
  105. \Magento\Integration\Controller\Token\Access::class,
  106. [
  107. 'context' => $this->context,
  108. 'oauthService'=> $this->frameworkOauthSvcMock,
  109. 'intOauthService' => $this->intOauthServiceMock,
  110. 'integrationService' => $this->integrationServiceMock,
  111. 'helper' => $this->helperMock,
  112. ]
  113. );
  114. }
  115. /**
  116. * Test the basic Access action.
  117. */
  118. public function testAccessAction()
  119. {
  120. $this->request->expects($this->any())
  121. ->method('getMethod')
  122. ->willReturn('GET');
  123. $this->helperMock->expects($this->once())
  124. ->method('getRequestUrl');
  125. $this->helperMock->expects($this->once())
  126. ->method('prepareRequest');
  127. $this->frameworkOauthSvcMock->expects($this->once())
  128. ->method('getAccessToken')
  129. ->willReturn(['response']);
  130. /** @var \Magento\Integration\Model\Oauth\Consumer|\PHPUnit_Framework_MockObject_MockObject */
  131. $consumerMock = $this->createMock(\Magento\Integration\Model\Oauth\Consumer::class);
  132. $consumerMock->expects($this->once())
  133. ->method('getId');
  134. $this->intOauthServiceMock->expects($this->once())
  135. ->method('loadConsumerByKey')
  136. ->willReturn($consumerMock);
  137. /** @var \Magento\Integration\Model\Integration|\PHPUnit_Framework_MockObject_MockObject */
  138. $integrationMock = $this->createMock(\Magento\Integration\Model\Integration::class);
  139. $integrationMock->expects($this->once())
  140. ->method('save')
  141. ->willReturnSelf();
  142. $this->integrationServiceMock->expects($this->once())
  143. ->method('findByConsumerId')
  144. ->willReturn($integrationMock);
  145. $this->response->expects($this->once())
  146. ->method('setBody');
  147. $this->accessAction->execute();
  148. }
  149. }