AttribNodeTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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\AttribNode;
  12. use Symfony\Component\CssSelector\Node\ElementNode;
  13. class AttribNodeTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testToXpath()
  16. {
  17. $element = new ElementNode('*', 'h1');
  18. $operators = array(
  19. '^=' => "h1[starts-with(@class, 'foo')]",
  20. '$=' => "h1[substring(@class, string-length(@class)-2) = 'foo']",
  21. '*=' => "h1[contains(@class, 'foo')]",
  22. '=' => "h1[@class = 'foo']",
  23. '~=' => "h1[contains(concat(' ', normalize-space(@class), ' '), ' foo ')]",
  24. '|=' => "h1[@class = 'foo' or starts-with(@class, 'foo-')]",
  25. '!=' => "h1[not(@class) or @class != 'foo']",
  26. );
  27. // h1[class??foo]
  28. foreach ($operators as $op => $xpath) {
  29. $attrib = new AttribNode($element, '*', 'class', $op, 'foo');
  30. $this->assertEquals($xpath, (string) $attrib->toXpath(), '->toXpath() returns the xpath representation of the node');
  31. }
  32. // h1[class]
  33. $attrib = new AttribNode($element, '*', 'class', 'exists', 'foo');
  34. $this->assertEquals('h1[@class]', (string) $attrib->toXpath(), '->toXpath() returns the xpath representation of the node');
  35. }
  36. }