Show
Ignore:
Timestamp:
03/01/08 22:10:54 (4 years ago)
Author:
nperriault
Message:

refs #40 - base xml related test case utilities

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cleversvg/trunk/tests/csBaseTestCase.class.php

    r326 r333  
    99class csBaseTestCase extends PHPUnit_Framework_TestCase 
    1010{ 
    11  
    12   /** 
    13    * Generated sample csCircle 
    14    * 
    15    * @return csCircle 
    16    */ 
    17   protected function generateTestCircle() 
    18   { 
    19     $doc = new csCircle(); 
    20     $doc->setCx(10); 
    21     $doc->setCy(5); 
    22     $doc->setRadius(12); 
    23     $doc->setX('blue'); 
    24     $doc->setX(2); 
    25     return $doc; 
    26   } 
    27  
    28   /** 
    29    * Generated sample cdBaseElement 
    30    * 
    31    * @return csBaseElement 
    32    */ 
    33   protected function generateTestBaseElement() 
    34   { 
    35     $doc = new csBaseElement(); 
    36     return $doc; 
    37   } 
    3811 
    3912  /** 
     
    5326  } 
    5427 
     28  /** 
     29   * Checks if an attribute xpath query matches given value for given doc. I 
     30   * know the code is a bit weird, but it is as weird as the php5 DOM XML 
     31   * namespace handling. 
     32   * 
     33   * @param  csDocument $doc 
     34   * @param  string     $xpath_query 
     35   * @param  mixed      $value 
     36   * @return boolean 
     37   */ 
     38  protected function assertAttrValueEquals($doc, $xpath_query, $value) 
     39  { 
     40    $dom = $doc->getDomDocument(); 
     41    $dom = DOMDocument::loadXML($doc->toXML()); // yeah, I *must* reload the xml 
     42    $xpath = new DOMXPath($dom); 
     43    $xpath->registerNamespace("svg", "http://www.w3.org/2000/svg"); // uh oh 
     44    $result = $xpath->query($xpath_query, $dom); 
     45    $attr = $result->item(0); // First match 
     46    if (!$attr) // Attribute not found 
     47    { 
     48      throw new PHPUnit_Framework_ExpectationFailedException( 
     49        sprintf('No attribute found for query "%s". Source: %s', 
     50                $xpath_query, "\n".$doc->toXML())); 
     51    } 
     52    $attr_value = $attr->value; 
     53    if (!$attr_value or $attr_value != (string) $value) // String compare 
     54    { 
     55      throw new PHPUnit_Framework_ExpectationFailedException( 
     56        sprintf('XPath query "%s" does not match value "%s". Source: %s', 
     57                $xpath_query, $value, "\n".$doc->toXML())); 
     58    } 
     59    return true; 
     60  } 
     61 
    5562}