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
7 declare(strict_types=1);
8 
10 
15 
22 class RestTest extends \PHPUnit\Framework\TestCase
23 {
27  private $restController;
28 
32  private $requestMock;
33 
37  private $responseMock;
38 
42  private $routeMock;
43 
47  private $serviceMock;
48 
52  private $oauthServiceMock;
53 
57  private $authorizationMock;
58 
62  private $serviceInputProcessorMock;
63 
67  private $swaggerGeneratorMock;
68 
72  private $storeManagerMock;
73 
77  private $storeMock;
78 
83  private $asyncSchemaRequestProcessor;
84 
89  private $asyncRequestProcessor;
90 
94  private $requestProcessorPool;
95 
96  const SERVICE_METHOD = 'testMethod';
97 
98  const SERVICE_ID = \Magento\Webapi\Controller\Rest::class;
99 
100  protected function setUp()
101  {
102  $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
103  $this->requestMock = $this->getRequestMock();
104  $this->requestMock->expects($this->any())->method('getHttpHost')->willReturn('testHostName.com');
105  $this->responseMock = $this->getResponseMock();
106  $routerMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router::class)->setMethods(['match'])
107  ->disableOriginalConstructor()->getMock();
108 
109  $this->routeMock = $this->getRouteMock();
110  $this->serviceMock = $this->getMockBuilder(self::SERVICE_ID)->setMethods([self::SERVICE_METHOD])
111  ->disableOriginalConstructor()->getMock();
112 
113  $this->oauthServiceMock = $this->getMockBuilder(\Magento\Framework\Oauth\OauthInterface::class)
114  ->setMethods(['validateAccessTokenRequest'])->getMockForAbstractClass();
115  $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\Webapi\Authorization::class)
116  ->disableOriginalConstructor()->getMock();
117 
118  $paramsOverriderMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\ParamsOverrider::class)
119  ->setMethods(['overrideParams'])
120  ->disableOriginalConstructor()->getMock();
121 
122  $dataObjectProcessorMock = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
123  ->disableOriginalConstructor()
124  ->setMethods(['getMethodReturnType'])
125  ->getMockForAbstractClass();
126 
127  $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
128  ->disableOriginalConstructor()->getMock();
129 
130  $errorProcessorMock = $this->createMock(\Magento\Framework\Webapi\ErrorProcessor::class);
131  $errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnArgument(0));
132 
133  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
134 
135  $this->serviceInputProcessorMock = $this->getMockBuilder(\Magento\Framework\Webapi\ServiceInputProcessor::class)
136  ->disableOriginalConstructor()->setMethods(['process'])->getMock();
137 
138  $areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
139  $areaMock = $this->createMock(\Magento\Framework\App\AreaInterface::class);
140  $areaListMock->expects($this->any())->method('getArea')->will($this->returnValue($areaMock));
141  $this->storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
142  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
143  $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($this->storeMock);
144  $this->requestProcessorPool = $this->getRequestProccessotPoolMock();
145 
146  $this->restController =
147  $objectManager->getObject(
148  \Magento\Webapi\Controller\Rest::class,
149  [
150  'request' => $this->requestMock,
151  'response' => $this->responseMock,
152  'router' => $routerMock,
153  'objectManager' => $objectManagerMock,
154  'layout' => $layoutMock,
155  'oauthService' => $this->oauthServiceMock,
156  'authorization' => $this->authorizationMock,
157  'serviceInputProcessor' => $this->serviceInputProcessorMock,
158  'errorProcessor' => $errorProcessorMock,
159  'areaList' => $areaListMock,
160  'paramsOverrider' => $paramsOverriderMock,
161  'dataObjectProcessor' => $dataObjectProcessorMock,
162  'storeManager' => $this->storeManagerMock,
163  'requestProcessorPool' => $this->requestProcessorPool,
164  ]
165  );
166 
167  $this->routeMock->expects($this->any())->method('getServiceClass')->will($this->returnValue(self::SERVICE_ID));
168  $this->routeMock->expects($this->any())->method('getServiceMethod')
169  ->will($this->returnValue(self::SERVICE_METHOD));
170 
171  $routerMock->expects($this->any())->method('match')->will($this->returnValue($this->routeMock));
172 
173  $objectManagerMock->expects($this->any())->method('get')->will($this->returnValue($this->serviceMock));
174  $this->responseMock->expects($this->any())->method('prepareResponse')->will($this->returnValue([]));
175  $this->serviceMock->expects($this->any())->method(self::SERVICE_METHOD)->will($this->returnValue(null));
176 
177  $dataObjectProcessorMock->expects($this->any())->method('getMethodReturnType')
178  ->with(self::SERVICE_ID, self::SERVICE_METHOD)
179  ->will($this->returnValue('null'));
180 
181  $paramsOverriderMock->expects($this->any())->method('overrideParams')->will($this->returnValue([]));
182 
183  parent::setUp();
184  }
185 
186  public function testDispatchSchemaRequest()
187  {
188  $params = [
190  ];
191  $this->requestMock->expects($this->any())
192  ->method('getPathInfo')
194 
195  $this->requestMock->expects($this->any())
196  ->method('getParams')
197  ->will($this->returnValue($params));
198 
199  $schema = 'Some REST schema content';
200  $this->swaggerGeneratorMock->expects($this->any())->method('generate')->willReturn($schema);
201  $this->requestProcessorPool->getProcessor($this->requestMock)->process($this->requestMock);
202 
203  $this->assertEquals($schema, $this->responseMock->getBody());
204  }
205 
207  {
208  $params = [
210  ];
211  $this->requestMock->expects($this->any())
212  ->method('getPathInfo')
214  $this->requestMock->expects($this->any())
215  ->method('getParam')
216  ->will(
217  $this->returnValueMap([
218  [
219  \Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES,
220  null,
221  'all',
222  ],
223  ])
224  );
225  $this->requestMock->expects($this->any())
226  ->method('getParams')
227  ->will($this->returnValue($params));
228  $this->requestMock->expects($this->any())
229  ->method('getRequestedServices')
230  ->will($this->returnValue('all'));
231 
232  $schema = 'Some REST schema content';
233  $this->swaggerGeneratorMock->expects($this->any())->method('generate')->willReturn($schema);
234  $this->requestProcessorPool->getProcessor($this->requestMock)->process($this->requestMock);
235 
236  $this->assertEquals($schema, $this->responseMock->getBody());
237  }
238 
242  private function getRequestProccessotPoolMock()
243  {
244  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
245 
246  $this->swaggerGeneratorMock = $this->getMockBuilder(\Magento\Webapi\Model\Rest\Swagger\Generator::class)
247  ->disableOriginalConstructor()
248  ->setMethods(['generate', 'getListOfServices'])
249  ->getMockForAbstractClass();
250 
251  $this->asyncSchemaRequestProcessor = $objectManager->getObject(
252  \Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor::class,
253  [
254  'swaggerGenerator' => $this->swaggerGeneratorMock,
255  'response' => $this->responseMock,
256  ]
257  );
258 
259  $this->asyncRequestProcessor =
260  $this->getMockBuilder(\Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor::class)
261  ->setMethods(['process'])
262  ->disableOriginalConstructor()
263  ->getMock();
264 
265  return $objectManager->getObject(
266  \Magento\Webapi\Controller\Rest\RequestProcessorPool::class,
267  [
268  'requestProcessors' => [
269  'asyncSchema' => $this->asyncSchemaRequestProcessor,
270  'async' => $this->asyncRequestProcessor,
271  ],
272  ]
273  );
274  }
275 
279  private function getRouteMock()
280  {
281  return $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router\Route::class)
282  ->setMethods([
283  'isSecure',
284  'getServiceMethod',
285  'getServiceClass',
286  'getAclResources',
287  'getParameters',
288  ])
289  ->disableOriginalConstructor()->getMock();
290  }
291 
295  private function getRequestMock()
296  {
297  return $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Request::class)
298  ->setMethods(
299  [
300  'isSecure',
301  'getRequestData',
302  'getParams',
303  'getParam',
304  'getRequestedServices',
305  'getPathInfo',
306  'getHttpHost',
307  'getMethod',
308  ]
309  )->disableOriginalConstructor()->getMock();
310  }
311 
315  private function getResponseMock()
316  {
317  return $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Response::class)
318  ->setMethods(['sendResponse', 'prepareResponse', 'setHeader'])
319  ->disableOriginalConstructor()
320  ->getMock();
321  }
322 }
$objectManager
Definition: bootstrap.php:17
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18