7 declare(strict_types=1);
22 class RestTest extends \PHPUnit\Framework\TestCase
27 private $restController;
37 private $responseMock;
52 private $oauthServiceMock;
57 private $authorizationMock;
62 private $serviceInputProcessorMock;
67 private $swaggerGeneratorMock;
72 private $storeManagerMock;
83 private $asyncSchemaRequestProcessor;
89 private $asyncRequestProcessor;
94 private $requestProcessorPool;
98 const SERVICE_ID = \Magento\Webapi\Controller\Rest::class;
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();
109 $this->routeMock = $this->getRouteMock();
110 $this->serviceMock = $this->getMockBuilder(self::SERVICE_ID)->setMethods([self::SERVICE_METHOD])
111 ->disableOriginalConstructor()->getMock();
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();
118 $paramsOverriderMock = $this->getMockBuilder(\
Magento\Webapi\Controller\Rest\ParamsOverrider::class)
119 ->setMethods([
'overrideParams'])
120 ->disableOriginalConstructor()->getMock();
122 $dataObjectProcessorMock = $this->getMockBuilder(\
Magento\Framework\Reflection\DataObjectProcessor::class)
123 ->disableOriginalConstructor()
124 ->setMethods([
'getMethodReturnType'])
125 ->getMockForAbstractClass();
127 $layoutMock = $this->getMockBuilder(\
Magento\Framework\View\LayoutInterface::class)
128 ->disableOriginalConstructor()->getMock();
130 $errorProcessorMock = $this->createMock(\
Magento\Framework\Webapi\ErrorProcessor::class);
131 $errorProcessorMock->expects($this->any())->method(
'maskException')->will($this->returnArgument(0));
133 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
135 $this->serviceInputProcessorMock = $this->getMockBuilder(\
Magento\Framework\Webapi\ServiceInputProcessor::class)
136 ->disableOriginalConstructor()->setMethods([
'process'])->getMock();
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();
146 $this->restController =
148 \
Magento\Webapi\Controller\Rest::class,
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,
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));
171 $routerMock->expects($this->any())->method(
'match')->will($this->returnValue($this->routeMock));
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));
177 $dataObjectProcessorMock->expects($this->any())->method(
'getMethodReturnType')
178 ->with(self::SERVICE_ID, self::SERVICE_METHOD)
179 ->will($this->returnValue(
'null'));
181 $paramsOverriderMock->expects($this->any())->method(
'overrideParams')->will($this->returnValue([]));
191 $this->requestMock->expects($this->any())
192 ->method(
'getPathInfo')
195 $this->requestMock->expects($this->any())
196 ->method(
'getParams')
197 ->will($this->returnValue(
$params));
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);
203 $this->assertEquals(
$schema, $this->responseMock->getBody());
211 $this->requestMock->expects($this->any())
212 ->method(
'getPathInfo')
214 $this->requestMock->expects($this->any())
217 $this->returnValueMap([
219 \
Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES,
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'));
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);
236 $this->assertEquals(
$schema, $this->responseMock->getBody());
242 private function getRequestProccessotPoolMock()
244 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
246 $this->swaggerGeneratorMock = $this->getMockBuilder(\
Magento\Webapi\Model\Rest\Swagger\Generator::class)
247 ->disableOriginalConstructor()
248 ->setMethods([
'generate',
'getListOfServices'])
249 ->getMockForAbstractClass();
252 \
Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor::class,
254 'swaggerGenerator' => $this->swaggerGeneratorMock,
255 'response' => $this->responseMock,
259 $this->asyncRequestProcessor =
260 $this->getMockBuilder(\
Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor::class)
261 ->setMethods([
'process'])
262 ->disableOriginalConstructor()
266 \
Magento\Webapi\Controller\Rest\RequestProcessorPool::class,
268 'requestProcessors' => [
269 'asyncSchema' => $this->asyncSchemaRequestProcessor,
270 'async' => $this->asyncRequestProcessor,
279 private function getRouteMock()
281 return $this->getMockBuilder(\
Magento\Webapi\Controller\Rest\Router\Route::class)
289 ->disableOriginalConstructor()->getMock();
295 private function getRequestMock()
297 return $this->getMockBuilder(\
Magento\Framework\Webapi\Rest\Request::class)
304 'getRequestedServices',
309 )->disableOriginalConstructor()->getMock();
315 private function getResponseMock()
317 return $this->getMockBuilder(\
Magento\Framework\Webapi\Rest\Response::class)
318 ->setMethods([
'sendResponse',
'prepareResponse',
'setHeader'])
319 ->disableOriginalConstructor()
const REQUEST_PARAM_SERVICES
testDispatchAllSchemaRequest()
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
testDispatchSchemaRequest()