SoapTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * Test SOAP controller class.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Webapi\Test\Unit\Controller;
  9. /**
  10. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  11. */
  12. class SoapTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Webapi\Controller\Soap
  16. */
  17. protected $_soapController;
  18. /**
  19. * @var \Magento\Webapi\Model\Soap\Server
  20. */
  21. protected $_soapServerMock;
  22. /**
  23. * @var \Magento\Webapi\Model\Soap\Wsdl\Generator
  24. */
  25. protected $_wsdlGeneratorMock;
  26. /**
  27. * @var \Magento\Framework\Webapi\Request
  28. */
  29. protected $_requestMock;
  30. /**
  31. * @var \Magento\Framework\Webapi\Response
  32. */
  33. protected $_responseMock;
  34. /**
  35. * @var \Magento\Framework\Webapi\ErrorProcessor
  36. */
  37. protected $_errorProcessorMock;
  38. /**
  39. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Locale\ResolverInterface
  40. */
  41. protected $_localeMock;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\State
  44. */
  45. protected $_appStateMock;
  46. protected $_appconfig;
  47. /**
  48. * Set up Controller object.
  49. */
  50. protected function setUp()
  51. {
  52. parent::setUp();
  53. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  54. $this->_soapServerMock = $this->getMockBuilder(\Magento\Webapi\Model\Soap\Server::class)
  55. ->disableOriginalConstructor()
  56. ->setMethods(['getApiCharset', 'generateUri', 'handle', 'setWSDL', 'setEncoding', 'setReturnResponse'])
  57. ->getMock();
  58. $this->_wsdlGeneratorMock = $this->getMockBuilder(\Magento\Webapi\Model\Soap\Wsdl\Generator::class)
  59. ->disableOriginalConstructor()
  60. ->setMethods(['generate'])
  61. ->getMock();
  62. $this->_requestMock = $this->getMockBuilder(\Magento\Framework\Webapi\Request::class)
  63. ->disableOriginalConstructor()
  64. ->setMethods(['getParams', 'getParam', 'getRequestedServices', 'getHttpHost'])
  65. ->getMock();
  66. $this->_requestMock->expects($this->any())
  67. ->method('getHttpHost')
  68. ->willReturn('testHostName.com');
  69. $this->_responseMock = $this->getMockBuilder(\Magento\Framework\Webapi\Response::class)
  70. ->disableOriginalConstructor()
  71. ->setMethods(['clearHeaders', 'setHeader', 'sendResponse', 'getHeaders'])
  72. ->getMock();
  73. $this->_errorProcessorMock = $this->getMockBuilder(\Magento\Framework\Webapi\ErrorProcessor::class)
  74. ->disableOriginalConstructor()
  75. ->setMethods(['maskException'])
  76. ->getMock();
  77. $this->_appStateMock = $this->createMock(\Magento\Framework\App\State::class);
  78. $localeResolverMock = $this->getMockBuilder(
  79. \Magento\Framework\Locale\Resolver::class
  80. )->disableOriginalConstructor()->setMethods(
  81. ['getLocale']
  82. )->getMock();
  83. $localeResolverMock->expects($this->any())->method('getLocale')->will($this->returnValue('en'));
  84. $this->_responseMock->expects($this->any())->method('clearHeaders')->will($this->returnSelf());
  85. $this->_responseMock
  86. ->expects($this->any())
  87. ->method('getHeaders')
  88. ->will($this->returnValue(new \Zend\Http\Headers()));
  89. $appconfig = $this->createMock(\Magento\Framework\App\Config::class);
  90. $objectManagerHelper->setBackwardCompatibleProperty(
  91. $this->_requestMock,
  92. 'appConfig',
  93. $appconfig
  94. );
  95. $this->_soapServerMock->expects($this->any())->method('setWSDL')->will($this->returnSelf());
  96. $this->_soapServerMock->expects($this->any())->method('setEncoding')->will($this->returnSelf());
  97. $this->_soapServerMock->expects($this->any())->method('setReturnResponse')->will($this->returnSelf());
  98. $pathProcessorMock = $this->createMock(\Magento\Webapi\Controller\PathProcessor::class);
  99. $areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
  100. $areaMock = $this->createMock(\Magento\Framework\App\AreaInterface::class);
  101. $areaListMock->expects($this->any())->method('getArea')->will($this->returnValue($areaMock));
  102. $rendererMock = $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Response\RendererFactory::class)
  103. ->disableOriginalConstructor()
  104. ->getMock();
  105. $this->_soapController = new \Magento\Webapi\Controller\Soap(
  106. $this->_requestMock,
  107. $this->_responseMock,
  108. $this->_wsdlGeneratorMock,
  109. $this->_soapServerMock,
  110. $this->_errorProcessorMock,
  111. $this->_appStateMock,
  112. $localeResolverMock,
  113. $pathProcessorMock,
  114. $rendererMock,
  115. $areaListMock
  116. );
  117. }
  118. /**
  119. * Test successful WSDL content generation.
  120. */
  121. public function testDispatchWsdl()
  122. {
  123. $params = [
  124. \Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL => 1,
  125. \Magento\Framework\Webapi\Request::REQUEST_PARAM_SERVICES => 'foo',
  126. ];
  127. $this->_mockGetParam(\Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL, 1);
  128. $this->_requestMock->expects($this->once())
  129. ->method('getParams')
  130. ->will($this->returnValue($params));
  131. $wsdl = 'Some WSDL content';
  132. $this->_wsdlGeneratorMock->expects($this->any())->method('generate')->will($this->returnValue($wsdl));
  133. $this->_soapController->dispatch($this->_requestMock);
  134. $this->assertEquals($wsdl, $this->_responseMock->getBody());
  135. }
  136. public function testDispatchInvalidWsdlRequest()
  137. {
  138. $params = [
  139. \Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL => 1,
  140. 'param_1' => 'foo',
  141. 'param_2' => 'bar,'
  142. ];
  143. $this->_mockGetParam(\Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL, 1);
  144. $this->_requestMock->expects($this->once())
  145. ->method('getParams')
  146. ->will($this->returnValue($params));
  147. $this->_errorProcessorMock->expects(
  148. $this->any()
  149. )->method(
  150. 'maskException'
  151. )->will(
  152. $this->returnValue(new \Magento\Framework\Webapi\Exception(__('message')))
  153. );
  154. $wsdl = 'Some WSDL content';
  155. $this->_wsdlGeneratorMock->expects($this->any())->method('generate')->will($this->returnValue($wsdl));
  156. $encoding = "utf-8";
  157. $this->_soapServerMock->expects($this->any())->method('getApiCharset')->will($this->returnValue($encoding));
  158. $this->_soapController->dispatch($this->_requestMock);
  159. $expectedMessage = <<<EXPECTED_MESSAGE
  160. <?xml version="1.0" encoding="{$encoding}"?>
  161. <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" >
  162. <env:Body>
  163. <env:Fault>
  164. <env:Code>
  165. <env:Value>env:Sender</env:Value>
  166. </env:Code>
  167. <env:Reason>
  168. <env:Text xml:lang="en">message</env:Text>
  169. </env:Reason>
  170. </env:Fault>
  171. </env:Body>
  172. </env:Envelope>
  173. EXPECTED_MESSAGE;
  174. $this->assertXmlStringEqualsXmlString($expectedMessage, $this->_responseMock->getBody());
  175. }
  176. /**
  177. * Test successful SOAP action request dispatch.
  178. */
  179. public function testDispatchSoapRequest()
  180. {
  181. $this->_soapServerMock->expects($this->once())->method('handle');
  182. $response = $this->_soapController->dispatch($this->_requestMock);
  183. $this->assertEquals(200, $response->getHttpResponseCode());
  184. }
  185. /**
  186. * Test handling exception during dispatch.
  187. */
  188. public function testDispatchWithException()
  189. {
  190. $exceptionMessage = 'some error message';
  191. $exception = new \Magento\Framework\Webapi\Exception(__($exceptionMessage));
  192. $this->_soapServerMock->expects($this->any())->method('handle')->will($this->throwException($exception));
  193. $this->_errorProcessorMock->expects(
  194. $this->any()
  195. )->method(
  196. 'maskException'
  197. )->will(
  198. $this->returnValue($exception)
  199. );
  200. $encoding = "utf-8";
  201. $this->_soapServerMock->expects($this->any())->method('getApiCharset')->will($this->returnValue($encoding));
  202. $this->_soapController->dispatch($this->_requestMock);
  203. $expectedMessage = <<<EXPECTED_MESSAGE
  204. <?xml version="1.0" encoding="{$encoding}"?>
  205. <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" >
  206. <env:Body>
  207. <env:Fault>
  208. <env:Code>
  209. <env:Value>env:Sender</env:Value>
  210. </env:Code>
  211. <env:Reason>
  212. <env:Text xml:lang="en">some error message</env:Text>
  213. </env:Reason>
  214. </env:Fault>
  215. </env:Body>
  216. </env:Envelope>
  217. EXPECTED_MESSAGE;
  218. $this->assertXmlStringEqualsXmlString($expectedMessage, $this->_responseMock->getBody());
  219. }
  220. /**
  221. * Mock getParam() of request object to return given value.
  222. *
  223. * @param $param
  224. * @param $value
  225. */
  226. protected function _mockGetParam($param, $value)
  227. {
  228. $this->_requestMock->expects(
  229. $this->any()
  230. )->method(
  231. 'getParam'
  232. )->with(
  233. $param
  234. )->will(
  235. $this->returnValue($value)
  236. );
  237. }
  238. }