Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AccessTest.php
Go to the documentation of this file.
1 <?php
8 
12 class AccessTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $request;
18 
22  protected $response;
23 
27  protected $context;
28 
33 
38 
43 
48 
52  protected $helperMock;
53 
57  protected $accessAction;
58 
59  protected function setUp()
60  {
61  $this->request = $this->createPartialMock(\Magento\Framework\App\RequestInterface::class, [
62  'getMethod',
63  'getModuleName',
64  'setModuleName',
65  'getActionName',
66  'setActionName',
67  'getParam',
68  'setParams',
69  'getParams',
70  'getCookie',
71  'isSecure'
72  ]);
73  $this->response = $this->createMock(\Magento\Framework\App\Console\Response::class);
75  $objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
77  $eventManager = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
79  $update = $this->createMock(\Magento\Framework\View\Layout\ProcessorInterface::class);
81  $layout = $this->createMock(\Magento\Framework\View\Layout::class);
82  $layout->expects($this->any())->method('getUpdate')->will($this->returnValue($update));
84  $pageConfig = $this->createMock(\Magento\Framework\View\Page\Config::class);
85  $pageConfig->expects($this->any())->method('addBodyClass')->will($this->returnSelf());
87  $page = $this->createPartialMock(
88  \Magento\Framework\View\Result\Page::class,
89  ['getConfig', 'initLayout', 'addPageLayoutHandles', 'getLayout']
90  );
91  $page->expects($this->any())->method('getConfig')->will($this->returnValue($pageConfig));
92  $page->expects($this->any())->method('addPageLayoutHandles')->will($this->returnSelf());
93  $page->expects($this->any())->method('getLayout')->will($this->returnValue($layout));
94 
96  $view = $this->createMock(\Magento\Framework\App\ViewInterface::class);
97  $view->expects($this->any())->method('getLayout')->will($this->returnValue($layout));
98 
100  $resultFactory = $this->createMock(\Magento\Framework\Controller\ResultFactory::class);
101  $resultFactory->expects($this->any())->method('create')->will($this->returnValue($page));
102 
103  $this->context = $this->createMock(\Magento\Backend\App\Action\Context::class);
104  $this->context->expects($this->any())->method('getRequest')->will($this->returnValue($this->request));
105  $this->context->expects($this->any())->method('getResponse')->will($this->returnValue($this->response));
106  $this->context->expects($this->any())->method('getObjectManager')
107  ->will($this->returnValue($objectManager));
108  $this->context->expects($this->any())->method('getEventManager')->will($this->returnValue($eventManager));
109  $this->context->expects($this->any())->method('getView')->will($this->returnValue($view));
110  $this->context->expects($this->any())->method('getResultFactory')
111  ->will($this->returnValue($resultFactory));
112 
113  $this->helperMock = $this->createMock(\Magento\Framework\Oauth\Helper\Request::class);
114  $this->frameworkOauthSvcMock = $this->createMock(\Magento\Framework\Oauth\OauthInterface::class);
115  $this->intOauthServiceMock = $this->createMock(\Magento\Integration\Api\OauthServiceInterface::class);
116  $this->integrationServiceMock = $this->createMock(\Magento\Integration\Api\IntegrationServiceInterface::class);
118  $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
119  $this->accessAction = $this->objectManagerHelper->getObject(
120  \Magento\Integration\Controller\Token\Access::class,
121  [
122  'context' => $this->context,
123  'oauthService'=> $this->frameworkOauthSvcMock,
124  'intOauthService' => $this->intOauthServiceMock,
125  'integrationService' => $this->integrationServiceMock,
126  'helper' => $this->helperMock,
127  ]
128  );
129  }
130 
134  public function testAccessAction()
135  {
136  $this->request->expects($this->any())
137  ->method('getMethod')
138  ->willReturn('GET');
139  $this->helperMock->expects($this->once())
140  ->method('getRequestUrl');
141  $this->helperMock->expects($this->once())
142  ->method('prepareRequest');
143  $this->frameworkOauthSvcMock->expects($this->once())
144  ->method('getAccessToken')
145  ->willReturn(['response']);
147  $consumerMock = $this->createMock(\Magento\Integration\Model\Oauth\Consumer::class);
148  $consumerMock->expects($this->once())
149  ->method('getId');
150  $this->intOauthServiceMock->expects($this->once())
151  ->method('loadConsumerByKey')
152  ->willReturn($consumerMock);
154  $integrationMock = $this->createMock(\Magento\Integration\Model\Integration::class);
155  $integrationMock->expects($this->once())
156  ->method('save')
157  ->willReturnSelf();
158  $this->integrationServiceMock->expects($this->once())
159  ->method('findByConsumerId')
160  ->willReturn($integrationMock);
161  $this->response->expects($this->once())
162  ->method('setBody');
163 
164  $this->accessAction->execute();
165  }
166 }
$objectManager
Definition: bootstrap.php:17
$page
Definition: pages.php:8