Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SoapErrorHandlingTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Webapi\Routing;
8 
10 
15 {
16  protected function setUp()
17  {
18  $this->_markTestAsSoapOnly();
19  parent::setUp();
20  }
21 
22  public function testWebapiException()
23  {
24  $serviceInfo = [
25  'soap' => [
26  'service' => 'testModule3ErrorV1',
27  'operation' => 'testModule3ErrorV1WebapiException',
28  ],
29  ];
30  try {
31  $this->_webApiCall($serviceInfo);
32  $this->fail("SoapFault was not raised as expected.");
33  } catch (\SoapFault $e) {
34  $this->checkSoapFault(
35  $e,
36  'Service not found',
37  'env:Sender'
38  );
39  }
40  }
41 
42  public function testUnknownException()
43  {
44  $serviceInfo = [
45  'soap' => [
46  'service' => 'testModule3ErrorV1',
47  'operation' => 'testModule3ErrorV1OtherException',
48  ],
49  ];
50  try {
51  $this->_webApiCall($serviceInfo);
52  $this->fail("SoapFault was not raised as expected.");
53  } catch (\SoapFault $e) {
55  if (strpos($e->getMessage(), 'Internal Error') === false) {
56  $this->checkSoapFault(
57  $e,
58  'Non service exception',
59  'env:Receiver',
60  null,
61  null,
62  'Magento\TestModule3\Service\V1\Error->otherException()'
63  );
64  } else {
65  $this->checkSoapFault(
66  $e,
67  'Internal Error. Details are available in Magento log file. Report ID:',
68  'env:Receiver'
69  );
70  }
71  }
72  }
73 
74  public function testEmptyInputException()
75  {
76  $parameters = [];
77  $this->_testWrappedError($parameters);
78  }
79 
81  {
82  $parameters = [
83  ['fieldName' => 'key1', 'value' => 'value1'],
84  ];
85  $this->_testWrappedError($parameters);
86  }
87 
89  {
90  $parameters = [
91  ['fieldName' => 'key1', 'value' => 'value1'],
92  ['fieldName' => 'key2', 'value' => 'value2'],
93  ];
94  $this->_testWrappedError($parameters);
95  }
96 
97  public function testUnauthorized()
98  {
99  $serviceInfo = [
100  'soap' => [
101  'service' => 'testModule3ErrorV1',
102  'operation' => 'testModule3ErrorV1AuthorizationException',
103  'token' => 'invalidToken',
104  ],
105  ];
106 
107  try {
108  $this->_webApiCall($serviceInfo);
109  $this->fail("SoapFault was not raised as expected.");
110  } catch (\SoapFault $e) {
111  $this->checkSoapFault(
112  $e,
113  "The consumer isn't authorized to access %resources.",
114  'env:Sender'
115  );
116  }
117  }
118 
119  protected function _testWrappedError($parameters)
120  {
121  $serviceInfo = [
122  'soap' => [
123  'service' => 'testModule3ErrorV1',
124  'operation' => 'testModule3ErrorV1InputException',
125  ],
126  ];
127 
128  $expectedException = new \Magento\Framework\Exception\InputException();
129  foreach ($parameters as $error) {
130  $expectedException->addError(
131  __('Invalid value of "%value" provided for the %fieldName field.', $error)
132  );
133  }
134 
135  $arguments = [
136  'wrappedErrorParameters' => $parameters,
137  ];
138 
139  $expectedErrors = [];
140  foreach ($expectedException->getErrors() as $key => $error) {
141  $expectedErrors[$key] = [
142  'message' => $error->getRawMessage(),
143  'params' => $error->getParameters(),
144  ];
145  }
146 
147  try {
148  $this->_webApiCall($serviceInfo, $arguments);
149  $this->fail("SoapFault was not raised as expected.");
150  } catch (\SoapFault $e) {
151  $this->checkSoapFault(
152  $e,
153  $expectedException->getRawMessage(),
154  'env:Sender',
155  $expectedException->getParameters(), // expected error parameters
156  $expectedErrors // expected wrapped errors
157  );
158  }
159  }
160 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
__()
Definition: __.php:13
checkSoapFault( $soapFault, $expectedMessage, $expectedFaultCode, $expectedErrorParams=[], $expectedWrappedErrors=[], $traceString=null)
$arguments