Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SoapTest.php
Go to the documentation of this file.
1 <?php
9 
13 class SoapTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $_soapController;
19 
23  protected $_soapServerMock;
24 
29 
33  protected $_requestMock;
34 
38  protected $_responseMock;
39 
44 
48  protected $_localeMock;
49 
53  protected $_appStateMock;
54 
55  protected $_appconfig;
56 
60  protected function setUp()
61  {
62  parent::setUp();
63 
64  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
65 
66  $this->_soapServerMock = $this->getMockBuilder(\Magento\Webapi\Model\Soap\Server::class)
67  ->disableOriginalConstructor()
68  ->setMethods(['getApiCharset', 'generateUri', 'handle', 'setWSDL', 'setEncoding', 'setReturnResponse'])
69  ->getMock();
70  $this->_wsdlGeneratorMock = $this->getMockBuilder(\Magento\Webapi\Model\Soap\Wsdl\Generator::class)
71  ->disableOriginalConstructor()
72  ->setMethods(['generate'])
73  ->getMock();
74  $this->_requestMock = $this->getMockBuilder(\Magento\Framework\Webapi\Request::class)
75  ->disableOriginalConstructor()
76  ->setMethods(['getParams', 'getParam', 'getRequestedServices', 'getHttpHost'])
77  ->getMock();
78  $this->_requestMock->expects($this->any())
79  ->method('getHttpHost')
80  ->willReturn('testHostName.com');
81  $this->_responseMock = $this->getMockBuilder(\Magento\Framework\Webapi\Response::class)
82  ->disableOriginalConstructor()
83  ->setMethods(['clearHeaders', 'setHeader', 'sendResponse', 'getHeaders'])
84  ->getMock();
85  $this->_errorProcessorMock = $this->getMockBuilder(\Magento\Framework\Webapi\ErrorProcessor::class)
86  ->disableOriginalConstructor()
87  ->setMethods(['maskException'])
88  ->getMock();
89 
90  $this->_appStateMock = $this->createMock(\Magento\Framework\App\State::class);
91 
92  $localeResolverMock = $this->getMockBuilder(
93  \Magento\Framework\Locale\Resolver::class
94  )->disableOriginalConstructor()->setMethods(
95  ['getLocale']
96  )->getMock();
97  $localeResolverMock->expects($this->any())->method('getLocale')->will($this->returnValue('en'));
98 
99  $this->_responseMock->expects($this->any())->method('clearHeaders')->will($this->returnSelf());
100  $this->_responseMock
101  ->expects($this->any())
102  ->method('getHeaders')
103  ->will($this->returnValue(new \Zend\Http\Headers()));
104 
105  $appconfig = $this->createMock(\Magento\Framework\App\Config::class);
106  $objectManagerHelper->setBackwardCompatibleProperty(
107  $this->_requestMock,
108  'appConfig',
109  $appconfig
110  );
111 
112  $this->_soapServerMock->expects($this->any())->method('setWSDL')->will($this->returnSelf());
113  $this->_soapServerMock->expects($this->any())->method('setEncoding')->will($this->returnSelf());
114  $this->_soapServerMock->expects($this->any())->method('setReturnResponse')->will($this->returnSelf());
115  $pathProcessorMock = $this->createMock(\Magento\Webapi\Controller\PathProcessor::class);
116  $areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
117  $areaMock = $this->createMock(\Magento\Framework\App\AreaInterface::class);
118  $areaListMock->expects($this->any())->method('getArea')->will($this->returnValue($areaMock));
119 
120  $rendererMock = $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Response\RendererFactory::class)
121  ->disableOriginalConstructor()
122  ->getMock();
123  $this->_soapController = new \Magento\Webapi\Controller\Soap(
124  $this->_requestMock,
125  $this->_responseMock,
126  $this->_wsdlGeneratorMock,
127  $this->_soapServerMock,
128  $this->_errorProcessorMock,
129  $this->_appStateMock,
130  $localeResolverMock,
131  $pathProcessorMock,
132  $rendererMock,
133  $areaListMock
134  );
135  }
136 
140  public function testDispatchWsdl()
141  {
142  $params = [
145  ];
146  $this->_mockGetParam(\Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL, 1);
147  $this->_requestMock->expects($this->once())
148  ->method('getParams')
149  ->will($this->returnValue($params));
150  $wsdl = 'Some WSDL content';
151  $this->_wsdlGeneratorMock->expects($this->any())->method('generate')->will($this->returnValue($wsdl));
152 
153  $this->_soapController->dispatch($this->_requestMock);
154  $this->assertEquals($wsdl, $this->_responseMock->getBody());
155  }
156 
158  {
159  $params = [
161  'param_1' => 'foo',
162  'param_2' => 'bar,'
163  ];
164  $this->_mockGetParam(\Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL, 1);
165  $this->_requestMock->expects($this->once())
166  ->method('getParams')
167  ->will($this->returnValue($params));
168  $this->_errorProcessorMock->expects(
169  $this->any()
170  )->method(
171  'maskException'
172  )->will(
173  $this->returnValue(new \Magento\Framework\Webapi\Exception(__('message')))
174  );
175  $wsdl = 'Some WSDL content';
176  $this->_wsdlGeneratorMock->expects($this->any())->method('generate')->will($this->returnValue($wsdl));
177  $encoding = "utf-8";
178  $this->_soapServerMock->expects($this->any())->method('getApiCharset')->will($this->returnValue($encoding));
179  $this->_soapController->dispatch($this->_requestMock);
180 
181  $expectedMessage = <<<EXPECTED_MESSAGE
182 <?xml version="1.0" encoding="{$encoding}"?>
183 <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" >
184  <env:Body>
185  <env:Fault>
186  <env:Code>
187  <env:Value>env:Sender</env:Value>
188  </env:Code>
189  <env:Reason>
190  <env:Text xml:lang="en">message</env:Text>
191  </env:Reason>
192  </env:Fault>
193  </env:Body>
194 </env:Envelope>
195 EXPECTED_MESSAGE;
196  $this->assertXmlStringEqualsXmlString($expectedMessage, $this->_responseMock->getBody());
197  }
198 
202  public function testDispatchSoapRequest()
203  {
204  $this->_soapServerMock->expects($this->once())->method('handle');
205  $response = $this->_soapController->dispatch($this->_requestMock);
206  $this->assertEquals(200, $response->getHttpResponseCode());
207  }
208 
212  public function testDispatchWithException()
213  {
214  $exceptionMessage = 'some error message';
215  $exception = new \Magento\Framework\Webapi\Exception(__($exceptionMessage));
216  $this->_soapServerMock->expects($this->any())->method('handle')->will($this->throwException($exception));
217  $this->_errorProcessorMock->expects(
218  $this->any()
219  )->method(
220  'maskException'
221  )->will(
222  $this->returnValue($exception)
223  );
224  $encoding = "utf-8";
225  $this->_soapServerMock->expects($this->any())->method('getApiCharset')->will($this->returnValue($encoding));
226 
227  $this->_soapController->dispatch($this->_requestMock);
228 
229  $expectedMessage = <<<EXPECTED_MESSAGE
230 <?xml version="1.0" encoding="{$encoding}"?>
231 <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" >
232  <env:Body>
233  <env:Fault>
234  <env:Code>
235  <env:Value>env:Sender</env:Value>
236  </env:Code>
237  <env:Reason>
238  <env:Text xml:lang="en">some error message</env:Text>
239  </env:Reason>
240  </env:Fault>
241  </env:Body>
242 </env:Envelope>
243 EXPECTED_MESSAGE;
244  $this->assertXmlStringEqualsXmlString($expectedMessage, $this->_responseMock->getBody());
245  }
246 
253  protected function _mockGetParam($param, $value)
254  {
255  $this->_requestMock->expects(
256  $this->any()
257  )->method(
258  'getParam'
259  )->with(
260  $param
261  )->will(
262  $this->returnValue($value)
263  );
264  }
265 }
$response
Definition: 404.php:11
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18