| 1 |
<?php |
|---|
| 2 |
require_once 'PHPUnit/Framework.php'; |
|---|
| 3 |
require_once dirname(__FILE__).'/../cleversvg.php'; |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
class csBaseTestCase extends PHPUnit_Framework_TestCase |
|---|
| 10 |
{ |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
* Generated sample csDocument |
|---|
| 14 |
* |
|---|
| 15 |
* @return csDocument |
|---|
| 16 |
*/ |
|---|
| 17 |
protected function generateTestDoc() |
|---|
| 18 |
{ |
|---|
| 19 |
$doc = new csDocument(320, 240, 'SVG test document'); |
|---|
| 20 |
$rect = new csRect(60, 60, 40, 40); |
|---|
| 21 |
$rect->setFill('red'); |
|---|
| 22 |
$rect->setStroke('yellow'); |
|---|
| 23 |
$rect->setStrokeWidth('4'); |
|---|
| 24 |
$doc->addElement($rect); |
|---|
| 25 |
return $doc; |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
* Checks if an attribute exists for given node |
|---|
| 30 |
* |
|---|
| 31 |
* @param csDocument $doc |
|---|
| 32 |
* @param string $xpath_query |
|---|
| 33 |
* @param string $attr_name |
|---|
| 34 |
* @return boolean |
|---|
| 35 |
*/ |
|---|
| 36 |
public function assertNodeAttrExists($doc, $xpath_query, $attr_name) |
|---|
| 37 |
{ |
|---|
| 38 |
$result = $this->documentXpath($doc, $xpath_query); |
|---|
| 39 |
$this->assertNodeExists($doc, $xpath_query); |
|---|
| 40 |
if (!$result->item(0)->hasAttribute($attr_name)) |
|---|
| 41 |
{ |
|---|
| 42 |
throw new PHPUnit_Framework_ExpectationFailedException( |
|---|
| 43 |
sprintf('No attribute "%s" found in node retrieved by "%s". Source: %s', |
|---|
| 44 |
$attr_name, $xpath_query, "\n".$doc->toXML())); |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
* Checks that an attribute does not exist for given node |
|---|
| 50 |
* |
|---|
| 51 |
* @param csDocument $doc |
|---|
| 52 |
* @param string $xpath_query |
|---|
| 53 |
* @param string $attr_name |
|---|
| 54 |
* @return boolean |
|---|
| 55 |
*/ |
|---|
| 56 |
public function assertNodeAttrNotExists($doc, $xpath_query, $attr_name) |
|---|
| 57 |
{ |
|---|
| 58 |
$result = $this->documentXpath($doc, $xpath_query); |
|---|
| 59 |
$this->assertNodeExists($doc, $xpath_query); |
|---|
| 60 |
if ($result->item(0)->hasAttribute($attr_name)) |
|---|
| 61 |
{ |
|---|
| 62 |
throw new PHPUnit_Framework_ExpectationFailedException( |
|---|
| 63 |
sprintf('Attribute %s="%s" retrieved by node "%s". Source: %s', |
|---|
| 64 |
$attr_name, |
|---|
| 65 |
$result->item(0)->getAttribute($attr_name), |
|---|
| 66 |
$xpath_query, "\n".$doc->toXML())); |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
* Checks if an attribute xpath query matches given value for given doc. |
|---|
| 72 |
* |
|---|
| 73 |
* @param csDocument $doc |
|---|
| 74 |
* @param string $xpath_query |
|---|
| 75 |
* @param mixed $value |
|---|
| 76 |
* @return boolean |
|---|
| 77 |
*/ |
|---|
| 78 |
protected function assertAttrValueEquals($doc, $xpath_query, $value) |
|---|
| 79 |
{ |
|---|
| 80 |
$result = $this->documentXpath($doc, $xpath_query); |
|---|
| 81 |
$attr = $result->item(0); |
|---|
| 82 |
if (!$attr) |
|---|
| 83 |
{ |
|---|
| 84 |
throw new PHPUnit_Framework_ExpectationFailedException( |
|---|
| 85 |
sprintf('No attribute found for query "%s". Source: %s', |
|---|
| 86 |
$xpath_query, "\n".$doc->toXML())); |
|---|
| 87 |
} |
|---|
| 88 |
$attr_value = $attr->value; |
|---|
| 89 |
if (!$attr_value or $attr_value != (string) $value) |
|---|
| 90 |
{ |
|---|
| 91 |
throw new PHPUnit_Framework_ExpectationFailedException( |
|---|
| 92 |
sprintf('XPath query "%s" does not match value "%s", but "%s". Source: %s', |
|---|
| 93 |
$xpath_query, $value, $attr_value, "\n".$doc->toXML())); |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
protected function assertNodeExists($doc, $xpath_query) |
|---|
| 98 |
{ |
|---|
| 99 |
$result = $this->documentXpath($doc, $xpath_query); |
|---|
| 100 |
if (is_null($result->item(0))) |
|---|
| 101 |
{ |
|---|
| 102 |
throw new PHPUnit_Framework_ExpectationFailedException( |
|---|
| 103 |
sprintf('XPath query "%s" does not return a result. Source: %s', |
|---|
| 104 |
$xpath_query, "\n".$doc->toXML())); |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
protected function assertNodeDoesNotExist($doc, $xpath_query) |
|---|
| 109 |
{ |
|---|
| 110 |
$result = $this->documentXpath($doc, $xpath_query); |
|---|
| 111 |
if (!is_null($result->item(0))) |
|---|
| 112 |
{ |
|---|
| 113 |
throw new PHPUnit_Framework_ExpectationFailedException( |
|---|
| 114 |
sprintf('XPath query "%s" return a result. Source: %s', |
|---|
| 115 |
$xpath_query, "\n".$doc->toXML())); |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
protected function assertTextNodeEquals($doc, $xpath_query, $value) |
|---|
| 120 |
{ |
|---|
| 121 |
$result = $this->documentXpath($doc, $xpath_query); |
|---|
| 122 |
if ($result->item(0)->nodeValue != $value) |
|---|
| 123 |
{ |
|---|
| 124 |
throw new PHPUnit_Framework_ExpectationFailedException( |
|---|
| 125 |
sprintf('XPath query "%s" does not match value "%s". Source: %s', |
|---|
| 126 |
$xpath_query, $value, "\n".$doc->toXML())); |
|---|
| 127 |
} |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
* Run an xpath query on csDocument resulting XML. I |
|---|
| 132 |
* know the code is a bit weird, but it is as weird as the php5 DOM XML |
|---|
| 133 |
* namespace handling. |
|---|
| 134 |
* |
|---|
| 135 |
* @param csDocument $doc |
|---|
| 136 |
* @param string $xpath_query |
|---|
| 137 |
* @return mixed |
|---|
| 138 |
*/ |
|---|
| 139 |
private function documentXpath(csDocument $doc, $xpath_query) |
|---|
| 140 |
{ |
|---|
| 141 |
$dom = new DOMDocument(); |
|---|
| 142 |
$dom->loadXML($doc->toXML()); |
|---|
| 143 |
$xpath = new DOMXPath($dom); |
|---|
| 144 |
$xpath->registerNamespace("svg", "http://www.w3.org/2000/svg"); |
|---|
| 145 |
$xpath->registerNamespace("xlink", "http://www.w3.org/1999/xlink"); |
|---|
| 146 |
return $xpath->query($xpath_query, $dom); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
} |
|---|
| 150 |
|
|---|