FaultTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Test\Unit\Model\Soap;
  7. use \Magento\Webapi\Model\Soap\Fault;
  8. /**
  9. * Test SOAP fault model.
  10. */
  11. class FaultTest extends \PHPUnit\Framework\TestCase
  12. {
  13. const WSDL_URL = 'http://host.com/?wsdl&services=customerV1';
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $_requestMock;
  18. /** @var \Magento\Webapi\Model\Soap\Server */
  19. protected $_soapServerMock;
  20. /** @var \Magento\Webapi\Model\Soap\Fault */
  21. protected $_soapFault;
  22. /** @var \PHPUnit_Framework_MockObject_MockObject */
  23. protected $_localeResolverMock;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $_appStateMock;
  28. protected function setUp()
  29. {
  30. $this->_requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  31. /** Initialize SUT. */
  32. $details = ['param1' => 'value1', 'param2' => 2];
  33. $code = 111;
  34. $webapiException = new \Magento\Framework\Webapi\Exception(
  35. __('Soap fault reason.'),
  36. $code,
  37. \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR,
  38. $details
  39. );
  40. $this->_soapServerMock = $this->getMockBuilder(
  41. \Magento\Webapi\Model\Soap\Server::class
  42. )->disableOriginalConstructor()->getMock();
  43. $this->_soapServerMock->expects($this->any())->method('generateUri')->will($this->returnValue(self::WSDL_URL));
  44. $this->_localeResolverMock = $this->getMockBuilder(
  45. \Magento\Framework\Locale\Resolver::class
  46. )->disableOriginalConstructor()->getMock();
  47. $this->_localeResolverMock->expects(
  48. $this->any()
  49. )->method(
  50. 'getLocale'
  51. )->will(
  52. $this->returnValue('en_US')
  53. );
  54. $this->_appStateMock = $this->createMock(\Magento\Framework\App\State::class);
  55. $this->_soapFault = new \Magento\Webapi\Model\Soap\Fault(
  56. $this->_requestMock,
  57. $this->_soapServerMock,
  58. $webapiException,
  59. $this->_localeResolverMock,
  60. $this->_appStateMock
  61. );
  62. parent::setUp();
  63. }
  64. protected function tearDown()
  65. {
  66. unset($this->_soapFault);
  67. unset($this->_requestMock);
  68. parent::tearDown();
  69. }
  70. public function testToXmlDeveloperModeOff()
  71. {
  72. $this->_appStateMock->expects($this->any())->method('getMode')->will($this->returnValue('production'));
  73. $wsdlUrl = urlencode(self::WSDL_URL);
  74. $expectedResult = <<<XML
  75. <?xml version="1.0" encoding="utf-8" ?>
  76. <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:m="{$wsdlUrl}">
  77. <env:Body>
  78. <env:Fault>
  79. <env:Code>
  80. <env:Value>env:Receiver</env:Value>
  81. </env:Code>
  82. <env:Reason>
  83. <env:Text xml:lang="en">Soap fault reason.</env:Text>
  84. </env:Reason>
  85. <env:Detail>
  86. <m:GenericFault>
  87. <m:Parameters>
  88. <m:GenericFaultParameter>
  89. <m:key>param1</m:key>
  90. <m:value>value1</m:value>
  91. </m:GenericFaultParameter>
  92. <m:GenericFaultParameter>
  93. <m:key>param2</m:key>
  94. <m:value>2</m:value>
  95. </m:GenericFaultParameter>
  96. </m:Parameters>
  97. </m:GenericFault>
  98. </env:Detail>
  99. </env:Fault>
  100. </env:Body>
  101. </env:Envelope>
  102. XML;
  103. $actualXml = $this->_soapFault->toXml();
  104. $this->assertEquals(
  105. $this->_sanitizeXML($expectedResult),
  106. $this->_sanitizeXML($actualXml),
  107. 'Wrong SOAP fault message with default parameters.'
  108. );
  109. }
  110. public function testToXmlDeveloperModeOn()
  111. {
  112. $this->_appStateMock->expects($this->any())->method('getMode')->will($this->returnValue('developer'));
  113. $actualXml = $this->_soapFault->toXml();
  114. $this->assertContains('<m:Trace>', $actualXml, 'Exception trace is not found in XML.');
  115. }
  116. /**
  117. * Test getSoapFaultMessage method.
  118. *
  119. * @dataProvider dataProviderForGetSoapFaultMessageTest
  120. */
  121. public function testGetSoapFaultMessage(
  122. $faultReason,
  123. $faultCode,
  124. $additionalParameters,
  125. $expectedResult,
  126. $assertMessage
  127. ) {
  128. $actualResult = $this->_soapFault->getSoapFaultMessage($faultReason, $faultCode, $additionalParameters);
  129. $wsdlUrl = urlencode(self::WSDL_URL);
  130. $this->assertEquals(
  131. $this->_sanitizeXML(str_replace('{wsdl_url}', $wsdlUrl, $expectedResult)),
  132. $this->_sanitizeXML($actualResult),
  133. $assertMessage
  134. );
  135. }
  136. /**
  137. * Data provider for GetSoapFaultMessage test.
  138. *
  139. * @return array
  140. */
  141. public function dataProviderForGetSoapFaultMessageTest()
  142. {
  143. /** Include file with all expected SOAP fault XMLs. */
  144. $expectedXmls = include __DIR__ . '/../../_files/soap_fault/soap_fault_expected_xmls.php';
  145. //Each array contains data for SOAP Fault Message, Expected XML, and Assert Message.
  146. return [
  147. 'ArrayDataDetails' => [
  148. 'Fault reason',
  149. 'Sender',
  150. [
  151. Fault::NODE_DETAIL_PARAMETERS => ['key1' => 'value1', 'key2' => 'value2', 'value3'],
  152. Fault::NODE_DETAIL_TRACE => 'Trace',
  153. 'Invalid' => 'This node should be skipped'
  154. ],
  155. $expectedXmls['expectedResultArrayDataDetails'],
  156. 'SOAP fault message with associated array data details is invalid.',
  157. ],
  158. 'IndexArrayDetails' => [
  159. 'Fault reason',
  160. 'Sender',
  161. ['value1', 'value2'],
  162. $expectedXmls['expectedResultIndexArrayDetails'],
  163. 'SOAP fault message with index array data details is invalid.',
  164. ],
  165. 'EmptyArrayDetails' => [
  166. 'Fault reason',
  167. 'Sender',
  168. [],
  169. $expectedXmls['expectedResultEmptyArrayDetails'],
  170. 'SOAP fault message with empty array data details is invalid.',
  171. ],
  172. 'ObjectDetails' => [
  173. 'Fault reason',
  174. 'Sender',
  175. (object)['key' => 'value'],
  176. $expectedXmls['expectedResultObjectDetails'],
  177. 'SOAP fault message with object data details is invalid.',
  178. ],
  179. 'ComplexDataDetails' => [
  180. 'Fault reason',
  181. 'Sender',
  182. [Fault::NODE_DETAIL_PARAMETERS => ['key' => ['sub_key' => 'value']]],
  183. $expectedXmls['expectedResultComplexDataDetails'],
  184. 'SOAP fault message with complex data details is invalid.',
  185. ]
  186. ];
  187. }
  188. public function testConstructor()
  189. {
  190. $message = "Soap fault reason.";
  191. $details = ['param1' => 'value1', 'param2' => 2];
  192. $code = 111;
  193. $webapiException = new \Magento\Framework\Webapi\Exception(
  194. __('Soap fault reason.'),
  195. $code,
  196. \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR,
  197. $details
  198. );
  199. $soapFault = new \Magento\Webapi\Model\Soap\Fault(
  200. $this->_requestMock,
  201. $this->_soapServerMock,
  202. $webapiException,
  203. $this->_localeResolverMock,
  204. $this->_appStateMock
  205. );
  206. $actualXml = $soapFault->toXml();
  207. $wsdlUrl = urlencode(self::WSDL_URL);
  208. $expectedXml = <<<FAULT_XML
  209. <?xml version="1.0" encoding="utf-8" ?>
  210. <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:m="{$wsdlUrl}">
  211. <env:Body>
  212. <env:Fault>
  213. <env:Code>
  214. <env:Value>env:Receiver</env:Value>
  215. </env:Code>
  216. <env:Reason>
  217. <env:Text xml:lang="en">{$message}</env:Text>
  218. </env:Reason>
  219. <env:Detail>
  220. <m:GenericFault>
  221. <m:Parameters>
  222. <m:GenericFaultParameter>
  223. <m:key>param1</m:key>
  224. <m:value>value1</m:value>
  225. </m:GenericFaultParameter>
  226. <m:GenericFaultParameter>
  227. <m:key>param2</m:key>
  228. <m:value>2</m:value>
  229. </m:GenericFaultParameter>
  230. </m:Parameters>
  231. </m:GenericFault>
  232. </env:Detail>
  233. </env:Fault>
  234. </env:Body>
  235. </env:Envelope>
  236. FAULT_XML;
  237. $this->assertEquals(
  238. $this->_sanitizeXML($expectedXml),
  239. $this->_sanitizeXML($actualXml),
  240. "Soap fault is invalid."
  241. );
  242. }
  243. /**
  244. * Convert XML to string.
  245. *
  246. * @param string $xmlString
  247. * @return string
  248. */
  249. protected function _sanitizeXML($xmlString)
  250. {
  251. $dom = new \DOMDocument(1.0);
  252. $dom->preserveWhiteSpace = false;
  253. $dom->formatOutput = false;
  254. // Only useful for "pretty" output with saveXML()
  255. $dom->loadXML($xmlString);
  256. // Must be done AFTER preserveWhiteSpace and formatOutput are set
  257. return $dom->saveXML();
  258. }
  259. }