18 class RestTest extends \PHPUnit\Framework\TestCase
68 private $storeManagerMock;
92 const SERVICE_ID = \Magento\Webapi\Controller\Rest::class;
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();
103 $this->_routeMock = $this->getRouteMock();
104 $this->_serviceMock = $this->getMockBuilder(self::SERVICE_ID)->setMethods([self::SERVICE_METHOD])
105 ->disableOriginalConstructor()->getMock();
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();
112 $paramsOverriderMock = $this->getMockBuilder(\
Magento\Webapi\Controller\Rest\ParamsOverrider::class)
113 ->setMethods([
'overrideParams'])
114 ->disableOriginalConstructor()->getMock();
116 $dataObjectProcessorMock = $this->getMockBuilder(\
Magento\Framework\Reflection\DataObjectProcessor::class)
117 ->disableOriginalConstructor()
118 ->setMethods([
'getMethodReturnType'])
119 ->getMockForAbstractClass();
121 $layoutMock = $this->getMockBuilder(\
Magento\Framework\View\LayoutInterface::class)
122 ->disableOriginalConstructor()->getMock();
124 $errorProcessorMock = $this->createMock(\
Magento\Framework\Webapi\ErrorProcessor::class);
125 $errorProcessorMock->expects($this->any())->method(
'maskException')->will($this->returnArgument(0));
127 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
129 $this->serviceInputProcessorMock = $this->getMockBuilder(\
Magento\Framework\Webapi\ServiceInputProcessor::class)
130 ->disableOriginalConstructor()->setMethods([
'process'])->getMock();
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();
140 $this->_restController =
142 \
Magento\Webapi\Controller\Rest::class,
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,
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));
165 $routerMock->expects($this->any())->method(
'match')->will($this->returnValue($this->_routeMock));
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));
171 $dataObjectProcessorMock->expects($this->any())->method(
'getMethodReturnType')
172 ->with(self::SERVICE_ID, self::SERVICE_METHOD)
173 ->will($this->returnValue(
'null'));
175 $paramsOverriderMock->expects($this->any())->method(
'overrideParams')->will($this->returnValue([]));
185 $this->_requestMock->expects($this->any())
186 ->method(
'getPathInfo')
187 ->willReturn(\
Magento\Webapi\Controller\Rest\SchemaRequestProcessor::PROCESSOR_PATH);
189 $this->_requestMock->expects($this->any())
190 ->method(
'getParams')
191 ->will($this->returnValue(
$params));
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);
197 $this->assertEquals(
$schema, $this->_responseMock->getBody());
205 $this->_requestMock->expects($this->any())
206 ->method(
'getPathInfo')
207 ->willReturn(\
Magento\Webapi\Controller\Rest\SchemaRequestProcessor::PROCESSOR_PATH);
208 $this->_requestMock->expects($this->any())
211 $this->returnValueMap([
213 \
Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES,
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'));
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);
230 $this->assertEquals(
$schema, $this->_responseMock->getBody());
236 private function getRequestProccessotPoolMock()
238 $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
240 $this->swaggerGeneratorMock = $this->getMockBuilder(\
Magento\Webapi\Model\Rest\Swagger\Generator::class)
241 ->disableOriginalConstructor()
242 ->setMethods([
'generate',
'getListOfServices'])
243 ->getMockForAbstractClass();
246 \
Magento\Webapi\Controller\Rest\SchemaRequestProcessor::class,
248 'swaggerGenerator' => $this->swaggerGeneratorMock,
249 'response' => $this->_responseMock,
253 $this->synchronousRequestProcessor =
254 $this->getMockBuilder(\
Magento\Webapi\Controller\Rest\SynchronousRequestProcessor::class)
255 ->setMethods([
'process'])
256 ->disableOriginalConstructor()
260 \
Magento\Webapi\Controller\Rest\RequestProcessorPool::class,
262 'requestProcessors' => [
263 'syncSchema' => $this->schemaRequestProcessor,
264 'sync' => $this->synchronousRequestProcessor,
273 private function getRouteMock()
275 return $this->getMockBuilder(\
Magento\Webapi\Controller\Rest\Router\Route::class)
283 ->disableOriginalConstructor()->getMock();
289 private function getRequestMock()
291 return $this->getMockBuilder(\
Magento\Framework\Webapi\Rest\Request::class)
298 'getRequestedServices',
303 )->disableOriginalConstructor()->getMock();
309 private function getResponseMock()
311 return $this->getMockBuilder(\
Magento\Framework\Webapi\Rest\Response::class)
312 ->setMethods([
'sendResponse',
'prepareResponse',
'setHeader'])
313 ->disableOriginalConstructor()
$serviceInputProcessorMock
testDispatchAllSchemaRequest()
$synchronousRequestProcessor
testDispatchSchemaRequest()
const REQUEST_PARAM_SERVICES
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]