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