SoapErrorHandlingTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Routing;
  7. use Magento\Framework\Exception\AuthorizationException;
  8. /**
  9. * SOAP error handling test.
  10. */
  11. class SoapErrorHandlingTest extends \Magento\TestFramework\TestCase\WebapiAbstract
  12. {
  13. protected function setUp()
  14. {
  15. $this->_markTestAsSoapOnly();
  16. parent::setUp();
  17. }
  18. public function testWebapiException()
  19. {
  20. $serviceInfo = [
  21. 'soap' => [
  22. 'service' => 'testModule3ErrorV1',
  23. 'operation' => 'testModule3ErrorV1WebapiException',
  24. ],
  25. ];
  26. try {
  27. $this->_webApiCall($serviceInfo);
  28. $this->fail("SoapFault was not raised as expected.");
  29. } catch (\SoapFault $e) {
  30. $this->checkSoapFault(
  31. $e,
  32. 'Service not found',
  33. 'env:Sender'
  34. );
  35. }
  36. }
  37. public function testUnknownException()
  38. {
  39. $serviceInfo = [
  40. 'soap' => [
  41. 'service' => 'testModule3ErrorV1',
  42. 'operation' => 'testModule3ErrorV1OtherException',
  43. ],
  44. ];
  45. try {
  46. $this->_webApiCall($serviceInfo);
  47. $this->fail("SoapFault was not raised as expected.");
  48. } catch (\SoapFault $e) {
  49. /** In developer mode message is masked, so checks should be different in two modes */
  50. if (strpos($e->getMessage(), 'Internal Error') === false) {
  51. $this->checkSoapFault(
  52. $e,
  53. 'Non service exception',
  54. 'env:Receiver',
  55. null,
  56. null,
  57. 'Magento\TestModule3\Service\V1\Error->otherException()'
  58. );
  59. } else {
  60. $this->checkSoapFault(
  61. $e,
  62. 'Internal Error. Details are available in Magento log file. Report ID:',
  63. 'env:Receiver'
  64. );
  65. }
  66. }
  67. }
  68. public function testEmptyInputException()
  69. {
  70. $parameters = [];
  71. $this->_testWrappedError($parameters);
  72. }
  73. public function testSingleWrappedErrorException()
  74. {
  75. $parameters = [
  76. ['fieldName' => 'key1', 'value' => 'value1'],
  77. ];
  78. $this->_testWrappedError($parameters);
  79. }
  80. public function testMultipleWrappedErrorException()
  81. {
  82. $parameters = [
  83. ['fieldName' => 'key1', 'value' => 'value1'],
  84. ['fieldName' => 'key2', 'value' => 'value2'],
  85. ];
  86. $this->_testWrappedError($parameters);
  87. }
  88. public function testUnauthorized()
  89. {
  90. $serviceInfo = [
  91. 'soap' => [
  92. 'service' => 'testModule3ErrorV1',
  93. 'operation' => 'testModule3ErrorV1AuthorizationException',
  94. 'token' => 'invalidToken',
  95. ],
  96. ];
  97. try {
  98. $this->_webApiCall($serviceInfo);
  99. $this->fail("SoapFault was not raised as expected.");
  100. } catch (\SoapFault $e) {
  101. $this->checkSoapFault(
  102. $e,
  103. "The consumer isn't authorized to access %resources.",
  104. 'env:Sender'
  105. );
  106. }
  107. }
  108. protected function _testWrappedError($parameters)
  109. {
  110. $serviceInfo = [
  111. 'soap' => [
  112. 'service' => 'testModule3ErrorV1',
  113. 'operation' => 'testModule3ErrorV1InputException',
  114. ],
  115. ];
  116. $expectedException = new \Magento\Framework\Exception\InputException();
  117. foreach ($parameters as $error) {
  118. $expectedException->addError(
  119. __('Invalid value of "%value" provided for the %fieldName field.', $error)
  120. );
  121. }
  122. $arguments = [
  123. 'wrappedErrorParameters' => $parameters,
  124. ];
  125. $expectedErrors = [];
  126. foreach ($expectedException->getErrors() as $key => $error) {
  127. $expectedErrors[$key] = [
  128. 'message' => $error->getRawMessage(),
  129. 'params' => $error->getParameters(),
  130. ];
  131. }
  132. try {
  133. $this->_webApiCall($serviceInfo, $arguments);
  134. $this->fail("SoapFault was not raised as expected.");
  135. } catch (\SoapFault $e) {
  136. $this->checkSoapFault(
  137. $e,
  138. $expectedException->getRawMessage(),
  139. 'env:Sender',
  140. $expectedException->getParameters(), // expected error parameters
  141. $expectedErrors // expected wrapped errors
  142. );
  143. }
  144. }
  145. }