dom_tester.php 3.4 KB

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