RestTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\WebapiAsync\Test\Unit\Controller;
  8. use Magento\Authorization\Model\UserContextInterface;
  9. use Magento\Framework\Exception\AuthorizationException;
  10. use Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor;
  11. use Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor;
  12. /**
  13. * Test Rest controller.
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. * @SuppressWarnings(PHPMD.TooManyFields)
  17. */
  18. class RestTest extends \PHPUnit\Framework\TestCase
  19. {
  20. /**
  21. * @var \Magento\Webapi\Controller\Rest
  22. */
  23. private $restController;
  24. /**
  25. * @var \Magento\Framework\Webapi\Rest\Request|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $requestMock;
  28. /**
  29. * @var \Magento\Framework\Webapi\Rest\Response|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $responseMock;
  32. /**
  33. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Webapi\Controller\Rest\Router\Route
  34. */
  35. private $routeMock;
  36. /**
  37. * @var \stdClass|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $serviceMock;
  40. /**
  41. * @var \Magento\Framework\Oauth\OauthInterface|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $oauthServiceMock;
  44. /**
  45. * @var \Magento\Framework\Webapi\Authorization|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $authorizationMock;
  48. /**
  49. * @var \Magento\Framework\Webapi\ServiceInputProcessor|\PHPUnit_Framework_MockObject_MockObject
  50. */
  51. private $serviceInputProcessorMock;
  52. /**
  53. * @var \Magento\Webapi\Model\Rest\Swagger\Generator | \PHPUnit_Framework_MockObject_MockObject
  54. */
  55. private $swaggerGeneratorMock;
  56. /**
  57. * @var \Magento\Store\Model\StoreManagerInterface | \PHPUnit_Framework_MockObject_MockObject
  58. */
  59. private $storeManagerMock;
  60. /**
  61. * @var \Magento\Store\Api\Data\StoreInterface | \PHPUnit_Framework_MockObject_MockObject
  62. */
  63. private $storeMock;
  64. /**
  65. * @var \Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor |
  66. * \PHPUnit_Framework_MockObject_MockObject
  67. */
  68. private $asyncSchemaRequestProcessor;
  69. /**
  70. * @var \Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor |
  71. * \PHPUnit_Framework_MockObject_MockObject
  72. */
  73. private $asyncRequestProcessor;
  74. /**
  75. * @var \Magento\Webapi\Controller\Rest\RequestProcessorPool | \PHPUnit_Framework_MockObject_MockObject
  76. */
  77. private $requestProcessorPool;
  78. const SERVICE_METHOD = 'testMethod';
  79. const SERVICE_ID = \Magento\Webapi\Controller\Rest::class;
  80. protected function setUp()
  81. {
  82. $objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  83. $this->requestMock = $this->getRequestMock();
  84. $this->requestMock->expects($this->any())->method('getHttpHost')->willReturn('testHostName.com');
  85. $this->responseMock = $this->getResponseMock();
  86. $routerMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router::class)->setMethods(['match'])
  87. ->disableOriginalConstructor()->getMock();
  88. $this->routeMock = $this->getRouteMock();
  89. $this->serviceMock = $this->getMockBuilder(self::SERVICE_ID)->setMethods([self::SERVICE_METHOD])
  90. ->disableOriginalConstructor()->getMock();
  91. $this->oauthServiceMock = $this->getMockBuilder(\Magento\Framework\Oauth\OauthInterface::class)
  92. ->setMethods(['validateAccessTokenRequest'])->getMockForAbstractClass();
  93. $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\Webapi\Authorization::class)
  94. ->disableOriginalConstructor()->getMock();
  95. $paramsOverriderMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\ParamsOverrider::class)
  96. ->setMethods(['overrideParams'])
  97. ->disableOriginalConstructor()->getMock();
  98. $dataObjectProcessorMock = $this->getMockBuilder(\Magento\Framework\Reflection\DataObjectProcessor::class)
  99. ->disableOriginalConstructor()
  100. ->setMethods(['getMethodReturnType'])
  101. ->getMockForAbstractClass();
  102. $layoutMock = $this->getMockBuilder(\Magento\Framework\View\LayoutInterface::class)
  103. ->disableOriginalConstructor()->getMock();
  104. $errorProcessorMock = $this->createMock(\Magento\Framework\Webapi\ErrorProcessor::class);
  105. $errorProcessorMock->expects($this->any())->method('maskException')->will($this->returnArgument(0));
  106. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  107. $this->serviceInputProcessorMock = $this->getMockBuilder(\Magento\Framework\Webapi\ServiceInputProcessor::class)
  108. ->disableOriginalConstructor()->setMethods(['process'])->getMock();
  109. $areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
  110. $areaMock = $this->createMock(\Magento\Framework\App\AreaInterface::class);
  111. $areaListMock->expects($this->any())->method('getArea')->will($this->returnValue($areaMock));
  112. $this->storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
  113. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  114. $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($this->storeMock);
  115. $this->requestProcessorPool = $this->getRequestProccessotPoolMock();
  116. $this->restController =
  117. $objectManager->getObject(
  118. \Magento\Webapi\Controller\Rest::class,
  119. [
  120. 'request' => $this->requestMock,
  121. 'response' => $this->responseMock,
  122. 'router' => $routerMock,
  123. 'objectManager' => $objectManagerMock,
  124. 'layout' => $layoutMock,
  125. 'oauthService' => $this->oauthServiceMock,
  126. 'authorization' => $this->authorizationMock,
  127. 'serviceInputProcessor' => $this->serviceInputProcessorMock,
  128. 'errorProcessor' => $errorProcessorMock,
  129. 'areaList' => $areaListMock,
  130. 'paramsOverrider' => $paramsOverriderMock,
  131. 'dataObjectProcessor' => $dataObjectProcessorMock,
  132. 'storeManager' => $this->storeManagerMock,
  133. 'requestProcessorPool' => $this->requestProcessorPool,
  134. ]
  135. );
  136. $this->routeMock->expects($this->any())->method('getServiceClass')->will($this->returnValue(self::SERVICE_ID));
  137. $this->routeMock->expects($this->any())->method('getServiceMethod')
  138. ->will($this->returnValue(self::SERVICE_METHOD));
  139. $routerMock->expects($this->any())->method('match')->will($this->returnValue($this->routeMock));
  140. $objectManagerMock->expects($this->any())->method('get')->will($this->returnValue($this->serviceMock));
  141. $this->responseMock->expects($this->any())->method('prepareResponse')->will($this->returnValue([]));
  142. $this->serviceMock->expects($this->any())->method(self::SERVICE_METHOD)->will($this->returnValue(null));
  143. $dataObjectProcessorMock->expects($this->any())->method('getMethodReturnType')
  144. ->with(self::SERVICE_ID, self::SERVICE_METHOD)
  145. ->will($this->returnValue('null'));
  146. $paramsOverriderMock->expects($this->any())->method('overrideParams')->will($this->returnValue([]));
  147. parent::setUp();
  148. }
  149. public function testDispatchSchemaRequest()
  150. {
  151. $params = [
  152. \Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES => 'foo',
  153. ];
  154. $this->requestMock->expects($this->any())
  155. ->method('getPathInfo')
  156. ->willReturn(AsynchronousSchemaRequestProcessor::PROCESSOR_PATH);
  157. $this->requestMock->expects($this->any())
  158. ->method('getParams')
  159. ->will($this->returnValue($params));
  160. $schema = 'Some REST schema content';
  161. $this->swaggerGeneratorMock->expects($this->any())->method('generate')->willReturn($schema);
  162. $this->requestProcessorPool->getProcessor($this->requestMock)->process($this->requestMock);
  163. $this->assertEquals($schema, $this->responseMock->getBody());
  164. }
  165. public function testDispatchAllSchemaRequest()
  166. {
  167. $params = [
  168. \Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES => 'all',
  169. ];
  170. $this->requestMock->expects($this->any())
  171. ->method('getPathInfo')
  172. ->willReturn(AsynchronousSchemaRequestProcessor::PROCESSOR_PATH);
  173. $this->requestMock->expects($this->any())
  174. ->method('getParam')
  175. ->will(
  176. $this->returnValueMap([
  177. [
  178. \Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES,
  179. null,
  180. 'all',
  181. ],
  182. ])
  183. );
  184. $this->requestMock->expects($this->any())
  185. ->method('getParams')
  186. ->will($this->returnValue($params));
  187. $this->requestMock->expects($this->any())
  188. ->method('getRequestedServices')
  189. ->will($this->returnValue('all'));
  190. $schema = 'Some REST schema content';
  191. $this->swaggerGeneratorMock->expects($this->any())->method('generate')->willReturn($schema);
  192. $this->requestProcessorPool->getProcessor($this->requestMock)->process($this->requestMock);
  193. $this->assertEquals($schema, $this->responseMock->getBody());
  194. }
  195. /**
  196. * @return object|\Magento\Webapi\Controller\Rest\RequestProcessorPool
  197. */
  198. private function getRequestProccessotPoolMock()
  199. {
  200. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  201. $this->swaggerGeneratorMock = $this->getMockBuilder(\Magento\Webapi\Model\Rest\Swagger\Generator::class)
  202. ->disableOriginalConstructor()
  203. ->setMethods(['generate', 'getListOfServices'])
  204. ->getMockForAbstractClass();
  205. $this->asyncSchemaRequestProcessor = $objectManager->getObject(
  206. \Magento\WebapiAsync\Controller\Rest\AsynchronousSchemaRequestProcessor::class,
  207. [
  208. 'swaggerGenerator' => $this->swaggerGeneratorMock,
  209. 'response' => $this->responseMock,
  210. ]
  211. );
  212. $this->asyncRequestProcessor =
  213. $this->getMockBuilder(\Magento\WebapiAsync\Controller\Rest\AsynchronousRequestProcessor::class)
  214. ->setMethods(['process'])
  215. ->disableOriginalConstructor()
  216. ->getMock();
  217. return $objectManager->getObject(
  218. \Magento\Webapi\Controller\Rest\RequestProcessorPool::class,
  219. [
  220. 'requestProcessors' => [
  221. 'asyncSchema' => $this->asyncSchemaRequestProcessor,
  222. 'async' => $this->asyncRequestProcessor,
  223. ],
  224. ]
  225. );
  226. }
  227. /**
  228. * @return \Magento\Webapi\Controller\Rest\Router\Route | \PHPUnit_Framework_MockObject_MockObject
  229. */
  230. private function getRouteMock()
  231. {
  232. return $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router\Route::class)
  233. ->setMethods([
  234. 'isSecure',
  235. 'getServiceMethod',
  236. 'getServiceClass',
  237. 'getAclResources',
  238. 'getParameters',
  239. ])
  240. ->disableOriginalConstructor()->getMock();
  241. }
  242. /**
  243. * @return \Magento\Framework\Webapi\Rest\Request|\PHPUnit_Framework_MockObject_MockObject
  244. */
  245. private function getRequestMock()
  246. {
  247. return $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Request::class)
  248. ->setMethods(
  249. [
  250. 'isSecure',
  251. 'getRequestData',
  252. 'getParams',
  253. 'getParam',
  254. 'getRequestedServices',
  255. 'getPathInfo',
  256. 'getHttpHost',
  257. 'getMethod',
  258. ]
  259. )->disableOriginalConstructor()->getMock();
  260. }
  261. /**
  262. * @return \Magento\Framework\Webapi\Rest\Response|\PHPUnit_Framework_MockObject_MockObject
  263. */
  264. private function getResponseMock()
  265. {
  266. return $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Response::class)
  267. ->setMethods(['sendResponse', 'prepareResponse', 'setHeader'])
  268. ->disableOriginalConstructor()
  269. ->getMock();
  270. }
  271. }