3232 *
3333 */
3434
35+ use Codeception \Exception \ModuleRequire ;
36+ use Codeception \Lib \Framework ;
37+ use Codeception \Lib \InnerBrowser ;
3538use Codeception \Util \Soap as SoapUtils ;
3639
3740class SOAP extends \Codeception \Module
3841{
3942
43+ protected $ dependencyMessage = <<<EOF
44+ Example using PhpBrowser as backend for SOAP module.
45+ --
46+ modules:
47+ enabled: [SOAP, ApiHelper]
48+ depends:
49+ SOAP: PhpBrowser
50+ --
51+ Framework modules can be used as well for functional testing of SOAP API.
52+ EOF ;
53+
54+
4055 protected $ config = ['schema ' => "" , 'schema_url ' => 'http://schemas.xmlsoap.org/soap/envelope/ ' ];
4156 protected $ requiredFields = ['endpoint ' ];
4257 /**
4358 * @var \Symfony\Component\BrowserKit\Client
4459 */
4560 public $ client = null ;
46- public $ is_functional = false ;
61+ public $ isFunctional = false ;
4762
4863 /**
4964 * @var \DOMDocument
@@ -54,33 +69,40 @@ class SOAP extends \Codeception\Module
5469 */
5570 public $ xmlResponse = null ;
5671
72+ /**
73+ * @var InnerBrowser
74+ */
75+ protected $ connectionModule ;
76+
5777 public function _before (\Codeception \TestCase $ test )
5878 {
59- if (!$ this ->client ) {
60- if (!strpos ($ this ->config ['endpoint ' ], ':// ' )) {
61- // not valid url
62- foreach ($ this ->getModules () as $ module ) {
63- if ($ module instanceof \Codeception \Lib \Framework) {
64- $ this ->client = $ module ->client ;
65- $ this ->is_functional = true ;
66- break ;
67- }
68- }
69- } else {
70- if (!$ this ->hasModule ('PhpBrowser ' )) {
71- throw new \Codeception \Exception \ModuleConfig (__CLASS__ , "For Soap testing via HTTP please enable PhpBrowser module " );
72- }
73- $ this ->client = $ this ->getModule ('PhpBrowser ' )->client ;
74- }
75- if (!$ this ->client ) {
76- throw new \Codeception \Exception \ModuleConfig (__CLASS__ , "Client for SOAP requests not initialized. \nProvide either PhpBrowser module or Framework module which shares FrameworkInterface " );
77- }
78- }
79-
79+ $ this ->client = &$ this ->connectionModule ->client ;
8080 $ this ->buildRequest ();
8181 $ this ->xmlResponse = null ;
8282 }
8383
84+ public function _depends ()
85+ {
86+ return ['Codeception\Lib\InnerBrowser ' => $ this ->dependencyMessage ];
87+ }
88+
89+ public function _inject (InnerBrowser $ connectionModule )
90+ {
91+ $ this ->connectionModule = $ connectionModule ;
92+ if ($ connectionModule instanceof Framework) {
93+ $ this ->isFunctional = true ;
94+ }
95+ }
96+
97+ private function getClient ()
98+ {
99+ if (!$ this ->client ) {
100+ throw new ModuleRequire ($ this , "Connection client is not available. " );
101+ }
102+ return $ this ->client ;
103+ }
104+
105+
84106 /**
85107 * Prepare SOAP header.
86108 * Receives header name and parameters as array.
@@ -162,7 +184,7 @@ public function sendSoapRequest($action, $body = "")
162184 $ xmlBody ->appendChild ($ call );
163185 $ this ->debugSection ("Request " , $ req = $ xml ->C14N ());
164186
165- if ($ this ->is_functional ) {
187+ if ($ this ->isFunctional ) {
166188 $ response = $ this ->processInternalRequest ($ action , $ req );
167189 } else {
168190 $ response = $ this ->processExternalRequest ($ action , $ req );
@@ -195,7 +217,7 @@ public function sendSoapRequest($action, $body = "")
195217 public function seeSoapResponseEquals ($ xml )
196218 {
197219 $ xml = SoapUtils::toXml ($ xml );
198- \PHPUnit_Framework_Assert:: assertEquals ($ this ->xmlResponse ->C14N (), $ xml ->C14N ());
220+ $ this -> assertEquals ($ this ->xmlResponse ->C14N (), $ xml ->C14N ());
199221 }
200222
201223 /**
@@ -221,7 +243,7 @@ public function seeSoapResponseEquals($xml)
221243 public function seeSoapResponseIncludes ($ xml )
222244 {
223245 $ xml = $ this ->canonicalize ($ xml );
224- \PHPUnit_Framework_Assert:: assertContains ($ xml , $ this ->xmlResponse ->C14N (), "found in XML Response " );
246+ $ this -> assertContains ($ xml , $ this ->xmlResponse ->C14N (), "found in XML Response " );
225247 }
226248
227249
@@ -250,7 +272,7 @@ public function dontSeeSoapResponseEquals($xml)
250272 public function dontSeeSoapResponseIncludes ($ xml )
251273 {
252274 $ xml = $ this ->canonicalize ($ xml );
253- \PHPUnit_Framework_Assert:: assertNotContains ($ xml , $ this ->xmlResponse ->C14N (), "found in XML Response " );
275+ $ this -> assertNotContains ($ xml , $ this ->xmlResponse ->C14N (), "found in XML Response " );
254276 }
255277
256278 /**
@@ -285,14 +307,14 @@ public function seeSoapResponseContainsStructure($xml)
285307 $ els = $ this ->xmlResponse ->getElementsByTagName ($ root ->nodeName );
286308
287309 if (empty ($ els )) {
288- return \PHPUnit_Framework_Assert:: fail ("Element {$ root ->nodeName } not found in response " );
310+ $ this -> fail ("Element {$ root ->nodeName } not found in response " );
289311 }
290312
291313 $ matches = false ;
292314 foreach ($ els as $ node ) {
293315 $ matches |= $ this ->structureMatches ($ root , $ node );
294316 }
295- \PHPUnit_Framework_Assert:: assertTrue ((bool )$ matches , "this structure is in response " );
317+ $ this -> assertTrue ((bool )$ matches , "this structure is in response " );
296318
297319 }
298320
@@ -455,7 +477,7 @@ protected function buildRequest()
455477
456478 protected function processRequest ($ action , $ body )
457479 {
458- $ this ->client ->request (
480+ $ this ->getClient () ->request (
459481 'POST ' ,
460482 $ this ->config ['endpoint ' ],
461483 [], [],
@@ -472,7 +494,7 @@ protected function processInternalRequest($action, $body)
472494 {
473495 ob_start ();
474496 try {
475- $ this ->client ->setServerParameter ('HTTP_HOST ' , 'localhost ' );
497+ $ this ->getClient () ->setServerParameter ('HTTP_HOST ' , 'localhost ' );
476498 $ this ->processRequest ($ action , $ body );
477499 } catch (\ErrorException $ e ) {
478500 // Zend_Soap outputs warning as an exception
0 commit comments