Changeset 334

Show
Ignore:
Timestamp:
03/01/08 22:38:45 (6 months ago)
Author:
nperriault
Message:

refs #40 - base xml related test case utilities

Files:

Legend:

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

    r333 r334  
    2727 
    2828  /** 
    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. 
     29   * Checks if an attribute xpath query matches given value for given doc. 
    3230   * 
    3331   * @param  csDocument $doc 
     
    3836  protected function assertAttrValueEquals($doc, $xpath_query, $value) 
    3937  { 
    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); 
     38    $result = $this->documentXpath($doc, $xpath_query); 
    4539    $attr = $result->item(0); // First match 
    4640    if (!$attr) // Attribute not found 
     
    6054  } 
    6155 
     56  protected function assertTextNodeEquals($doc, $xpath_query, $value) 
     57  { 
     58    $result = $this->documentXpath($doc, $xpath_query); 
     59    if ($result->item(0)->nodeValue != $value) 
     60    { 
     61      throw new PHPUnit_Framework_ExpectationFailedException( 
     62        sprintf('XPath query "%s" does not match value "%s". Source: %s', 
     63                $xpath_query, $value, "\n".$doc->toXML())); 
     64    } 
     65    return true; 
     66  } 
     67 
     68  /** 
     69   * Run an xpath query on csDocument resulting XML. I 
     70   * know the code is a bit weird, but it is as weird as the php5 DOM XML 
     71   * namespace handling. 
     72   * 
     73   * @param  csDocument $doc 
     74   * @param  string $xpath_query 
     75   * @return mixed 
     76   */ 
     77  private function documentXpath(csDocument $doc, $xpath_query) 
     78  { 
     79    $dom = DOMDocument::loadXML($doc->toXML()); // yeah, I *must* reload the xml 
     80    $xpath = new DOMXPath($dom); 
     81    $xpath->registerNamespace("svg", "http://www.w3.org/2000/svg"); // uh oh 
     82    return $xpath->query($xpath_query, $dom); 
     83  } 
     84 
    6285} 
  • cleversvg/trunk/tests/csDocumentTest.php

    r326 r334  
    1515    $doc = $this->generateTestDoc(); 
    1616    $doc->setDescription('Added description'); 
    17     $xml = $doc->toXML(); 
    18     $this->assertEquals(preg_match('#<desc>Added description</desc>#si', $xml), 1, $xml); 
     17    $this->assertTextNodeEquals($doc, '/svg:svg/svg:desc', 'Added description'); 
     18    $doc->setDescription('Changed description'); 
     19    $this->assertTextNodeEquals($doc, '/svg:svg/svg:desc', 'Changed description'); 
    1920  } 
    2021 
     
    2223  { 
    2324    $doc = $this->generateTestDoc(); 
     25    $doc->setTitle('Added title'); 
     26    $this->assertTextNodeEquals($doc, '/svg:svg/svg:title', 'Added title'); 
    2427    $doc->setTitle('Changed title'); 
    25     $xml = $doc->toXML(); 
    26     $this->assertEquals(preg_match('#<title>Changed title</title>#si', $xml), 1, $xml); 
     28    $this->assertTextNodeEquals($doc, '/svg:svg/svg:title', 'Changed title'); 
    2729  } 
    2830