Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RestTest.php
Go to the documentation of this file.
1 <?php
8 
11 
18 class RestTest extends \PHPUnit\Framework\TestCase
19 {
23  protected $_restController;
24 
28  protected $_requestMock;
29 
33  protected $_responseMock;
34 
38  protected $_routeMock;
39 
43  protected $_serviceMock;
44 
48  protected $_oauthServiceMock;
49 
54 
59 
64 
68  private $storeManagerMock;
69 
73  private $storeMock;
74 
79 
84 
89 
90  const SERVICE_METHOD = 'testMethod';
91 
92  const SERVICE_ID = \Magento\Webapi\Controller\Rest::class;
93 
94  protected function setUp()
95  {
96  $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
97  $this->_requestMock = $this->getRequestMock();
98  $this->_requestMock->expects($this->any())->method('getHttpHost')->willReturn('testHostName.com');
99  $this->_responseMock = $this->getResponseMock();
100  $routerMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router::class)->setMethods(['match'])
101  ->disableOriginalConstructor()->getMock();
102 
103  $this->_routeMock = $this->getRouteMock();
104  $this->_serviceMock = $this->getMockBuilder(self::SERVICE_ID)->setMethods([self::SERVICE_METHOD])
105  ->disableOriginalConstructor()->getMock();
106 
107  $this->_oauthServiceMock = $this->getMockBuilder(\Magento\Framework\Oauth\OauthInterface::class)
108  ->setMethods(['validateAccessTokenRequest'])->getMockForAbstractClass();
109  $this->_authorizationMock = $this->getMockBuilder(\Magento\Framework\Webapi\Authorization::class)
110  ->disableOriginalConstructor()->getMock();
111 
112  $paramsOverriderMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\ParamsOverrider::class)
113  ->setMethods(['overrideParams'])
114  ->disableOriginalConstructor()->getMock();
115 
116  $dataObjectProcessorMock = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
117  ->disableOriginalConstructor()
118  ->setMethods(['getMethodReturnType'])
119  ->getMockForAbstractClass();
120 
121  $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
122  ->disableOriginalConstructor()->getMock();
123 
124  $errorProcessorMock = $this->createMock(\Magento\Framework\Webapi\ErrorProcessor::class);
125  $errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnArgument(0));
126 
127  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
128 
129  $this->serviceInputProcessorMock = $this->getMockBuilder(\Magento\Framework\Webapi\ServiceInputProcessor::class)
130  ->disableOriginalConstructor()->setMethods(['process'])->getMock();
131 
132  $areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
133  $areaMock = $this->createMock(\Magento\Framework\App\AreaInterface::class);
134  $areaListMock->expects($this->any())->method('getArea')->will($this->returnValue($areaMock));
135  $this->storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
136  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
137  $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($this->storeMock);
138  $this->requestProcessorPool = $this->getRequestProccessotPoolMock();
139 
140  $this->_restController =
141  $objectManager->getObject(
142  \Magento\Webapi\Controller\Rest::class,
143  [
144  'request' => $this->_requestMock,
145  'response' => $this->_responseMock,
146  'router' => $routerMock,
147  'objectManager' => $objectManagerMock,
148  'layout' => $layoutMock,
149  'oauthService' => $this->_oauthServiceMock,
150  'authorization' => $this->_authorizationMock,
151  'serviceInputProcessor' => $this->serviceInputProcessorMock,
152  'errorProcessor' => $errorProcessorMock,
153  'areaList' => $areaListMock,
154  'paramsOverrider' => $paramsOverriderMock,
155  'dataObjectProcessor' => $dataObjectProcessorMock,
156  'storeManager' => $this->storeManagerMock,
157  'requestProcessorPool' => $this->requestProcessorPool,
158  ]
159  );
160 
161  $this->_routeMock->expects($this->any())->method('getServiceClass')->will($this->returnValue(self::SERVICE_ID));
162  $this->_routeMock->expects($this->any())->method('getServiceMethod')
163  ->will($this->returnValue(self::SERVICE_METHOD));
164 
165  $routerMock->expects($this->any())->method('match')->will($this->returnValue($this->_routeMock));
166 
167  $objectManagerMock->expects($this->any())->method('get')->will($this->returnValue($this->_serviceMock));
168  $this->_responseMock->expects($this->any())->method('prepareResponse')->will($this->returnValue([]));
169  $this->_serviceMock->expects($this->any())->method(self::SERVICE_METHOD)->will($this->returnValue(null));
170 
171  $dataObjectProcessorMock->expects($this->any())->method('getMethodReturnType')
172  ->with(self::SERVICE_ID, self::SERVICE_METHOD)
173  ->will($this->returnValue('null'));
174 
175  $paramsOverriderMock->expects($this->any())->method('overrideParams')->will($this->returnValue([]));
176 
177  parent::setUp();
178  }
179 
180  public function testDispatchSchemaRequest()
181  {
182  $params = [
184  ];
185  $this->_requestMock->expects($this->any())
186  ->method('getPathInfo')
187  ->willReturn(\Magento\Webapi\Controller\Rest\SchemaRequestProcessor::PROCESSOR_PATH);
188 
189  $this->_requestMock->expects($this->any())
190  ->method('getParams')
191  ->will($this->returnValue($params));
192 
193  $schema = 'Some REST schema content';
194  $this->swaggerGeneratorMock->expects($this->any())->method('generate')->willReturn($schema);
195  $this->requestProcessorPool->getProcessor($this->_requestMock)->process($this->_requestMock);
196 
197  $this->assertEquals($schema, $this->_responseMock->getBody());
198  }
199 
201  {
202  $params = [
204  ];
205  $this->_requestMock->expects($this->any())
206  ->method('getPathInfo')
207  ->willReturn(\Magento\Webapi\Controller\Rest\SchemaRequestProcessor::PROCESSOR_PATH);
208  $this->_requestMock->expects($this->any())
209  ->method('getParam')
210  ->will(
211  $this->returnValueMap([
212  [
213  \Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES,
214  null,
215  'all',
216  ],
217  ])
218  );
219  $this->_requestMock->expects($this->any())
220  ->method('getParams')
221  ->will($this->returnValue($params));
222  $this->_requestMock->expects($this->any())
223  ->method('getRequestedServices')
224  ->will($this->returnValue('all'));
225 
226  $schema = 'Some REST schema content';
227  $this->swaggerGeneratorMock->expects($this->any())->method('generate')->willReturn($schema);
228  $this->requestProcessorPool->getProcessor($this->_requestMock)->process($this->_requestMock);
229 
230  $this->assertEquals($schema, $this->_responseMock->getBody());
231  }
232 
236  private function getRequestProccessotPoolMock()
237  {
238  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
239 
240  $this->swaggerGeneratorMock = $this->getMockBuilder(\Magento\Webapi\Model\Rest\Swagger\Generator::class)
241  ->disableOriginalConstructor()
242  ->setMethods(['generate', 'getListOfServices'])
243  ->getMockForAbstractClass();
244 
245  $this->schemaRequestProcessor = $objectManager->getObject(
246  \Magento\Webapi\Controller\Rest\SchemaRequestProcessor::class,
247  [
248  'swaggerGenerator' => $this->swaggerGeneratorMock,
249  'response' => $this->_responseMock,
250  ]
251  );
252 
253  $this->synchronousRequestProcessor =
254  $this->getMockBuilder(\Magento\Webapi\Controller\Rest\SynchronousRequestProcessor::class)
255  ->setMethods(['process'])
256  ->disableOriginalConstructor()
257  ->getMock();
258 
259  return $objectManager->getObject(
260  \Magento\Webapi\Controller\Rest\RequestProcessorPool::class,
261  [
262  'requestProcessors' => [
263  'syncSchema' => $this->schemaRequestProcessor,
264  'sync' => $this->synchronousRequestProcessor,
265  ],
266  ]
267  );
268  }
269 
273  private function getRouteMock()
274  {
275  return $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router\Route::class)
276  ->setMethods([
277  'isSecure',
278  'getServiceMethod',
279  'getServiceClass',
280  'getAclResources',
281  'getParameters',
282  ])
283  ->disableOriginalConstructor()->getMock();
284  }
285 
289  private function getRequestMock()
290  {
291  return $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Request::class)
292  ->setMethods(
293  [
294  'isSecure',
295  'getRequestData',
296  'getParams',
297  'getParam',
298  'getRequestedServices',
299  'getPathInfo',
300  'getHttpHost',
301  'getMethod',
302  ]
303  )->disableOriginalConstructor()->getMock();
304  }
305 
309  private function getResponseMock()
310  {
311  return $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Response::class)
312  ->setMethods(['sendResponse', 'prepareResponse', 'setHeader'])
313  ->disableOriginalConstructor()
314  ->getMock();
315  }
316 }
$objectManager
Definition: bootstrap.php:17
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18