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