Skip to content

Commit a11a326

Browse files
committed
XPath matchers added
1 parent 5406583 commit a11a326

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

src/Codeception/Module/SOAP.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ public function dontSeeSoapResponseIncludes($xml)
255255
* ```
256256
*
257257
* Use this method to check XML of valid structure is returned.
258-
* This method doesn't use schema for validation.
259-
* This method dosn't require whole response XML to match the structure.
258+
* This method does not use schema for validation.
259+
* This method does not require path from root to match the structure.
260260
*
261261
* @param $xml
262262
*/
@@ -285,9 +285,57 @@ public function seeSoapResponseContainsStructure($xml) {
285285
* @param $code
286286
*/
287287
public function seeResponseCodeIs($code) {
288-
\PHPUnit_Framework_Assert::assertEquals($code, $this->client->getResponse()->getStatusCode(), "soap response code matches expected");
288+
\PHPUnit_Framework_Assert::assertEquals($code, $this->client->getResponse()->getStatus(), "soap response code matches expected");
289289
}
290290

291+
/**
292+
* Finds and returns text contents of element.
293+
* Element is matched by either CSS or XPath
294+
*
295+
* @version 1.1
296+
* @param $cssOrXPath
297+
* @return string
298+
*/
299+
public function grabTextFrom($cssOrXPath) {
300+
$el = $this->matchElement($cssOrXPath);
301+
return $el->textContent;
302+
}
303+
304+
/**
305+
* Finds and returns attribute of element.
306+
* Element is matched by either CSS or XPath
307+
*
308+
* @version 1.1
309+
* @param $cssOrXPath
310+
* @param $attribute
311+
* @return string
312+
*/
313+
public function grabAttributeFrom($cssOrXPath, $attribute) {
314+
$el = $this->matchElement($cssOrXPath);
315+
if (!$el->hasAttribute($attribute)) $this->fail("Attribute not found in element matched by '$cssOrXPath'");
316+
return $el->getAttribute($attribute);
317+
}
318+
319+
/**
320+
* @param $cssOrXPath
321+
* @return \DOMElement
322+
*/
323+
protected function matchElement($cssOrXPath)
324+
{
325+
$xpath = new \DOMXpath($this->xmlResponse);
326+
try {
327+
$selector = \Symfony\Component\CssSelector\CssSelector::toXPath($cssOrXPath);
328+
$els = $xpath->query($selector);
329+
if ($els) return $els->item(0);
330+
} catch (\Symfony\Component\CssSelector\Exception\ParseException $e) {}
331+
$els = $xpath->query($cssOrXPath);
332+
if ($els) {
333+
return $els->item(0);
334+
}
335+
$this->fail("No node matched CSS or XPath '$cssOrXPath'");
336+
}
337+
338+
291339
protected function structureMatches($schema, $xml)
292340
{
293341
foreach ($schema->childNodes as $node1) {

tests/unit/Codeception/Module/SoapTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,17 @@ public function testSeeXmlIncludesWithBuilder() {
9797
->val('123');
9898
$this->module->seeSoapResponseIncludes($xml);
9999
}
100+
101+
public function testGrabTextFrom() {
102+
$dom = new DOMDocument();
103+
$this->module->xmlResponse = $dom;
104+
$dom->loadXML('<?xml version="1.0" encoding="UTF-8"?><doc><node>123</node></doc>');
105+
$res = $this->module->grabTextFrom('doc node');
106+
$this->assertEquals('123', $res);
107+
$res = $this->module->grabTextFrom('descendant-or-self::doc/descendant::node');
108+
$this->assertEquals('123', $res);
109+
110+
111+
}
100112

101113
}

0 commit comments

Comments
 (0)