SoapTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. use Codeception\Util\Stub;
  3. use Codeception\Util\Soap as SoapUtil;
  4. /**
  5. * Class SoapTest
  6. * @group appveyor
  7. */
  8. class SoapTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Codeception\Module\Soap
  12. */
  13. protected $module = null;
  14. protected $layout;
  15. public function setUp()
  16. {
  17. $this->module = new \Codeception\Module\SOAP(make_container());
  18. $this->module->_setConfig(array(
  19. 'schema' => 'http://www.w3.org/2001/xml.xsd',
  20. 'endpoint' => 'http://codeception.com/api/wsdl'
  21. ));
  22. $this->layout = \Codeception\Configuration::dataDir().'/xml/layout.xml';
  23. $this->module->isFunctional = true;
  24. $this->module->_before(Stub::makeEmpty('\Codeception\Test\Test'));
  25. $this->module->client = Stub::makeEmpty('\Codeception\Lib\Connector\Universal');
  26. }
  27. public function testXmlIsBuilt()
  28. {
  29. $dom = new \DOMDocument();
  30. $dom->load($this->layout);
  31. $this->assertEqualXMLStructure($this->module->xmlRequest->documentElement, $dom->documentElement);
  32. $this->assertXmlStringEqualsXmlString($dom->saveXML(), $this->module->xmlRequest->saveXML());
  33. }
  34. public function testBuildHeaders()
  35. {
  36. $this->module->haveSoapHeader('AuthHeader', ['username' => 'davert', 'password' => '123456']);
  37. $dom = new \DOMDocument();
  38. $dom->load($this->layout);
  39. $header = $dom->createElement('AuthHeader');
  40. $header->appendChild($dom->createElement('username', 'davert'));
  41. $header->appendChild($dom->createElement('password', '123456'));
  42. $dom->documentElement->getElementsByTagName('Header')->item(0)->appendChild($header);
  43. $this->assertEqualXMLStructure($this->module->xmlRequest->documentElement, $dom->documentElement);
  44. }
  45. public function testBuildRequest()
  46. {
  47. $this->module->sendSoapRequest('KillHumans', "<item><id>1</id><subitem>2</subitem></item>");
  48. $this->assertNotNull($this->module->xmlRequest);
  49. $dom = new \DOMDocument();
  50. $dom->load($this->layout);
  51. $body = $dom->createElement('item');
  52. $body->appendChild($dom->createElement('id', 1));
  53. $body->appendChild($dom->createElement('subitem', 2));
  54. $request = $dom->createElement('ns:KillHumans');
  55. $request->appendChild($body);
  56. $dom->documentElement->getElementsByTagName('Body')->item(0)->appendChild($request);
  57. $this->assertEqualXMLStructure($this->module->xmlRequest->documentElement, $dom->documentElement);
  58. }
  59. public function testBuildRequestWithDomNode()
  60. {
  61. $dom = new \DOMDocument();
  62. $dom->load($this->layout);
  63. $body = $dom->createElement('item');
  64. $body->appendChild($dom->createElement('id', 1));
  65. $body->appendChild($dom->createElement('subitem', 2));
  66. $request = $dom->createElement('ns:KillHumans');
  67. $request->appendChild($body);
  68. $dom->documentElement->getElementsByTagName('Body')->item(0)->appendChild($request);
  69. $this->module->sendSoapRequest('KillHumans', $body);
  70. $this->assertEqualXMLStructure($this->module->xmlRequest->documentElement, $dom->documentElement);
  71. }
  72. public function testSeeXmlIncludes()
  73. {
  74. $dom = new DOMDocument();
  75. $this->module->xmlResponse = $dom;
  76. $dom->preserveWhiteSpace = false;
  77. $dom->loadXML('<?xml version="1.0" encoding="UTF-8"?> <doc> <a a2="2" a1="1" >123</a> </doc>');
  78. $this->module->seeSoapResponseIncludes('<a a2="2" a1="1" >123</a>');
  79. }
  80. public function testSeeXmlContainsXPath()
  81. {
  82. $dom = new DOMDocument();
  83. $this->module->xmlResponse = $dom;
  84. $dom->preserveWhiteSpace = false;
  85. $dom->loadXML('<?xml version="1.0" encoding="UTF-8"?> <doc> <a a2="2" a1="1" >123</a> </doc>');
  86. $this->module->seeSoapResponseContainsXPath('//doc/a[@a2=2 and @a1=1]');
  87. }
  88. public function testSeeXmlNotContainsXPath()
  89. {
  90. $dom = new DOMDocument();
  91. $this->module->xmlResponse = $dom;
  92. $dom->preserveWhiteSpace = false;
  93. $dom->loadXML('<?xml version="1.0" encoding="UTF-8"?> <doc> <a a2="2" a1="1" >123</a> </doc>');
  94. $this->module->dontSeeSoapResponseContainsXPath('//doc/a[@a2=2 and @a31]');
  95. }
  96. public function testSeeXmlEquals()
  97. {
  98. $dom = new DOMDocument();
  99. $this->module->xmlResponse = $dom;
  100. $xml = '<?xml version="1.0" encoding="UTF-8"?> <doc> <a a2="2" a1="1" >123</a> </doc>';
  101. $dom->preserveWhiteSpace = false;
  102. $dom->loadXML($xml);
  103. $this->module->seeSoapResponseEquals($xml);
  104. }
  105. public function testSeeXmlIncludesWithBuilder()
  106. {
  107. $dom = new DOMDocument();
  108. $this->module->xmlResponse = $dom;
  109. $dom->loadXML('<?xml version="1.0" encoding="UTF-8"?>'."\n".' <doc><a a2="2" a1="1" >123</a></doc>');
  110. $xml = SoapUtil::request()->doc->a
  111. ->attr('a2', '2')
  112. ->attr('a1', '1')
  113. ->val('123');
  114. $this->module->seeSoapResponseIncludes($xml);
  115. }
  116. public function testGrabTextFrom()
  117. {
  118. $dom = new DOMDocument();
  119. $this->module->xmlResponse = $dom;
  120. $dom->loadXML('<?xml version="1.0" encoding="UTF-8"?><doc><node>123</node></doc>');
  121. $res = $this->module->grabTextContentFrom('doc node');
  122. $this->assertEquals('123', $res);
  123. $res = $this->module->grabTextContentFrom('descendant-or-self::doc/descendant::node');
  124. $this->assertEquals('123', $res);
  125. }
  126. }