Source for file dom_tester.php

Documentation is available at dom_tester.php

  1. <?php
  2.  
  3. /**
  4.  *    @package    SimpleTest
  5.  *    @subpackage    DomTestCase
  6.  *  @author     Perrick Penet <perrick@noparking.net>
  7.  *    @version    $Id: dom_tester.php 1637 2008-01-20 20:18:54Z pp11 $
  8.  */
  9.  
  10. /**#@+
  11.  * include SimpleTest files
  12.  */
  13. require_once dirname(__FILE__).'/../web_tester.php';
  14. require_once dirname(__FILE__).'/dom_tester/css_selector.php';
  15. /**#@-*/
  16.  
  17.  * CssSelectorExpectation
  18.  * 
  19.  * Create a CSS Selector expectation
  20.  * 
  21.  * @param DomDocument $_dom 
  22.  * @param string $_selector 
  23.  * @param array $_value 
  24.  * 
  25.  */
  26.  
  27.     var $_dom;
  28.     var $_selector;
  29.     var $_value;
  30.     
  31.     /**
  32.      *    Sets the dom tree and the css selector to compare against
  33.      *    @param mixed $dom          Dom tree to search into.
  34.      *    @param string $selector    Css selector to match element.
  35.      *    @param string $message     Customised message on failure.
  36.      *    @access public
  37.      */
  38.     function CssSelectorExpectation($dom$selector$message '%s'{
  39.         $this->SimpleExpectation($message);
  40.         $this->_dom = $dom;
  41.         $this->_selector = $this->_normalizeSelector($selector);
  42.         
  43.         $css_selector new CssSelector($this->_dom);
  44.         $this->_value = $css_selector->getTexts($this->_selector);
  45.     }
  46.  
  47.     /**
  48.      *    Normalizes the selector. In particular, replaces single-quotes with
  49.      *    double-quotes.
  50.      *    @param string $selector     Css selector to match element.
  51.      *    @return string              Normalized Css selector.
  52.      *    @access public
  53.      */
  54.     function _normalizeSelector($selector{
  55.         return str_replace("'"'"'$selector);
  56.     }    
  57.  
  58.     /**
  59.      *    Tests the expectation. True if it matches the
  60.      *    held value.
  61.      *    @param mixed $compare        Comparison value.
  62.      *    @return boolean              True if correct.
  63.      *    @access public
  64.      */
  65.     function test($compare{
  66.             return (($this->_value == $compare&& ($compare == $this->_value));
  67.     }
  68.     
  69.     /**
  70.      *    Returns a human readable test message.
  71.      *    @param mixed $compare      Comparison value.
  72.      *    @return string             Description of success
  73.      *                                or failure.
  74.      *    @access public
  75.      */
  76.     function testMessage($compare{
  77.         $dumper &$this->_getDumper();
  78.         if (is_array($compare)) {
  79.             sort($compare);
  80.         }
  81.         if ($this->test($compare)) {
  82.             return "CSS selector expectation [" $dumper->describeValue($this->_value"]".
  83.                     " using [" $dumper->describeValue($this->_selector"]";
  84.         else {
  85.             return "CSS selector expectation [" $dumper->describeValue($this->_value"]".
  86.                     " using [" $dumper->describeValue($this->_selector"]".
  87.                     " fails with [" .
  88.                     $dumper->describeValue($compare"] " .
  89.                     $dumper->describeDifference($this->_value$compare);
  90.         }
  91.     }
  92. }
  93.  
  94. /**
  95.  * DomTestCase
  96.  * 
  97.  * Extend Web test case with DOM related assertions,
  98.  * CSS selectors in particular
  99.  * 
  100.  * @category Testing
  101.  * @package DomTestCase
  102.  * 
  103.  * @param DomDocument $dom 
  104.  * 
  105.  */
  106. class DomTestCase extends WebTestCase {
  107.     var $dom;
  108.  
  109.     function assertElementsBySelector($selector$elements$message '%s'{
  110.         $this->dom = new DomDocument('1.0''utf-8');
  111.         $this->dom->validateOnParse true;
  112.         $this->dom->loadHTML($this->_browser->getContent());
  113.  
  114.         return $this->assert(
  115.                 new CssSelectorExpectation($this->dom$selector),
  116.                 $elements,
  117.                 $message);
  118.     }
  119. }
  120.  
  121. ?>

Documentation generated on Sun, 04 May 2008 09:21:26 -0500 by phpDocumentor 1.3.0