3232 *
3333 */
3434
35- use Codeception \Exception \ModuleRequire ;
35+ use Codeception \Exception \ModuleRequireException ;
3636use Codeception \Lib \Framework ;
3737use Codeception \Lib \InnerBrowser ;
3838use Codeception \Util \Soap as SoapUtils ;
39+ use Codeception \Util \XmlStructure ;
3940
4041class SOAP extends \Codeception \Module
4142{
@@ -69,6 +70,11 @@ class SOAP extends \Codeception\Module
6970 */
7071 public $ xmlResponse = null ;
7172
73+ /**
74+ * @var XmlStructure
75+ */
76+ protected $ xmlStructure ;
77+
7278 /**
7379 * @var InnerBrowser
7480 */
@@ -97,7 +103,7 @@ public function _inject(InnerBrowser $connectionModule)
97103 private function getClient ()
98104 {
99105 if (!$ this ->client ) {
100- throw new ModuleRequire ($ this , "Connection client is not available. " );
106+ throw new ModuleRequireException ($ this , "Connection client is not available. " );
101107 }
102108 return $ this ->client ;
103109 }
@@ -192,6 +198,7 @@ public function sendSoapRequest($action, $body = "")
192198
193199 $ this ->debugSection ("Response " , $ response );
194200 $ this ->xmlResponse = SoapUtils::toXml ($ response );
201+ $ this ->xmlStructure = new XmlStructure ($ this ->xmlResponse );
195202 }
196203
197204 /**
@@ -300,22 +307,18 @@ public function seeSoapResponseContainsStructure($xml)
300307 {
301308 $ xml = SoapUtils::toXml ($ xml );
302309 $ this ->debugSection ("Structure " , $ xml ->saveXML ());
303- $ root = $ xml ->firstChild ;
304-
305- $ this ->debugSection ("Structure Root " , $ root ->nodeName );
306-
307- $ els = $ this ->xmlResponse ->getElementsByTagName ($ root ->nodeName );
308-
309- if (empty ($ els )) {
310- $ this ->fail ("Element {$ root ->nodeName } not found in response " );
311- }
312-
313- $ matches = false ;
314- foreach ($ els as $ node ) {
315- $ matches |= $ this ->structureMatches ($ root , $ node );
316- }
317- $ this ->assertTrue ((bool )$ matches , "this structure is in response " );
310+ $ this ->assertTrue ((bool )$ this ->xmlValidator ->matchXmlStructure ($ xml ), "this structure is in response " );
311+ }
318312
313+ /**
314+ * Opposite to `seeSoapResponseContainsStructure`
315+ * @param $xml
316+ */
317+ public function dontSeeSoapResponseContainsStructure ($ xml )
318+ {
319+ $ xml = SoapUtils::toXml ($ xml );
320+ $ this ->debugSection ("Structure " , $ xml ->saveXML ());
321+ $ this ->assertFalse ((bool )$ this ->xmlValidator ->matchXmlStructure ($ xml ), "this structure is in response " );
319322 }
320323
321324 /**
@@ -331,12 +334,7 @@ public function seeSoapResponseContainsStructure($xml)
331334 */
332335 public function seeSoapResponseContainsXPath ($ xpath )
333336 {
334- $ path = new \DOMXPath ($ this ->xmlResponse );
335- $ res = $ path ->query ($ xpath );
336- if ($ res === false ) {
337- $ this ->fail ("XPath selector is malformed " );
338- }
339- $ this ->assertGreaterThan (0 , $ res ->length );
337+ $ this ->assertTrue ($ this ->xmlValidator ->matchesXpath ($ xpath ));
340338 }
341339
342340 /**
@@ -352,12 +350,7 @@ public function seeSoapResponseContainsXPath($xpath)
352350 */
353351 public function dontSeeSoapResponseContainsXPath ($ xpath )
354352 {
355- $ path = new \DOMXPath ($ this ->xmlResponse );
356- $ res = $ path ->query ($ xpath );
357- if ($ res === false ) {
358- $ this ->fail ("XPath selector is malformed " );
359- }
360- $ this ->assertEquals (0 , $ res ->length );
353+ $ this ->assertFalse ($ this ->xmlValidator ->matchesXpath ($ xpath ));
361354 }
362355
363356
@@ -368,7 +361,7 @@ public function dontSeeSoapResponseContainsXPath($xpath)
368361 */
369362 public function seeResponseCodeIs ($ code )
370363 {
371- \PHPUnit_Framework_Assert:: assertEquals ($ code , $ this ->client ->getInternalResponse ()->getStatus (), "soap response code matches expected " );
364+ $ this -> assertEquals ($ code , $ this ->client ->getInternalResponse ()->getStatus (), "soap response code matches expected " );
372365 }
373366
374367 /**
@@ -381,7 +374,7 @@ public function seeResponseCodeIs($code)
381374 */
382375 public function grabTextContentFrom ($ cssOrXPath )
383376 {
384- $ el = $ this ->matchElement ($ cssOrXPath );
377+ $ el = $ this ->xmlValidator -> matchElement ($ cssOrXPath );
385378 return $ el ->textContent ;
386379 }
387380
@@ -396,64 +389,21 @@ public function grabTextContentFrom($cssOrXPath)
396389 */
397390 public function grabAttributeFrom ($ cssOrXPath , $ attribute )
398391 {
399- $ el = $ this ->matchElement ($ cssOrXPath );
392+ $ el = $ this ->xmlValidator -> matchElement ($ cssOrXPath );
400393 if (!$ el ->hasAttribute ($ attribute )) {
401394 $ this ->fail ("Attribute not found in element matched by ' $ cssOrXPath' " );
402395 }
403396 return $ el ->getAttribute ($ attribute );
404397 }
405398
406- /**
407- * @param $cssOrXPath
408- * @return \DOMElement
409- */
410- protected function matchElement ($ cssOrXPath )
411- {
412- $ xpath = new \DOMXpath ($ this ->xmlResponse );
413- try {
414- $ selector = \Symfony \Component \CssSelector \CssSelector::toXPath ($ cssOrXPath );
415- $ els = $ xpath ->query ($ selector );
416- if ($ els ) {
417- return $ els ->item (0 );
418- }
419- } catch (\Symfony \Component \CssSelector \Exception \ParseException $ e ) {
420- }
421- $ els = $ xpath ->query ($ cssOrXPath );
422- if ($ els ) {
423- return $ els ->item (0 );
424- }
425- $ this ->fail ("No node matched CSS or XPath ' $ cssOrXPath' " );
426- }
427-
428-
429- protected function structureMatches ($ schema , $ xml )
430- {
431- foreach ($ schema ->childNodes as $ node1 ) {
432- $ matched = false ;
433- foreach ($ xml ->childNodes as $ node2 ) {
434- if ($ node1 ->nodeName == $ node2 ->nodeName ) {
435- $ matched = $ this ->structureMatches ($ node1 , $ node2 );
436- if ($ matched ) {
437- break ;
438- }
439- }
440- }
441- if (!$ matched ) {
442- return false ;
443- }
444- }
445- return true ;
446- }
447-
448399 protected function getSchema ()
449400 {
450401 return $ this ->config ['schema ' ];
451402 }
452403
453404 protected function canonicalize ($ xml )
454405 {
455- $ xml = SoapUtils::toXml ($ xml )->C14N ();
456- return $ xml ;
406+ return SoapUtils::toXml ($ xml )->C14N ();
457407 }
458408
459409 /**
0 commit comments