dom_tester.php 3.4KB

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