PseudoNodeTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\CssSelector\Node;
  11. use Symfony\Component\CssSelector\Node\PseudoNode;
  12. use Symfony\Component\CssSelector\Node\ElementNode;
  13. class PseudoNodeTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testToXpath()
  16. {
  17. $element = new ElementNode('*', 'h1');
  18. // h1:checked
  19. $pseudo = new PseudoNode($element, ':', 'checked');
  20. $this->assertEquals("h1[(@selected or @checked) and (name(.) = 'input' or name(.) = 'option')]", (string) $pseudo->toXpath(), '->toXpath() returns the xpath representation of the node');
  21. // h1:first-child
  22. $pseudo = new PseudoNode($element, ':', 'first-child');
  23. $this->assertEquals("*/*[name() = 'h1' and (position() = 1)]", (string) $pseudo->toXpath(), '->toXpath() returns the xpath representation of the node');
  24. // h1:last-child
  25. $pseudo = new PseudoNode($element, ':', 'last-child');
  26. $this->assertEquals("*/*[name() = 'h1' and (position() = last())]", (string) $pseudo->toXpath(), '->toXpath() returns the xpath representation of the node');
  27. // h1:first-of-type
  28. $pseudo = new PseudoNode($element, ':', 'first-of-type');
  29. $this->assertEquals("*/h1[position() = 1]", (string) $pseudo->toXpath(), '->toXpath() returns the xpath representation of the node');
  30. // h1:last-of-type
  31. $pseudo = new PseudoNode($element, ':', 'last-of-type');
  32. $this->assertEquals("*/h1[position() = last()]", (string) $pseudo->toXpath(), '->toXpath() returns the xpath representation of the node');
  33. // h1:only-child
  34. $pseudo = new PseudoNode($element, ':', 'only-child');
  35. $this->assertEquals("*/*[name() = 'h1' and (last() = 1)]", (string) $pseudo->toXpath(), '->toXpath() returns the xpath representation of the node');
  36. // h1:only-of-type
  37. $pseudo = new PseudoNode($element, ':', 'only-of-type');
  38. $this->assertEquals("h1[last() = 1]", (string) $pseudo->toXpath(), '->toXpath() returns the xpath representation of the node');
  39. // h1:empty
  40. $pseudo = new PseudoNode($element, ':', 'empty');
  41. $this->assertEquals("h1[not(*) and not(normalize-space())]", (string) $pseudo->toXpath(), '->toXpath() returns the xpath representation of the node');
  42. }
  43. }