setFill('red'); $rect->setStroke('yellow'); $rect->setStrokeWidth('4'); $doc->addElement($rect); return $doc; } /** * Checks if an attribute exists for given node * * @param csDocument $doc * @param string $xpath_query * @param string $attr_name * @return boolean */ public function assertNodeAttrExists($doc, $xpath_query, $attr_name) { $result = $this->documentXpath($doc, $xpath_query); $this->assertNodeExists($doc, $xpath_query); if (!$result->item(0)->hasAttribute($attr_name)) { throw new PHPUnit_Framework_ExpectationFailedException( sprintf('No attribute "%s" found in node retrieved by "%s". Source: %s', $attr_name, $xpath_query, "\n".$doc->toXML())); } } /** * Checks that an attribute does not exist for given node * * @param csDocument $doc * @param string $xpath_query * @param string $attr_name * @return boolean */ public function assertNodeAttrNotExists($doc, $xpath_query, $attr_name) { $result = $this->documentXpath($doc, $xpath_query); $this->assertNodeExists($doc, $xpath_query); if ($result->item(0)->hasAttribute($attr_name)) { throw new PHPUnit_Framework_ExpectationFailedException( sprintf('Attribute %s="%s" retrieved by node "%s". Source: %s', $attr_name, $result->item(0)->getAttribute($attr_name), $xpath_query, "\n".$doc->toXML())); } } /** * Checks if an attribute xpath query matches given value for given doc. * * @param csDocument $doc * @param string $xpath_query * @param mixed $value * @return boolean */ protected function assertAttrValueEquals($doc, $xpath_query, $value) { $result = $this->documentXpath($doc, $xpath_query); $attr = $result->item(0); // First match if (!$attr) // Attribute not found { throw new PHPUnit_Framework_ExpectationFailedException( sprintf('No attribute found for query "%s". Source: %s', $xpath_query, "\n".$doc->toXML())); } $attr_value = $attr->value; if (!$attr_value or $attr_value != (string) $value) // String compare { throw new PHPUnit_Framework_ExpectationFailedException( sprintf('XPath query "%s" does not match value "%s", but "%s". Source: %s', $xpath_query, $value, $attr_value, "\n".$doc->toXML())); } } protected function assertNodeExists($doc, $xpath_query) { $result = $this->documentXpath($doc, $xpath_query); if (is_null($result->item(0))) { throw new PHPUnit_Framework_ExpectationFailedException( sprintf('XPath query "%s" does not return a result. Source: %s', $xpath_query, "\n".$doc->toXML())); } } protected function assertNodeDoesNotExist($doc, $xpath_query) { $result = $this->documentXpath($doc, $xpath_query); if (!is_null($result->item(0))) { throw new PHPUnit_Framework_ExpectationFailedException( sprintf('XPath query "%s" return a result. Source: %s', $xpath_query, "\n".$doc->toXML())); } } protected function assertTextNodeEquals($doc, $xpath_query, $value) { $result = $this->documentXpath($doc, $xpath_query); if ($result->item(0)->nodeValue != $value) { throw new PHPUnit_Framework_ExpectationFailedException( sprintf('XPath query "%s" does not match value "%s". Source: %s', $xpath_query, $value, "\n".$doc->toXML())); } } /** * Run an xpath query on csDocument resulting XML. I * know the code is a bit weird, but it is as weird as the php5 DOM XML * namespace handling. * * @param csDocument $doc * @param string $xpath_query * @return mixed */ private function documentXpath(csDocument $doc, $xpath_query) { $dom = new DOMDocument(); $dom->loadXML($doc->toXML()); // yeah, I *must* reload the xml $xpath = new DOMXPath($dom); $xpath->registerNamespace("svg", "http://www.w3.org/2000/svg"); // uh oh $xpath->registerNamespace("xlink", "http://www.w3.org/1999/xlink"); return $xpath->query($xpath_query, $dom); } }